繁体   English   中英

下标快速使用不明确2

[英]Ambiguous use of subscript swift 2

在第二章中,出现了:下标的模棱两可的使用方式有人知道发生了什么吗?

 var plistDict: NSDictionary?
        if let path = NSBundle.mainBundle().pathForResource("myTip", ofType: "plist") {
            plistDict = NSDictionary(contentsOfFile: path)
            let plistArray = plistDict!["tip"]
            let randV = Int(arc4random_uniform(UInt32(plistArray!.count)))
        ------->>>>    let tipMessage = plistArray![randV] //error here???

                let tipAlert = UIAlertController(title: "Something...", message: tipMessage as? String, preferredStyle: UIAlertControllerStyle.Alert)
                tipAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

当你plistArrayplistDict ,它不知道元素的类型是什么,所以它认为一个AnyObject? 因此,它不知道如何处理给定的下标,因为它不知道如何处理AnyObject类型的下标。 如果在将plistDict!["tip"]分配给plistArray时将其转换为适当的类型,则它应该能够找出来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM