Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting NSNumber to NSString issue

I know this is an issue that is already been asked a thousand times but of the few posted examples I've tried, I'm stilling running into issues.

Right now I'm trying to display a value to my debug log of an object that holds the data I need as an NSNumber. What I was trying was the following line of code:

NSString *distanceString = [NSString stringWithFormat:@"%d", (NSNumber)self.selectedBeacon.distance ];

However, with the above I get the following error:

Used type 'NSNumber' where arithmetic or pointer type is required

So then I tried the following:

NSString *distanceString = [NSNumber self.selectedBeacon.distance];

But when I went to type self.selectedBeacon.distance the line didn't appear in my intelisense. So for my third attempt I've tried the following line;

NSString *distanceString = [NSString stringWithFormat:@"%d", [NSNumber self.selectedBeacon.distance]];

But the error I get is this;

Expected ']'

Though I can see i have two closing brackets so that error has thrown me. Can anyone please help me on this?

like image 485
N0xus Avatar asked Dec 01 '22 19:12

N0xus


1 Answers

Assuming self.selectedBeacon.distance is an NSNumber, then you can use [NSNumber stringValue] to get a string representation:

NSString *distanceString = [self.selectedBeacon.distance stringValue];
like image 164
trojanfoe Avatar answered Dec 21 '22 10:12

trojanfoe