【问题标题】:Array of tuples in SwiftSwift 中的元组数组
【发布时间】:2014-06-13 17:18:42
【问题描述】:

我有一个函数:

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)

我有一个错误:

调用中的参数“ccmets”缺少参数

为什么?请帮忙....

【问题讨论】:

    标签: nsarray tuples swift ios8


    【解决方案1】:

    在我看来resultArray.append() 将元组有点像可变参数,并试图扩展元组以匹配它自己的参数。它抱怨你的第二个参数,因为它只期待一个。我还没有在任何地方看到 Array.append() 的这种行为,所以我会说这是 Swift 中的一个错误。

    使用附加运算符+= 似乎没有这个问题:

    resultArray += tempDict
    

    【讨论】:

    • 我不相信这描述了这种行为,请参阅我的回答以了解我的一些有趣的调查结果!
    【解决方案2】:

    所以这很疯狂 - 不确定我是否会将其定性为错误或未记录的行为,但这绝对是应该在雷达上进行修复/澄清的事情!

    情况是append 将您的参数tempDict(我们希望它是一个接受单个成员并将其添加到集合中的 Array 方法的唯一参数)作为 first 签名中的参数,它正在寻找 5 个参数 (!),每个参数对应 Array 所拥有的 Tuple 类型的每个成员。

    请参阅以下内容了解一些有趣的行为(包括为 1-member 'Tuple' 的单个成员分配标签??)->

    var arrayOne: Array<String> = []
    arrayOne.append("hi")
    println(arrayOne[0])        // hi
    
    var arrayTwo: Array<(String)> = []    // where (String) is a single-member Tuple
    arrayTwo.append("hi")
    println(arrayTwo[0])        // hi
    println(arrayTwo[0].0)      // hi  -> using .0 subscript to access the first member of the Tuple
    
    // wanna see something crazy? remember arrayOne, that holds members of type String?
    println(arrayOne[0].0)      // hi  -> this Array does not hold Tuples, but it looks like we can still treat its members like "single-member Tuples"?
    
    var arrayThree: Array<(str: String)> = []    // members of the Array are single-member Tuples with the label 'str' for their member
    arrayThree.append(str: "hi")                 // now we can't use append without providing the label 'str', acting as what looks like an argument label?
    var byeString = "bye"
    var byeTuple = ("bye")
    arrayThree += byeString     // += doesn't care about the label, and will take either a String or a single-member Tuple holding a String
    arrayThree += byeTuple
    println(arrayThree[0])      // hi
    println(arrayThree[0].0)    // hi
    println(arrayThree[0].str)  // hi  -> accessing the single member of the Tuple by its label
    

    ...所以在您的情况下,您看到append 的错误它想要您做的是(使用您用来将元组声明为看起来像参数标签的标签):

    var resultArray: (id:Int, ccomments:Int, post_date:String, post_title:String, url:String)[] = []
    ...
    resultArray.append(id: someIntValue, ccomments: someOtherIntValue, post_date: someStringValue, post_title: someOtherStringValue, url: someAnotherStringValue)
    

    ...当然,如前所述,您可以通过使用 += 来避免这样做

    疯狂的东西!可能是为了某种目的而设计的,可能是协议继承的结果,而这并不是为了产生这种效果......知道答案会很有趣!

    【讨论】:

    • 这真的很有趣(并且还在 Beta 3 中发生)。我想这与func foo(x:Int,y:String)func foo((x:Int,y:String)) 具有相同的效果有关。
    【解决方案3】:

    resultArray.append() 似乎将tempDict 作为第一个元组元素 (id)。

    改成:

    resultArray += tempDict

    似乎可以编译和工作。

    我不确定为什么append() 的行为方式不同,也许你可以提交一个错误!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-19
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      相关资源
      最近更新 更多