2011-05-31 56 views
0

在下面的代碼中,我有一個NSNumbers字典轉換爲數組。然後隨機排序,前四行需要顯示在適當的標籤中。 NSLog確實顯示數組顯示適當的數字。我相信代碼失敗,因爲我需要將NSNumbers轉換爲NSStrings。什麼是適用的代碼或我忽略了什麼?將NSNumbers轉換爲NSStrings

-(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; 
} 
+0

可能重複其他視圖](http://stackoverflow.com/questions/3969972/convert-nsnumber-to-nsstring-and-use-it-in-other-view) – DarthJDG 2011-06-10 08:20:11

回答

4

我相信,代碼失敗,因爲我 需要將NSNumbers轉換成 NSString的。

你是正確的,請嘗試以下操作:

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

你是對的,你需要將它轉換爲NSString

您只需致電stringValue,方法NSNumber。像:

select1.text = [one stringValue]; 
0
blank = [NSString stringWithFormat:@"%i",[nsnumber_var integerValue]]; 
[轉換的NSNumber到的NSString,並用它在