Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用swift在字典中迭代_Swift_Dictionary_Optional - Fatal编程技术网

用swift在字典中迭代

用swift在字典中迭代,swift,dictionary,optional,Swift,Dictionary,Optional,我正在学习swift,并试图在字典中重复。你能告诉我为什么变量l最后是零吗 let LandsDictionary = ["DE":"Germany", "FR":"France"] var l:String? for land in LandsDictionary{ l?+=land } print (l) 用于迭代字典 for (key , value) in LandsDictionary{ print(key)

我正在学习swift,并试图在字典中重复。你能告诉我为什么变量l最后是零吗

let LandsDictionary = ["DE":"Germany", "FR":"France"]

var l:String?

for land in LandsDictionary{
    l?+=land
}
print (l)

用于迭代字典

 for (key , value) in LandsDictionary{
                print(key)
                print(value)
            }

用于迭代字典

 for (key , value) in LandsDictionary{
                print(key)
                print(value)
            }

根据您的评论,我假设您正在尝试读取变量“l”的国家名称

请尝试使用此代码段

let LandsDictionary = ["DE":"Germany", "FR":"France"]

var l:String?
//You need to assign an initial value to l before you start appending country names.
//If you don't assign an initial value, the value of variable l will be nil as it is an optional.
//If it is nil, l? += value which will be executed as optional chaining will not work because optional chaining will stop whenever nil is encountered.
l = ""

for (key, value) in LandsDictionary{
    l? += value
}
print (l)

希望这能有所帮助。

根据您的评论,我假设您正在尝试读取变量“l”的国家名称

请尝试使用此代码段

let LandsDictionary = ["DE":"Germany", "FR":"France"]

var l:String?
//You need to assign an initial value to l before you start appending country names.
//If you don't assign an initial value, the value of variable l will be nil as it is an optional.
//If it is nil, l? += value which will be executed as optional chaining will not work because optional chaining will stop whenever nil is encountered.
l = ""

for (key, value) in LandsDictionary{
    l? += value
}
print (l)

希望这能有所帮助。

这是一个示例,说明如何通过两种不同的方式获取键和值。试着多读一点关于收藏的内容

let LandsDictionary = ["DE":"Germany", "FR":"France"]

var keys :String = ""

var values :String = ""

//Iteration is going on properly and fetching key value.
for land in LandsDictionary {

    print (land) // "DE":"Germany" and "FR":"France"

    keys += land.0

    values +=  land.1
}

//All keys
print(keys)

//All values
print(values)

//If you would like to recive all values and all keys use standart method of the collection.

let allKeys = LandsDictionary.keys

let allValues = LandsDictionary.values

这是一个如何以两种不同方式获取键和值的示例。试着多读一点关于收藏的内容

let LandsDictionary = ["DE":"Germany", "FR":"France"]

var keys :String = ""

var values :String = ""

//Iteration is going on properly and fetching key value.
for land in LandsDictionary {

    print (land) // "DE":"Germany" and "FR":"France"

    keys += land.0

    values +=  land.1
}

//All keys
print(keys)

//All values
print(values)

//If you would like to recive all values and all keys use standart method of the collection.

let allKeys = LandsDictionary.keys

let allValues = LandsDictionary.values

由于此字典中的所有键和值都是非可选的,因此不需要使用可选变量

let landsDictionary = ["DE":"Germany", "FR":"France"]

var l = ""

// the underscore represents the unused key
for (_, land) in landsDictionary {
  l += land
}
print (l) // "GermanyFrance"
或者没有环

let v = Array(landsDictionary.values).joinWithSeparator("")
print (v) // "GermanyFrance"

由于此字典中的所有键和值都是非可选的,因此不需要使用可选变量

let landsDictionary = ["DE":"Germany", "FR":"France"]

var l = ""

// the underscore represents the unused key
for (_, land) in landsDictionary {
  l += land
}
print (l) // "GermanyFrance"
或者没有环

let v = Array(landsDictionary.values).joinWithSeparator("")
print (v) // "GermanyFrance"

你能执行这个代码吗?因为编译器永远不会允许您在尝试为可选字符串分配元组时执行此操作-l?+=land。另外,请告诉我们您试图读取变量l的哪个值?我正在游乐场中执行它。另外,这对LandsDictionary{l?+=land}中的(键,land)不起作用。您试图实现什么结果?“DEFR”、“GermanyFrance”、“DE:Germany,FR:France”或者其他什么?@vadian字典中键的值可以是可选的。但是,如果您将key的值设置为nil,则相当于从字典中删除该key。您能够执行此代码吗?因为编译器永远不会允许您在尝试为可选字符串分配元组时执行此操作-l?+=land。另外,请告诉我们您试图读取变量l的哪个值?我正在游乐场中执行它。另外,这对LandsDictionary{l?+=land}中的(键,land)不起作用。您试图实现什么结果?“DEFR”、“GermanyFrance”、“DE:Germany,FR:France”或者其他什么?@vadian字典中键的值可以是可选的。但是,如果将key的值设置为nil,则相当于从字典中删除该key。