2011-10-31 112 views
0
int number = 3; 

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: 
           @"Apple",[NSNumber numberWithInt:0], 
           @"Peach",[NSNumber numberWithInt:1], 
           @"Lemon",[NSNumber numberWithInt:2], 
           @"Pineapple",[NSNumber numberWithInt:3], 
           ........................, 
           nil]; 

NSString *numberToKey = [dict objectForKey:number]; 

如何在字符串中插入基於int數的字典值?基於int數的密鑰對象

回答

3

你把值到字典用同樣的方法:

NSString *numberToKey = [dict objectForKey:[NSNumber numberWithInt:number]]; 
+0

給予好評。你不能在需要指針類型對象的地方傳入int,因爲它會認爲int是指針本身;使用[NSNumber]是要走的路。 – Tim