KaiserJohaan
KaiserJohaan

Reputation: 9240

iOS - NSString to uint

I've got a quick question; I want to convert the text in a textfield to an unsigned integer, and here's what I do:

uint nmbr = [textfield.text intValue];

will objective-c implicitly cast and interpret nmbr as an unsigned int or is there another way to do this?

Upvotes: 0

Views: 3479

Answers (2)

Vlad
Vlad

Reputation: 3366

int atoi ( const char * str );

Convert string to integer Parses the C string str interpreting its content as an integral number, which is returned as an int value.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

Parameters

str C string beginning with the representation of an integral number.

Return Value On success, the function returns the converted integral number as an int value. If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned.

Upvotes: 2

Jörg Jacobsen
Jörg Jacobsen

Reputation: 110

Yes it will implicitly cast the return value to an unsigned int. Just be aware that you will get an unexpected unsigned value if your text field contains a negative number (e.g. -1 will give you 4294967295).

Upvotes: 3

Related Questions