简体   繁体   中英

Get specific value from NSMutableArray

I'm parsing an XML and saving result to NSMutableArray. When I do NSLog,

NSLog(@"Data : %@",_data);

I'm getting

Data : (
        {
        SessionToken = 9e72dd029e0e8268380b919356881935;
    }
)

I only want 9e72dd029e0e8268380b919356881935 from the array. What is the best solution to achieve this?

EDIT : There will be only one SessionToken at a time.

You can try this code :

for (NSDictionary *data1 in _data) {
    NSlog("session token %@",[data1 objectForKey:@"SessionToken"]);//Other wise add into another array which contain session token.. only..
}

Since there will be only one session at a time.

NSDictionary *session = [_data lastObject];
NSString *sessionToken = session[@"SessionToken"];

OR with literals

NSString *sessionToken = _data[0][@"SessionToken"];
if ([_data count]) {
    NSDictionary *dic = [_data objectAtIndex:0];
    NSLog(@"Data : %@",[dic objectForKey:@"SessionToken"]);
}
for (NSDictionary *data1 in _data) {
    NSlog("session token %@",[data1 valueForKey:@"SessionToken"]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM