Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 3.1中使用字符下标?_Swift_Swift3_Subscript - Fatal编程技术网

如何在Swift 3.1中使用字符下标?

如何在Swift 3.1中使用字符下标?,swift,swift3,subscript,Swift,Swift3,Subscript,我是斯威夫特的新手。我发现了一些下标匹配的奇怪行为: struct SubscriptTest{ 下标(字符:字符)->字符串{ 返回“字符:\(字符)” } 下标(string:string)->string{ 返回“字符串:\(字符串)” } 下标(int:int)->字符串{ 返回“Int:\(Int)” } } let test=SubscriptTest() test[“abc”]//字符串:abc test[13]//Int:13 测试[“”那么,Swift中是否有类似类型优先级的内

我是斯威夫特的新手。我发现了一些下标匹配的奇怪行为:

struct SubscriptTest{
下标(字符:字符)->字符串{
返回“字符:\(字符)”
}
下标(string:string)->string{
返回“字符串:\(字符串)”
}
下标(int:int)->字符串{
返回“Int:\(Int)”
}
}
let test=SubscriptTest()
test[“abc”]//字符串:abc
test[13]//Int:13

测试[“
”那么,Swift中是否有类似类型优先级的内容呢?@RoboRobok:有一种默认的文本类型。
123
可以是
Int
Float
Double
UInt64
或….如果从上下文中无法推断出任何其他内容,则将其视为
Int
。我越来越喜欢Swift:)只是为了好玩(在生产代码中不要这样做),可以定义
typealias UnicodeScalarType=Character;typealias ExtendedGraphimeClusterType=Character
,将unicode标量和字符的默认文字类型从
String
更改为
Character
:D,然后
测试[“上述方法之所以有效,是因为当没有其他上下文来推断文本的类型时,编译器会搜索范围内具有给定名称的默认类型。标准库为这些类型定义了类型,但我们可以覆盖它们。每个ExpressibleBy*文本协议的默认名称如下所示: