简体   繁体   English

下标快速使用不明确2

[英]Ambiguous use of subscript swift 2

In swift 2 this came up: Ambiguous use of 'subscript' Does anyone have any idea what is going on? 在第二章中,出现了:下标的模棱两可的使用方式有人知道发生了什么吗?

 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))

When you get plistArray out of plistDict , it doesn't know what the type of element it is, so it considers it an AnyObject? 当你plistArrayplistDict ,它不知道元素的类型是什么,所以它认为一个AnyObject? . And because of that, it doesn't know how to deal with the subscript that you're giving it, because it doesn't know how to deal with subscripts for the AnyObject type. 因此,它不知道如何处理给定的下标,因为它不知道如何处理AnyObject类型的下标。 If you cast plistDict!["tip"] to the appropriate type when assigning it to plistArray , then it should be able to figure it out. 如果在将plistDict!["tip"]分配给plistArray时将其转换为适当的类型,则它应该能够找出来。

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

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