Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

nsarray - Array of tuples in Swift

I have a function:

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
}

In line

resultArray.append(tempDict)

I have an error:

Missing argument for parameter 'ccomments' in call

Why? Help please....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks to me like resultArray.append() is treating the tuple a little bit like a variadic parameter, and trying to expand the tuple to match its own arguments. It's complaining about your second parameter because it's only expecting one. I haven't seen this behavior for Array.append() documented anywhere, so I would say it's a bug in Swift.

Using the appending operator += doesn't seem to have that issue:

resultArray += tempDict

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...