初始化器
init(_:
init(_:radix:)
从给定的字符串和基数创建一个新的整数值。
声明
init?<S>(
_ text: S,
radix: Int = 10
) where S : StringProtocol
参数
text以
radix形式传递的基数中数字的 ASCII 表示。radix用于将
text转换为整数值的基数或基数。radix必须在2...36范围内。默认值为 10。
详述
作为text 传递的字符串可以以加号或减号字符(+ 或-)开头,后跟一个或多个数字(0-9)或字母(a-z 或A-Z)。字符串的解析不区分大小写。
let x = Int("123")
// x == 123
let y = Int("-123", radix: 8)
// y == -83
let y = Int("+123", radix: 8)
// y == +83
let z = Int("07b", radix: 16)
// z == 123
如果 text 格式无效或包含超出给定 radix 范围的字符,或者如果它在给定 radix 中表示的值不可表示,则结果为 nil 。例如,以下转换导致 nil :
Int(" 100") // Includes whitespace
Int("21-50") // Invalid format
Int("ff6600") // Characters out of bounds
Int("zzzzzzzzzzzzz", radix: 36) // Out of range
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift UInt16 init(_:)用法及代码示例
- Swift UInt16 init(clamping:)用法及代码示例
- Swift UInt16 init(integerLiteral:)用法及代码示例
- Swift UInt16 init(exactly:)用法及代码示例
- Swift UInt16 init(truncatingIfNeeded:)用法及代码示例
- Swift UInt16 &*=(_:_:)用法及代码示例
- Swift UInt16 leadingZeroBitCount用法及代码示例
- Swift UInt16 %=(_:_:)用法及代码示例
- Swift UInt16 ...(_:)用法及代码示例
- Swift UInt16 &<<(_:_:)用法及代码示例
- Swift UInt16 -(_:_:)用法及代码示例
- Swift UInt16 <<=(_:_:)用法及代码示例
- Swift UInt16 &-(_:_:)用法及代码示例
- Swift UInt16 random(in:)用法及代码示例
- Swift UInt16 ...(_:_:)用法及代码示例
- Swift UInt16 quotientAndRemainder(dividingBy:)用法及代码示例
- Swift UInt16 /(_:_:)用法及代码示例
- Swift UInt16 *(_:_:)用法及代码示例
- Swift UInt16 &+(_:_:)用法及代码示例
- Swift UInt16 ^(_:_:)用法及代码示例
- Swift UInt16 ..<(_:_:)用法及代码示例
- Swift UInt16 ^=(_:_:)用法及代码示例
- Swift UInt16 &<<=(_:_:)用法及代码示例
- Swift UInt16 %(_:_:)用法及代码示例
- Swift UInt16 &>>(_:_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 UInt16 init(_:radix:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
