quarterpi
quarterpi

Reputation: 863

No intValue on Swift 3 String?

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

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236568

You can extend String to add such property as follow:

extension String { 
    var intValue: Int { 
        return Int(self) ?? 0 
    }
}

Upvotes: 1

Related Questions