In Swift, Does Int Have a Hidden Initializer That Takes a String

In Swift, does Int have a hidden initializer that takes a String?

There is a

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)
}

extension method taking a string and an optional radix (which is 10 by default):

var foo = Int("100") // Optional(100)
var bar = Int("100", radix: 2) // Optional(4)
var baz = Int("44", radix: 3) // nil

How could one find that? Using the "trick" from
"Jump to definition" for methods without external parameter names, write the equivalent
code

var foo = Int.init("100")
// ^^^^

and then cmd-click on init in Xcode :)

Can't find the Int type initialiser that takes a string as argument in Apple's Swift official documentation

If you click on init(_:radix:) to expand the declaration then you'll see

Construct from an ASCII representation in the given radix.

Declaration

init?(_ text: String, radix radix: Int = default)

The first parameter is a string (and has an empty external parameter
name). The second parameter "radix" has a default value,
therefore it can be omitted when calling the function:

let number = Int("123")

but you can specify the radix to create a number from a string
representation in another base:

let numberFromHexString = Int("100", radix: 16) // Optional(256)
let numberFomOctalString = Int("077", radix: 8) // Optional(63)

There is also a "trick" which I learned at
"Jump to definition" for methods without external parameter names: If you write

let number = Int("123")

as

let number = Int.init("123")

then you can "command-click" on "init" in Xcode, and you are led
directly to the declaration

public init?(_ text: String, radix: Int = default)

Cannot downcast object of type Any to Int when accessing from dictionary


I must first downcast Any to String, then convert that string to Int. Is this a bug or rather a feature of Swift that I don't understand?

It's neither a bug nor a feature of Swift. It's a fact about the dictionary you're working with. This thing is a String, not an Int. So you cannot cast it to an Int.

Swift Form Validation - Check if Int or String has been entered

If the number the user has entered is not an integer, convertedNumber will be nil. Just add an else clause in which you can show the alert.

Jump to definition for methods without external parameter names

You have to do some work:

let c = Array.init("abc".characters)
// ^^^^^

Use initializer expression, then cmd + click on it.

User expected to input integer into Text Field

If the initializer for the Goal class needs the values as Int, you need to explicitly cast it from String to Int for it to work.

let integerValue = Int(stringValue)

Note that this generates an optional value, and thus, has to be unwrapped if your Goal init doesn't accept optionals.



Related Topics



Leave a reply



Submit