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.

Add Comment