iOS

How to Convert Between Datatypes Types in Xcode

Datatypes in objective-c are easy to change between. But often you find yourself looking all over the web, on forums for the answers.

Converting between types is a big problem if done wrong. If you convert the variable wrong your app will crash. The app may not crash until another point in your app. Such as when another function trys to read the variable. This guide should help minimise these problems and save you time.

This guide also features how to print each variable to the console. If you put the wrong type in the NSLog, the app will just print null or crash.

NSInteger

To NSNumber
[NSNumber numberWithInteger:integer];

To NSString
[NSString stringWithFormat:@"%d", integer];

NSString

To NSInteger
[string intValue];

To NSNumber
[NSNumber numberWithInt:[string intValue]];

NSNumber

To NSString
[NSString stringWithFormat:@"%@", number];

To NSInteger
[number integerValue];

NSData

To NSString
[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

To NSSInteger
[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
[string intValue];

To NSNumber
[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
[NSNumber numberWithInt:[string intValue]];

NSLog console commands to print types

Display NSString
NSLog(@"%@", string);

Display NSInteger
NSLog(@"%d", int);

Display NSFloat
NSLog(@"%f", float);

If you have any other requests for types which aren’t here please comment.

Before you go

After spending over 20 years working with Macs, both old and new, theres a tool I think would be useful to every Mac owner who is experiencing performance issues.

CleanMyMac is highest rated all-round cleaning app for the Mac, it can quickly diagnose and solve a whole plethora of common (but sometimes tedious to fix) issues at the click of a button. It also just happens to resolve many of the issues covered in the speed up section of this site, so Download CleanMyMac to get your Mac back up to speed today.

mac-pc

About the author

Chris

I've been a passionate evangelist for Apple and the Macintosh throughout my working life, my first love was a Quadra 605 working with a small creative agency in the south of Norfolk UK in the mid 1990's, I later progressed to other roles in other Macintosh dominated industries, first as a Senior graphic designer at a small printing company and then a production manager at Guardian Media Group. As the publishing and printing sector wained I moved into Internet Marketing and in 2006 co-founded blurtit.com which grew to become one the top 200 visited sites in the US (according to Quantcast), at its peak receiving over 15 million visits per month. For the last ten years I have worked as an Affiliate and Consultant to many different business and start ups, my key skill set being online marketing, on page monetisation, landing page optimisation and traffic generation, if you would like to hire me or discuss your current project please reach out to me here.

You can also follow me on: and

Add Comment

Click here to post a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.