我试着看看用于Int的Swift API,我仍然不确定为什么这样做:
var foo = Int("100")
Run Code Online (Sandbox Code Playgroud)
我在文档中看到以下初始化器:
init()
init(_: Builtin.Word)
init(_: Double)
init(_: Float)
init(_: Int)
init(_: Int16)
init(_: Int32)
init(_: Int64)
init(_: Int8)
init(_: UInt)
init(_: UInt16)
init(_: UInt32)
init(_: UInt64)
init(_: UInt8)
init(_:radix:)
init(_builtinIntegerLiteral:)
init(bigEndian:)
init(bitPattern:)
init(integerLiteral:)
init(littleEndian:)
init(truncatingBitPattern: Int64)
init(truncatingBitPattern: UInt64)
Run Code Online (Sandbox Code Playgroud)
但我没有看到init(_: String)上面的内容.引擎盖下是否有一些自动化?
有一个
extension Int {
/// Construct from an ASCII representation in the given `radix`.
///
/// If `text` does not match the regular expression
/// "[+-][0-9a-zA-Z]+", or the value it denotes in the given `radix`
/// is not representable, the result is `nil`.
public init?(_ text: String, radix: Int = default)
}
Run Code Online (Sandbox Code Playgroud)
采用字符串和可选基数的扩展方法(默认为10):
var foo = Int("100") // Optional(100)
var bar = Int("100", radix: 2) // Optional(4)
var baz = Int("44", radix: 3) // nil
Run Code Online (Sandbox Code Playgroud)
怎么会找到那个?对于没有外部参数名称的方法,使用"跳转到定义"中的"技巧" ,编写等效代码
var foo = Int.init("100")
// ^^^^
Run Code Online (Sandbox Code Playgroud)
然后在Xcode中cmd点击init:)
| 归档时间: |
|
| 查看次数: |
1607 次 |
| 最近记录: |