Reputation: 2163
I have a question regarding NSInteger
to NSString
i keep trying but with no luck.
however for some strange reason one of my NSInteger
does but i dont know why
i have this method:
//Lets say loc_id = 869
-(void)addResource:(NSInteger)loc_id
{
//[loc.chosenLocations addObject:[NSString stringWithFormat@"%@",loc_id]];
NSLog([NSString stringWithFormat:@"addResource: loc_id:%@",loc_id]);
}
Results BAD
Because its giving me: addResource: loc_id:20987787696 in the debugger of xcode
Its ment to be loc_id:860
when i try this with loc_id = 8
it works pretty well why is this strange behaviour
or what did i mis understood thanks for the tips and help.
when i use %@
in string format it works for loc_id = 8
but when i try to use
%d
wich is normal useage it wil also result in loc_id:20987787696
how come ?
Useage of method:
-(IBAction)switchChanged:(id)sender {
UISwitch *switchControl = sender;
[loc addResource:switchControl.tag]
}
-(void)addSwitch:(NSInteger)aid
{
UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(x,y,w,h)]];
sw.tag = aid;
[sw addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEvenValueChanged]];
[self.View addSubview:sw];
}
//Data is dynamic (NSObject) but for example i will exclude the dynamic data.
for(int i = 0; i < 20; i++)
{
NSInteger *loc_id = 868;
[self addSwitch:loc_id];
}
at a point i want to insert it into the NSMutableArray as commented out above but i expect a value of 869 and it giefs me 2071894069 i want the value of the int not the memory thingy
Kind Regards, Stefan
Upvotes: 0
Views: 345
Reputation: 7410
You should use %li
or @ld
instead of %@
, and drop the stringWithFormat:
call in the NSLog
(it already expects a format):
NSLog(@"addResource: loc_id:%li",loc_id);
%@
is the selector to format objects descriptions and strings. %li
is to cast your integer to a long, and prevent an eventual compiler warning.
Edit :
After quite a few questions in the comment, here is another wrong thing in your code :
NSInteger *loc_id = 868;
You should not initiate loc_id
as a pointer. Use instead :
NSInteger loc_id = 868;
Edit #2 :
As discussed in the chat, the issue came from the way the NSInteger value was extracted from the array / dictionary in which it was held.
Upvotes: 3
Reputation: 422
To display your NSInteger, you don't need to format it in NSString. Just use this code below.
NSLog(@" %ld",(long)loc_id);
Upvotes: 1
Reputation: 14780
Apple Documentation
The utility functions
NSLog()
andNSLogv()
use theNSString
string formatting services to log error messages. Note that as a consequence of this, you should take care when specifying the argument for these functions. A common mistake is to specify a string that includes formatting characters, as shown in the following example.
You don't need to add formatting strings inside NSLog:
NSLog([NSString stringWithFormat:@"addResource: loc_id:%@",loc_id]);
Also using %li
on 64 Bit gives integer precision as @rdurand asnwer
NSLog(@"addResource: loc_id:%li",loc_id);
Upvotes: 0
Reputation: 481
NSLog([NSString stringWithFormat:@"addResource: loc_id:%ld",(long)loc_id]);
Gets a warning: Format string is not a string literal.
The following code works as ordered:
-(void)addResource:(NSInteger)loc_id
{
//[loc.chosenLocations addObject:[NSString stringWithFormat@"%@",loc_id]];
NSString *string = [NSString stringWithFormat:@"addResource: loc_id:%ld",(long)loc_id];
NSLog(@"%@", string);
}
Upvotes: 0
Reputation: 1388
You also can cast your Integer
to a long
value:
-(void)addResource:(NSInteger)loc_id
{
NSLog(@"addResource: loc_id:%ld",(long)loc_id);
}
Upvotes: 0
Reputation: 15512
You need to use %ld or %li for integers. %@ is used for objects.
NSLog([NSString stringWithFormat:@"addResource:loc_id:%ld",(long)loc_id]);
Upvotes: 0