Editing key inside array item - plist

Posted by F0u4d on Stack Overflow See other posts from Stack Overflow or by F0u4d
Published on 2014-08-23T09:30:34Z Indexed on 2014/08/23 10:21 UTC
Read the original article Hit count: 178

Filed under:
|
|

I have the following plist:

<plist version="1.0">
<dict>
    <key>General</key>
    <dict>
        <key>Table 1</key>
        <array>
            <dict>
                <key>subheadingName</key>
                <string>Item 1 of table 1</string>
                <key>subheadingDetail</key>
                <string>details about item 1</string>
            </dict>
            <dict>
                <key>subheadingName</key>
                <string>Item 2 of table 1</string>
                <key>subheadingDetail</key>
                <string>details about item 2!</string>
            </dict>
            <dict>
                <key>subheadingName</key>
                <string>Item 3 of table 1</string>
                <key>subheadingDetail</key>
                <string>details about item 3!</string>
            </dict>
        </array>
    </dict>
    <key>Table 2</key>
    <dict>
        <key>subheadingArr</key>
        <array>
            <dict>
                <key>subheadingName</key>
                <string>Item 1 of table 2</string>
                <key>subheadingDetail</key>
                <string>details about item 1</string>
            </dict>
            <dict>
                <key>subheadingName</key>
                <string>Item 2 of table 2</string>
                <key>subheadingDetail</key>
                <string>details about item 2!</string>
            </dict>
            <dict>
                <key>subheadingName</key>
                <string>Item 3 of table 2</string>
                <key>subheadingDetail</key>
                <string>details about item 3!</string>
            </dict>
        </array>
    </dict>
</dict>
</plist>

I am trying to write and read the

I have made these 2 methods trying to read and write subheadingDetail for a specific subheadingName but they are wrong/incomplete and can't manage to get it work.

-(void)updateInfo:(NSString *)info forSubHeadingName:(NSString *)subheadingName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DeviceInformation.plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    [data setObject:info forKey:subheadingName];

    [data writeToFile:path atomically:YES];
}

-(NSString *)readInfoForSubHeadingName:(NSString *)subheadingName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DeviceInformation.plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    return [data objectForKey:subheadingDetail];
}

Tried searching around the answers, but couldn't find anything similar to my issue. Thanks in advance.

© Stack Overflow or respective owner

Related posts about ios

Related posts about objective-c