Write JSON Response to .plist File

前端 未结 6 1604
遥遥无期
遥遥无期 2021-02-20 11:14

Frustration on the Top !!!

I am getting some JSON Response from the Service and I want to store it in the .plist file

6条回答
  •  孤独总比滥情好
    2021-02-20 11:50

    I build my file urls this way:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filename = @"FinalResult.plist";
    NSString *pathFilename = [path stringByAppendingPathComponent:filename];
    

    Then see if this writes:

    BOOL success = [dictResult writeToFile:pathFilename atomically:YES];
    NSLog(@"success? %d", success);
    

    Edit - Funny, I just recently confronted this problem and then forgot all about it. Some JSON parsers will use [NSNull null] as placeholders for nil values. I wrote this (seriously, just about two weeks ago and then spaced on it) to clean up the parse result...

    - (NSDictionary *)compact:(NSDictionary *)aDictionary {
    
        NSDictionary *answer = [NSMutableDictionary dictionary];
    
        for (NSString *key in [aDictionary allKeys]) {
            id value = [self.dictionary valueForKey:key];
            if (value && value != [NSNull null]) {
                [answer setValue:value forKey:key];
            }
        }
        return [NSDictionary dictionaryWithDictionary:answer];
    }
    

    This could be made into a NSDictionary category addition if you wanted.

提交回复
热议问题