nsarray - Swift 中的元组数组

标签 nsarray tuples swift ios8

我有一个函数:

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/

相关文章:

objective-c - 如何实现像 NSArray 这样的自定义对象

Java - 如何构建唯一的对象元组(n 维)?

ios - 如何移动文本框?

ios - IOS swift如何从 "multi-segue relationship page"回到主页面

iphone - 为什么我的 NSArray intValue 没有按预期工作?

ios - 使用NSPredicates在数组上在Objective-C中解析JSON

c++ - 我可以将新值分配/移动到元组内的 unique_ptr 中吗?

ios - swift:细胞中的图像

ios - 如何在 Objective C 中将 NSString 转换为 NSArray

Python 计算,使用元组、列表。 Python 2.7