gpt4 book ai didi

swift - 具有单个元素的元组

转载 作者:搜寻专家 更新时间:2023-11-01 06:50:29 24 4
gpt4 key购买 nike

我有以下语法,用于具有单个值的元组

let http200Status = (statusCode: 200, description: "OK")
print("The status code is \(http200Status.statusCode)")

// Prints "The status code is 200"

现在,如果我将个人值(value)更改为

    let http200Status = (Code: 200, Code: "OK")
print("The status code is \(http200Status.0)"). // 200
print("The status code is \(http200Status.1)"). // 0k

print("The status code is \(http200Status.Code)") // it prints 200
print("The status code is \(http200Status.Code)") // it prints 200 always why not value Ok

我的问题:为什么它总是取 Code 的第一个值?不是第二?

enter image description here

最佳答案

接受同名元组标签是一个错误 (SR-8974)。它已在 Swift 5.1 中修复。来自CHANGELOG :

Duplicate tuple element labels are no longer allowed, because it leads to incorrect behavior.

Xcode 11(测试版 6)正确地提示:

let http200Status = (Code: 200, Code: "OK")
// Error: Cannot create a tuple with a duplicate element label

关于swift - 具有单个元素的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57685408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com