简体   繁体   English

在NSDictionary中编辑plist

[英]Editing a plist in NSDictionary

I'm importing a plist into an NSDictionary object and want to change the contents of the plist once in the dictionary to lowercase. 我正在将plist导入NSDictionary对象,并希望将字典中plist的内容一次更改为小写。 I have tried several different ways with no luck. 我已经尝试了几种不同的方法,但是没有运气。 Maybe I am supposed to edit the strings that are inside the NSDictionary, but then I don't know how to do that. 也许我应该编辑NSDictionary内部的字符串,但是后来我不知道该怎么做。

What I have currently imports the plist correctly, but the lowercaseString line effectively does nothing. 我目前正确地导入了plist,但是lowercaseString这一行实际上什么也没做。

NSString *contents = [[NSBundle mainBundle] pathForResource:@"Assets/test" ofType:@"plist"];

NSString *newContents = [contents lowercaseString];

NSDictionary *someDic = [NSDictionary dictionaryWithContentsOfFile:newContents];

for (NSString *someData in someDic) {
        NSLog(@"%@ relates to %@", someData, [someDic objectForKey:someData]);
    }

I NSLog'd the "content" string and it gave me a path value, so obviously changing that to a lowercaseString wouldn't do anything. 我NSLog的“内容”字符串,它给了我一个路径值,所以很显然将其更改为lowercaseString不会做任何事情。 I'm feeling like I have to access the strings within the dictionary. 我觉得我必须访问字典中的字符串。

This line 这条线

NSString *newContents = [contents lowercaseString];

is change the path string returned from 更改从返回的路径字符串

NSString *contents = [[NSBundle mainBundle] pathForResource:@"Assets/test" ofType:@"plist"];

so you will end up with something like ../assets/test.plist 所以您最终会得到类似../assets/test.plist的信息

you will need to walk through the contents of someDic an create a new dictionary based on the old turning strings into lowercase, if you are only concerned about values directly in someDic you can do something like 您将需要遍历someDic的内容,并基于将旧的变成小写字母的字符串创建一个新字典,如果您仅关注someDic中的直接值,则可以执行以下操作

for( NSString * theKey in someDic )
{
    id     theObj = [someDic objectForKey:theKey];
    if( [theObj isKindOfClass:[NSString class]] )
        theObj = [theObj lowercaseString];
    [theNewDict setObject:theObj forKey:theKey];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM