How to split NSString to NSDictionary

281 Views Asked by At

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?

1

There are 1 best solutions below

0
AudioBubble On BEST ANSWER
  • First separate your text by new line.
  • Then for each line, separate by :.
  • Then set first part as key and last part as value of dictionary.

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