Swift ';任何对象?';没有名为'的成员;计数';编译错误
首先,我尝试在下面的代码中使用JSON序列化,并尝试循环遍历数组中的每个可用项。然而,这一定是类型铸造,我错过了一些东西。以下是我正在尝试做的事情的片段:Swift ';任何对象?';没有名为'的成员;计数';编译错误,swift,ios8,Swift,Ios8,首先,我尝试在下面的代码中使用JSON序列化,并尝试循环遍历数组中的每个可用项。然而,这一定是类型铸造,我错过了一些东西。以下是我正在尝试做的事情的片段: let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary va
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options:
NSJSONReadingOptions.MutableContainers, error: nil) as
NSDictionary
var items = [[String:String]()]
var item: AnyObject
var authorDictionary: AnyObject
for var i = 0; i < jsonResult["items"].count; i++ {
items.append([String:String]())
items[i]["content"] = item["content"] as? NSString
items[i]["title"] = item["title"] as? NSString
items[i]["publishedDate"] = item["published"] as? NSString
authorDictionary = item["author"] as NSDictionary
items[i]["author"] = item["displayName"] as? NSString
}
让jsonResult=NSJSONSerialization.JSONObjectWithData(数据,选项:
NSJSONReadingOptions.MutableContainers,错误:nil)为
字典
var items=[[String:String]()]
var项:AnyObject
var authorDictionary:AnyObject
对于var i=0;i
要解决错误,请更改以下内容:
jsonResult["items"]
为此:
(jsonResult["items"] as! NSArray)
对不起,我这里没有我的xcode,但是在查看代码时,我想到了一些关于这个的东西
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options:
NSJSONReadingOptions.MutableContainers, error: nil) as
NSDictionary
var items = [[String:String]()]
var item: AnyObject
var authorDictionary: AnyObject
if let itemsFromJson:NSArray = jsonResult["items"] as? NSArray
{
for itemJson in itemsFromJson {
items.append([String:String]())
items[i]["content"] = itemJson["content"] as? NSString
items[i]["title"] = itemJson["title"] as? NSString
items[i]["publishedDate"] = itemJson["published"] as? NSString
authorDictionary = itemJson["author"] as NSDictionary
items[i]["author"] = itemJson["displayName"] as? NSString
}
}
这与Xcode无关。您得到的编译器错误可以通过将对象强制转换为具有
.count
属性的类型来解决。此外,您的项
数组将始终有一个空字典,因为您使用一个字典初始化它,并且每次迭代时都会在数组末尾追加一个新字典。