我有一个函数:
func parseJSON3(inputData: NSData) -> NSArray {
var tempDict: (id:Int, ccomments:Int, post_date:String, post_title:String, url:String) = (id: 0, ccomments: 0, post_date: "null", post_title: "null", url: "null")
var resultArray: (id:Int, ccomments:Int, post_date:String, post_title:String, url:String)[] = []
var error: NSError?
var jsonDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
var firstArray = jsonDictionary.objectForKey("locations") as NSArray
for dict in firstArray {
tempDict.id = dict.valueForKey("ID") as Int
tempDict.ccomments = dict.valueForKey("ccomments") as Int
tempDict.post_date = dict.valueForKey("post_date") as String
tempDict.post_title = dict.valueForKey("post_title") as String
tempDict.url = dict.valueForKey("url") as String
resultArray.append(tempDict)
}
return resultArray
}
排队
resultArray.append(tempDict)
我有一个错误:
调用中缺少参数“ccomments”的参数
为什么?请帮助....
最佳答案
在我看来,resultArray.append()
有点像对待可变参数一样对待元组,并试图扩展元组以匹配它自己的参数。它提示你的第二个参数,因为它只期待一个。我没有在任何地方看到 Array.append()
的这种行为,所以我认为这是 Swift 中的一个错误。
使用附加运算符 +=
似乎没有这个问题:
resultArray += tempDict
关于nsarray - Swift 中的元组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24210692/