开发者

Convert NSNumbers into NSStrings

In the code below, I have a dictionary of NSNumbers that are turned into an Array. It is then randomly sorted and the first four rows need to be displayed in the appropriate labels. NSLog does show that the array displays the appropriate numbers. I believe the code fails because I need to convert the NSNumbers into NSStrings. What is the applicable code or what am I overlooking?

-(IBAction)shakeit {
NSDictionary * myExampleDict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dictionaryKey"];
exampleArray = [myExampleDict allValues];
NSUI开发者_运维问答nteger count = [exampleArray count];
for (NSUInteger i = 0; i < count; ++i) {
   int nElements = count - i;
   int n = (arc4random() % nElements) + i;
   [exampleArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
NSString *one = [exampleArray objectAtIndex:1];
NSString *two = [exampleArray objectAtIndex:2];
NSString *three = [exampleArray objectAtIndex:3];
NSString *four = [exampleArray objectAtIndex:4];
select1.text = one;
select2.text = two;
select3.text = three;
select4.text = four;
}


I believe the code fails because I need to convert the NSNumbers into NSStrings.

you are right, try the following:

NSString *one   = [[exampleArray objectAtIndex:1] stringValue];
NSString *two   = [[exampleArray objectAtIndex:2] stringValue];
NSString *three = [[exampleArray objectAtIndex:3] stringValue];
NSString *four  = [[exampleArray objectAtIndex:4] stringValue];


You are right, you need to convert it to NSString.

You simply need to call stringValue, a method of NSNumber. Like :

select1.text = [one stringValue];


I copied answer from this link How to convert NSNumber to NSString

NSString *myString = [NSNumber stringValue];


blank = [NSString stringWithFormat:@"%i",[nsnumber_var integerValue]];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜