NSDictionary Quickies

From macwrench
Jump to navigation Jump to search

Rekursiv ein Dictionary durchwandern

- (void)recurse:(NSDictionary *)dict counter: (int*) i parent:(NSString *) parent{
    for (NSString* key in [dict allKeys]) {
        id value = [dict objectForKey:key];
        NSLog(@"%@ -> %@", parent, key);
		
        if ([value isKindOfClass:[NSMutableDictionary class]]) {
            i++;
            NSDictionary* newDict = (NSDictionary*)value;
            [self recurse:newDict counter:i parent:[parent stringByAppendingFormat:@" -> %@",key]];
            i--;
        } else {
            // do something with this entry ...
        }
    }
}

NSDictionary aus NSData auslesen

NSString *errorStr = nil;
NSPropertyListFormat format;
NSDictionary *myDictionary = [NSPropertyListSerialization propertyListFromData:myData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&errorStr];
NSError *error = nil;
if (errorStr != nil) {
	error = [NSError errorWithDomain:@"NSPropertyListReadError" code:unimpErr userInfo:nil];
}

NSDictionary in NSData schreiben

NSData *myData = nil;
NSString *errorStr = nil;
myData = [NSPropertyListSerialization dataFromPropertyList:[saveProxy dictionary] format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorStr];
if (errorStr != nil) {
	*outError = [NSError errorWithDomain:@"NSPropertyListReadError" code:unimpErr userInfo:nil];
}

Siehe auch