Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialShaun Kelly
5,648 PointsFormat Arguments?
I have an NSInteger and when I print out the value there is an error that says. Values of type 'NSInteger' should be used as format arguments; add an explicit cast to 'long' instead.
My "number" variable is only a small number of 10, why do I need to change it to a long? I thought a long was just another data type but allocates more memory because it a bigger number ? And another question, what is a format argument ?
NSInteger number = 10;
NSLog(@"%d", number);
2 Answers
Damien Watson
27,419 PointsHey Shaun,
On OSX 64bit an NSInteger is defined as a long, so when you try to format into a string, you need to use the %ld as below. I have been coming across this a lot myself lately and still trying to figure it out.
NSInteger number = 10;
NSLog(@"%ld", (long)number);
Format arguments are I believe the '%?' inside a string which controls the format of the element. eg.
int abc = 10;
NSString *def = @"Hey";
CGFloat myFloat = 1.23;
NSLog(@"%d, %@, %0.2f", abc, def, myFloat);
Brenden Konnagan
16,858 Pointsyou are correct... you have to use the correct string format specifier and cast it to a long since NSInteger is a long.
Here is Apple's Documentation:Documentation
Shaun Kelly
5,648 Pointsbut the value of the NSInteger is 10 in this case. 'long' is only for big number though ? confused
Brenden Konnagan
16,858 PointsThe value you set it to isn't what makes it a "long" but the type. Much like Double can be assigned 10 but it is still a double.
Suppose:
var numInt: Int = 10
// num is an Int
var numDouble: Double = 10.0
// numDouble is a Double
Both values are "10" but what makes them different is the type it was defined as.
Given the above example your correct that the value is "10" but it is of type NSInteger which the computer knows is a long.
Damien Watson
27,419 PointsDamien Watson
27,419 PointsFor your code example above, you can use the three ` followed by 'objc' to colorise.