N0xus
N0xus

Reputation: 2724

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?

Upvotes: 4

Views: 12866

Answers (4)

Volker
Volker

Reputation: 4660

If you just wont to log it via NSLog you can do

NSLog(@"%@",self.selectedBeacon.distance);

This will call descriptionof self.selectedBeacon.distance This works only for objects, though.

Upvotes: 0

Prince Agrawal
Prince Agrawal

Reputation: 3607

NSNumber is an object. You cannot refer to an object directly..

To get NSNumber value in string format, get its string value like this:

NSString *numberAsString = [myNSNumber stringValue];

Upvotes: 1

trojanfoe
trojanfoe

Reputation: 122381

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

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

Upvotes: 12

Mani
Mani

Reputation: 17585

You can try this..

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

Upvotes: 6

Related Questions