objective-cnsstringnsdictionarytheoslogos

How to split NSString to NSDictionary


I'm getting the APT repository name from the Release file.

Origin: blah blah
Label: This is the name
Suite: stable
Version: 1.0
Codename: ios
Architectures: iphoneos-arm
Components: main
Description: blah blah

Release file looks like this, and I want to get 'This is the name' after the 'Label:'.

So, I want to split this string to NSDictionary. To use it like objectForKey:@"Label" to get This is the name

I get Release file with AFNetworking.

Or is there any good way to split that string and get only the name?


Solution

  • Like this:

    NSArray* rows = [originalString componentsSeparatedByString:@"\n"];
    NSMutableDictionary* dictionary = [NSMutableDictionary new];
    
    for (NSString* row in rows) {
        NSString* key = [row componentsSeparatedByString:@": "].firstObject;
        NSString* value = [row componentsSeparatedByString:@": "].lastObject;
        [dictionary setObject:value forKey:key];
    }