简体   繁体   中英

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? . 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. If you cast plistDict!["tip"] to the appropriate type when assigning it to plistArray , then it should be able to figure it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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