0

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];
NSUInteger 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;
}
StreaminJA
  • 53
  • 1
  • 1
  • 9
  • possible duplicate of [Convert NSNumber to NSString and use it in other View](http://stackoverflow.com/questions/3969972/convert-nsnumber-to-nsstring-and-use-it-in-other-view) – DarthJDG Jun 10 '11 at 08:20

4 Answers4

4

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];
Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
1

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];
gcamp
  • 14,622
  • 4
  • 54
  • 85
1

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

NSString *myString = [NSNumber stringValue];
Community
  • 1
  • 1
Chanok
  • 790
  • 6
  • 23
0
blank = [NSString stringWithFormat:@"%i",[nsnumber_var integerValue]];
DarthJDG
  • 16,511
  • 11
  • 49
  • 56
Jack
  • 1
  • 1