gpt4 book ai didi

Swift 1.2 [AnyObject] 没有名为 'objectForKey' 的成员

转载 作者:可可西里 更新时间:2023-11-01 01:06:01 25 4
gpt4 key购买 nike

升级到 Swift 1.2 后,出现错误:

[AnyObject] does not have member named 'objectForKey'

代码:

self.photoNames = objects.map { $0.objectForKey("PhotoName") as! String }

objects 是类型 [AnyObject]

编辑:(更多代码)

var photoNames: [String] = []
var query = PFQuery(className: "Photos")
query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]?, error: NSError?) in
if(error == nil){
self.photoNames = objects.map { $0.objectForKey("PhotoName") as! String }
}
else{
println("Error in retrieving \(error)")
}

})

最佳答案

objectForKey:NSDictionary的成员方法,不是AnyObject。这是 Reference .

如果 objectsNSDictionary 的数组,试试这个:

self.photoNames = objects?.map { ($0 as? NSDictionary)?.objectForKey("PhotoName") as! String } ?? []

关于Swift 1.2 [AnyObject] 没有名为 'objectForKey' 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30116457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com