返回多个值时,元组与 NSDictionary 看起来一样吗?
Tuples vs NSDictionary when returning multiple value, seem same?
我了解到 tuple
并且提到的一个好处是
- Tuple can contain different values with different datatype, dictionary can contain only on datatype value at a time
ex: let nameAndAge = (name:"Jon", age:10)
但字典也可以 return 多种数据类型(非原始数据)如果我是正确的话。
NSDictionary *dict= @{@"name":@"jon",@"age":[NSNumber numberWithLongLong: age],@"array":[NSArray new]} ... etc;
这些是在字典中传递的不同数据类型,我是否遗漏了什么?谢谢。
Swift
中的每个变量都有特定的类型,可以是 tuple
或 dictionary
。
在下面tuple
:
let nameAndAge = (name:"Jon", age:10)
nameAndAge
是类型 (String, Int)
在 dictionary
中,type
由它包含的值引用,
let dict = ["One": 1, 1: "One"]
dict
的类型是 [AnyHashable:Any]
在
NSDictionary *dict= @{@"name":@"jon",@"age":[NSNumber numberWithLongLong: age],@"array":[NSArray new]} ... etc;
尽管它在 Objective-C
中,但在 Swift
中它将具有 [String:Any]
的类型
即let dict = ["name":"jon", "age":10, "array":[String]]
一个dictionary
键值对类型是满足它包含的所有元素的类型,即最顶层的超类。
在Swift中:
元组
Tuples group multiple values into a single compound value. The values within a tuple can be of any type and don’t have to be of the same type as each other.
词典
A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary.
此外,对于字典,如果你想有多个类型,你可以用 Any
类型声明它。
例如:
var dictionary = Dictionary<String, Any>()
更多信息:
我了解到 tuple
并且提到的一个好处是
- Tuple can contain different values with different datatype, dictionary can contain only on datatype value at a time
ex: let nameAndAge = (name:"Jon", age:10)
但字典也可以 return 多种数据类型(非原始数据)如果我是正确的话。
NSDictionary *dict= @{@"name":@"jon",@"age":[NSNumber numberWithLongLong: age],@"array":[NSArray new]} ... etc;
这些是在字典中传递的不同数据类型,我是否遗漏了什么?谢谢。
Swift
中的每个变量都有特定的类型,可以是 tuple
或 dictionary
。
在下面tuple
:
let nameAndAge = (name:"Jon", age:10)
nameAndAge
是类型 (String, Int)
在 dictionary
中,type
由它包含的值引用,
let dict = ["One": 1, 1: "One"]
dict
的类型是 [AnyHashable:Any]
在
NSDictionary *dict= @{@"name":@"jon",@"age":[NSNumber numberWithLongLong: age],@"array":[NSArray new]} ... etc;
尽管它在 Objective-C
中,但在 Swift
中它将具有 [String:Any]
即let dict = ["name":"jon", "age":10, "array":[String]]
一个dictionary
键值对类型是满足它包含的所有元素的类型,即最顶层的超类。
在Swift中:
元组
Tuples group multiple values into a single compound value. The values within a tuple can be of any type and don’t have to be of the same type as each other.
词典
A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary.
此外,对于字典,如果你想有多个类型,你可以用 Any
类型声明它。
例如:
var dictionary = Dictionary<String, Any>()
更多信息: