Ios Swift 5中作为字典值的Swift元组

Ios Swift 5中作为字典值的Swift元组,ios,swift,swift5,Ios,Swift,Swift5,我有一个字典,其键类型为String,值为元组: var dictionaryTuple = [String: (x: Double, y: Double)]() 如何设置和访问元组的值。这就是我试过的 dictionaryTuple[“One”].x=5.5-编译器给出错误:可选类型的值“(x:Double,y:Double)?“必须展开以引用已包装基类型的成员“x”(x:Double,y:Double)”请参见下面的屏幕截图 不确定编译器为什么给出错误。我没有将元组声明为可选,也没有将字典

我有一个字典,其键类型为String,值为元组:

var dictionaryTuple = [String: (x: Double, y: Double)]()
如何设置和访问元组的值。这就是我试过的

dictionaryTuple[“One”].x=5.5
-编译器给出错误:可选类型的值“(x:Double,y:Double)?“必须展开以引用已包装基类型的成员“x”(x:Double,y:Double)”请参见下面的屏幕截图 不确定编译器为什么给出错误。我没有将元组声明为可选,也没有将字典声明为可选

当我改为:
dictionaryTuple[“一”]?.x=5.5
-编译器很高兴,但我没有得到任何回报

如果我将其更改为:
dictionaryTuple[“一”]!。x=5.5
-它崩溃了。


我做错了什么?我需要在使用元组之前初始化它吗?如果是,怎么做


非常感谢

为什么不这样构造结构

struct DT {
    let key: String
    let x: Double
    let y: Double
}

let dt = DT(key: "One", x: 1, y: 2)

如果你真的想要自己的方式,就像你的问题一样:

struct DT {
    var dictionaryTuple = [String: (x: Double, y: Double)]()
}


var dt = DT(dictionaryTuple: ["One": (x: 1, y: 2)])

print(dt)//prints -> DT(dictionaryTuple: ["One": (x: 1.0, y: 2.0)])

var tuple: (x: Double, y: Double) = dt.dictionaryTuple["One"]!
tuple.x = 100
tuple.y = 200

//apply
dt.dictionaryTuple["One"] = tuple

print(dt)//prints -> DT(dictionaryTuple: ["One": (x: 100.0, y: 200.0)])

这里的关键是我显式地声明了
tuple
变量的类型,并将其强制转换。

为什么不这样构造您的结构

struct DT {
    let key: String
    let x: Double
    let y: Double
}

let dt = DT(key: "One", x: 1, y: 2)

如果你真的想要自己的方式,就像你的问题一样:

struct DT {
    var dictionaryTuple = [String: (x: Double, y: Double)]()
}


var dt = DT(dictionaryTuple: ["One": (x: 1, y: 2)])

print(dt)//prints -> DT(dictionaryTuple: ["One": (x: 1.0, y: 2.0)])

var tuple: (x: Double, y: Double) = dt.dictionaryTuple["One"]!
tuple.x = 100
tuple.y = 200

//apply
dt.dictionaryTuple["One"] = tuple

print(dt)//prints -> DT(dictionaryTuple: ["One": (x: 100.0, y: 200.0)])

这里的关键是我显式地声明了
tuple
变量的类型,并将其强制转换。

您可以像这样插入一个tuple

dictionaryTuple["One"] = (3.3, 4.4)
或者更新一个现有的,但您需要将其视为可选的

dictionaryTuple["Two"]?.x = 5.5
或在更新时提供默认值

dictionaryTuple["Three", default: (0.0, 0.0)].x = 5.5
执行这3个操作意味着字典包含2个元组

var dictionaryTuple = [String: (x: Double, y: Double)]()
dictionaryTuple["One"] = (3.3, 4.4)
dictionaryTuple["Two"]?.x = 5.5
dictionaryTuple["Three", default: (0.0, 0.0)].x = 5.5
print(dictionaryTuple)
[“三个”:(x:5.5,y:0.0),“一个”:(x:3.3,y:4.4)]


您可以像这样插入一个元组

dictionaryTuple["One"] = (3.3, 4.4)
或者更新一个现有的,但您需要将其视为可选的

dictionaryTuple["Two"]?.x = 5.5
或在更新时提供默认值

dictionaryTuple["Three", default: (0.0, 0.0)].x = 5.5
执行这3个操作意味着字典包含2个元组

var dictionaryTuple = [String: (x: Double, y: Double)]()
dictionaryTuple["One"] = (3.3, 4.4)
dictionaryTuple["Two"]?.x = 5.5
dictionaryTuple["Three", default: (0.0, 0.0)].x = 5.5
print(dictionaryTuple)
[“三个”:(x:5.5,y:0.0),“一个”:(x:3.3,y:4.4)]


我认为您应该使用
CGPoint
而不是元组,或者使用x和y属性创建
Point
结构。“我做错了什么?”。您正在使用可选的链接。这意味着忽略代码的右侧,以防该键没有值。“我需要在使用元组之前初始化它吗?”是的。“如果是,怎么做?”您需要使用默认值的基于字典键的下标,并为元组提供初始值,以防它不存在。谢谢您的评论。同意,如果我要用CGPoint来表示x,y点,我会用CGPoint。我刚刚创建了这个用于发布问题。我用它来创建两种颜色的元组!您可能也不需要将元组作为值的字典。如果您发布了一个带有实际目标的问题,我们可能会向您展示一个更好的解决方案。我认为您应该使用
CGPoint
而不是元组,或者创建一个带有x和y属性的
Point
结构。“我做错了什么?”。您正在使用可选的链接。这意味着忽略代码的右侧,以防该键没有值。“我需要在使用元组之前初始化它吗?”是的。“如果是,怎么做?”您需要使用默认值的基于字典键的下标,并为元组提供初始值,以防它不存在。谢谢您的评论。同意,如果我要用CGPoint来表示x,y点,我会用CGPoint。我刚刚创建了这个用于发布问题。我用它来创建两种颜色的元组!您可能也不需要将元组作为值的字典。如果你提出了一个问题,并提出了你的实际目标,我们可能会给你一个更好的解决方案。