Objective-C: type conversion

Tuesday, June 23, 2009

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

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

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

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

Instead, here's how to convert a string to integer:

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

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

There are no comments for this post

Leave a comment

TAG XHTML PERMISSIONS: <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 [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL