Objective-C: type conversion

With some high-level languages ​​such as JavaScript or PHP, where 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 the integers without worry of particular conversions (explicit), such as:

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

In Objective-C, instead, the type given is important and the type conversion must be express. 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 =% s", 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 ] ; int mioNumero = [myString integerValue];
/ /
"miaStringa=%@ mioNumero=%i" , miaStringa, mioNumero ) ; NSLog (@ "% @ myString = mioNumero =% s", myString, mioNumero);

There are no comments for this post

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA