Sharief
Sharief

Reputation: 1485

How do I get String value out of NSMutableArray

Code:

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

menuList is a NSMutableArray.

I want to read the PropertySubtype2fTitle localized string later in the code, like in:

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

Upvotes: 0

Views: 17114

Answers (2)

Sid
Sid

Reputation: 137

Why not directly use the NSMutableDictionary?

For example:

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);
}

Upvotes: 0

Till
Till

Reputation: 27597

To fetch an entry from a NSDictionary, you can use

[NSDictionary objectForKey:(id)keyValue]
. To fetch an entry from a NSArray / NSMutableArray, you can use
[NSArray objectAtIndex:(NSUInteger)index]
. In combination and applied to your example that should be:
NSString *title = [[menuList objectAtIndex:index] objectForKey:kTitleKey];
whereas index would be an unsigned integer (NSUInteger).

Upvotes: 8

Related Questions