最新消息:20190529 VPS服务器已从腾讯云香港换为Vultr新加坡,主题仍用朋友推荐的大前端D8

[已解决]Swift中Property的willSet中警告:Attempting to store to property within its own willSet, which is about to be overwritten by the new value

Swift crifan 903浏览 0评论

[问题]

swift代码:

class RecordTabBarController : UIViewController {
    var viewControllers = [UIViewController]()
    {
        willSet(newViewControllers){
            。。。。。 
            viewControllers = newViewControllers

 

出现警告:⚠️

Attempting to store to property ‘viewControllers’ within its own willSet, which is about to be overwritten by the new value

如图:

Attempting to store to property within its own willSet

要去搞清楚,此警告是否一定要解决掉,还是可以忽略无所谓。

因为觉得这个警告有点怪:

本来在willSet,就是拿到newXXX进行处理掉,把旧的XXX换掉。

结果Xcode却还是警告。不知道为啥。

 

[解决过程]

1.搜:

swift Attempting to store to property within its own willSet, which is about to be overwritten by the new value

参考:

这个警告是什么原因啊-敏捷大拇指-智能移动社区,垂直到苹果新语言swift

ios – Attempting to store to property ” within its own willSet – Stack Overflow

说是一个已知的bug:

Developer Forums: Compiler warns on non-setting willSet

前面加上self即可:

self.viewControllers = newViewControllers

 

[总结]

此处,在willSet中用新的值newXXX,赋值给旧的值XXX,出现了警告:

Attempting to store to property within its own willSet, which is about to be overwritten by the new value

原因是:

这个是Xcode本身的bug

解决办法是:

在旧的值前面加上self.,变成self.XXX即可。

转载请注明:在路上 » [已解决]Swift中Property的willSet中警告:Attempting to store to property within its own willSet, which is about to be overwritten by the new value

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
64 queries in 0.116 seconds, using 18.78MB memory