问题标签 [mutating-function]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
23 浏览

ios - 如何在swiftui中停止观察模型中特定变量的变化

我有一个模型,我有一个甲板变量。现在我想从 deck array 中删除十二个项目。但是,我删除的每个项目的模型都在发生变化,我希望仅在移除十二张卡片后才能更改视图。函数 getTwelveCards 正在连续运行并且所有卡片都被移除。所需的功能是只删除十二张卡片这是我的模型:

这是我的视图模型:

这是我的观点:

0 投票
1 回答
27 浏览

swift - 包含惰性初始化器的 Swift 结构的初始化问题

我从一个在线编程练习网站为 Swift 编程练习编写了两个版本的代码。练习如下:

求和的平方与前 N 个自然数的平方和之间的差。前十个自然数的平方和为 (1 + 2 + ... + 10)² = 55² = 3025。前十个自然数的平方和为 1² + 2² + ... + 10² = 385。因此,前十个自然数之和的平方和前十个自然数的平方和之差为 3025 - 385 = 2640。

第一个版本的代码是:

它通过编译并运行没有问题。

第二个版本的代码是:

编译代码时,编译器会生成以下消息:

如果我按照编译器的建议更改letvarfor sqrs,代码将运行良好。或者,如果我将类型更改structclass使对象可变,代码也将运行良好。struct但是,在我的第一个版本的代码中,对象和let可变性分隔符的组合没有问题。怎么在第二个版本中,这两个变得不一致?虽然我不知道为什么,但我怀疑这可能与第二个版本中的惰性初始化器有关。

谁能帮忙解释一下?非常感谢您提前。

0 投票
1 回答
54 浏览

swift - Swift mutating function struct pass struct’s variable name in function?

I am working in Swift trying to update an organization struct that will need to hold a latitude and longitude. I created a mutating function in the struct that will update the latitude and longitude based on the organization organization struct’s address. I got it to work, but the issue is that when I call the mutating function, I need to manually enter the variable name with the .latitude and .longitude. Is there a way that I can pass the variable struct’s name automatically and reference the .latitude and .longitude without calling the specific variable name with it so I can make it more usable? I included an example below with my code. Thanks for your help!

0 投票
1 回答
384 浏览

typescript - TypeScript / Vue 3:注入变异函数会导致 TypeScript 错误“对象属于‘未知’类型”

我是 TypeScript 的新手,并尝试将它与 Vue 3 组合 API 一起使用并提供/注入。假设在父组件中A我有这样的东西:

...然后想updateScore在子组件中注入函数B以便能够更新父组件中的值A这是文档推荐的)。不幸的是,我收到一个 TS 错误Object is of type 'unknown'

我应该怎么做才能修复 TypeScript 错误?我找不到任何关于在 TS 中注入更新函数的示例。

0 投票
1 回答
50 浏览

swift - 函数覆盖和函数变异之间更准确的区别是什么?特别是迅速

本质上,两者都用于根据我们的自定义需求修改函数的行为。但是,当两者都用于同一目的时,为什么有必要使用两种方法来做同一件事。

我假设,如果一个函数具有带参数的HEAD和具有这些参数的特定功能的BODY

当我们必须在HEAD进行修改时,使用变异功能。

变异-> HEAD ->参数

当我们必须在BODY处修改时使用覆盖函数

覆盖->身体->功能

我在互联网上搜索过..但在任何地方都找不到令人满意的解释。请帮助我更好地理解它们。如果我错了,请纠正我。