Articles Tagged 'casting'

Objective-C: type conversion

With some high-level languages ​​such as JavaScript or PHP, where the data are not typed or otherwise can not be, we are "spoiled" in compare or convert strings and integers and vice versa, all hidden or manipulated by the interpreter (or compiler ). For example, in JavaScript functions are "forced" (like parseInt() for example) required in special cases. However, in other circumstances we treat without worrying about a particular integer conversions (explicit), such as:

1
2
3
5 ; mioNumero var = 5;
/ / ...
"Il valore di mioNumero è " + mioNumero ) ; alert ("The value of mioNumero is" + mioNumero);

In Objective-C, however, the data type is important and the type conversion must be explicit. Besides the casting (better typecasting) we can use the features and functionality specific to the type conversion. For example, here is how to convert an integer to a string:

1
2
3
4
5 ; mioNumero int = 5;
miaStringa = [ NSString stringWithFormat : @ "%d" , mioNumero ] ; NSString * myString = [ NSString stringWithFormat: @ "% d", mioNumero];
/ /
"miaStringa=%@ mioNumero=%i" , miaStringa, mioNumero ) ; NSLog (@ "myString = @ mioNumero% =% i", myString, mioNumero);

In contrast, here is how to convert a string to an integer:

1
2
3
4
miaStringa = @ "128" ; NSString * myString = @ "128";
[ miaStringa integerValue ] ; mioNumero int = [myString integerValue];
/ /
"miaStringa=%@ mioNumero=%i" , miaStringa, mioNumero ) ; NSLog (@ "myString = @ mioNumero% =% i", myString, mioNumero);

Continued ...