最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift代码出错:Cannot invoke initializer for type [UIViewController] with an argument list of type (() -> ())

Swift crifan 6484浏览 0评论

[背景]

折腾:

[已解决]Swift代码出错:Method setViewControllers with Objective-C selector setViewControllers: conflicts with setter for viewControllers with the same Objective-C selector

期间,之前初始化代码:

   var viewControllers = [UIViewController]()

是好好的,没问题的。

然后为了加willSet和didSet:

  var viewControllers = [UIViewController]() {
        willset(newViewControllers:[UIViewController]){
           
        }
       
        didSet {
           
        }
    }

结果却出错了:

Cannot invoke initializer for type ‘[UIViewController]’ with an argument list of type ‘(() -> ())’

如图:

Cannot invoke initializer for type UIViewController with an argument list of type

 

[解决过程]

1.搜:

swift Cannot invoke initializer for type [UIViewController] with an argument list of type (() -> ())

参考:

regex – Error after updating to xcode 7 : Cannot invoke initializer for type ‘NSRegularExpression’ with an argument list of type – Stack Overflow

ios – "Cannot find an initializer for type ‘MKPlaceMark’ that accepts an argument list of type – Stack Overflow

 

2.抽空去搞清楚:

[已解决]Swift中Property、Computed property和Property Observer的区别

 

3.那就再去看看UIViewController,是有几种initializer

不对,应该是:

搞清楚,对于数组类型的[UIViewController],如何初始化。

搜:

swift how init UIViewController array

swift how init class array

参考:

The Swift Programming Language (Swift 2.1): Collection Types

ios – Swift: Array over custom class does not initialize – Stack Overflow

一个类UIViewController的数组的初始化,的确就是:

[UIViewController]()

的啊

-》所以之前的代码,才可以正常通过编译的啊。。。

-》但是为何现在就是不能通过编译呢。。。

算了,还是先去看看:

[已解决]Swift中尝试给变量加willSet和didSet出错:Use of unresolved identifier willset didSet

 

4.参考:

The Swift Programming Language (Swift 2): Properties

去试试,去掉初始化()

折腾到后来,终于通过编译了:

    var viewControllers = [UIViewController]()
    {
        //willSet(newViewControllers:[UIViewController]){
        willSet(newViewControllers){
           
        }

        didSet {
           
        }
    }

 

[总结]

之前的代码:

    var viewControllers = [UIViewController]() {
        willset(newViewControllers:[UIViewController]){
           
        }
       
        didSet {
           
        }
       
    }

之所以出错是因为:

  1. 1.willSet,不小心写成willset了
  2. 2.willSet的参数的类型,不需要再重复制定为[UIViewController],所以直接用:
    • willSet(newViewControllers
    • 即可。
  3. 之前的UIViewController的数组的初始化,用:
    • var viewControllers = [UIViewController]()
    • 是正确的,没问题的。

 

此处之所以报错:

Cannot invoke initializer for type [UIViewController] with an argument list of type (() -> ())

是因为当时上面的willSet部分出错而引出来的连带的错误

解决了上面的错误,此处错误自然就消失了。

 

最后正确的代码:

    var viewControllers = [UIViewController]()
    {
        willSet(newViewControllers){
           
        }

        didSet {
           
        }
    }

即可通过初始化。

转载请注明:在路上 » [已解决]Swift代码出错:Cannot invoke initializer for type [UIViewController] with an argument list of type (() -> ())

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.138 seconds, using 22.08MB memory