2010-02-26 5 views
0

code:Comment puis-je obtenir la valeur chaîne de NSMutableArray

[self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys: 
             NSLocalizedString(@"PropertySubtype2fTitle", @""), 
             kTitleKey, 
             publishNoOfRoomsViewController, 
             kViewControllerKey, nil]]; 

MenuList est un NSMutableArray.

Je veux lire la chaîne localisée PropertySubtype2fTitle plus tard dans le code, comme dans:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

Répondre

8

Pour récupérer une entrée d'un NSDictionary, vous pouvez utiliser

[NSDictionary objectForKey:(id)keyValue]
. Pour récupérer une entrée d'un NSArray/NSMutableArray, vous pouvez utiliser
[NSArray objectAtIndex:(NSUInteger)index]
. En combinaison et appliqué à votre exemple, cela devrait être:
NSString *title = [[menuList objectAtIndex:index] objectForKey:kTitleKey];
tandis que l'index serait un entier non signé (NSUInteger).

0

Pourquoi ne pas utiliser directement le NSMutableDictionary?

Par exemple:

NSData *getData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]]; 
NSError *errorData; 
NSMutableDictionary *dicData = [NSJSONSerialization 
            JSONObjectWithData:getData 
            options:NSJSONReadingMutableContainers 
            error:&errorData]; 

if(errorData) 
{ 
    NSLog(@"%@", [errorData localizedDescription]); 
} 
else { 

    NSString *title = [[dicData[@"items"] objectAtIndex:1] objectForKey:@"titlekey"]; 
    NSLog(@"titlekey %@",title); 
}