Reputation: 863
Does anybody know why you have to cast to NSString
in order to get an intValue
in Swift 3? Why doesn't the Swift String
type have an intValue
? I'm sure there's a reason, but I haven't been able to find an answer. Does it have anything to do with hardware architecture and Swift running on multiple platforms? Any insight would be appreciated.
Upvotes: 2
Views: 437
Reputation: 236568
You can extend String to add such property as follow:
extension String {
var intValue: Int {
return Int(self) ?? 0
}
}
Upvotes: 1