Ios 我能读懂plist的钥匙吗?
我是否可以只读取plist中的键而不读取其值?如果我知道该值,我是否可以读取该键?使用Ios 我能读懂plist的钥匙吗?,ios,iphone,ipad,plist,key-value,Ios,Iphone,Ipad,Plist,Key Value,我是否可以只读取plist中的键而不读取其值?如果我知道该值,我是否可以读取该键?使用-[NSDictionary allKeysForObject:* 例子 *不知道为什么它返回一个NSArray对象而不是NSSet对象,因为键没有排序。哦,好吧。作为替代,您可以使用返回 一个新数组,包含与字典中某个对象的所有匹配项相对应的键。如果找不到与某个对象匹配的对象,则返回空数组 从该数组中,您可以通过调用objectAtIndex:方法来获取密钥。Reading.plist: NSString
-[NSDictionary allKeysForObject:
*
例子
*不知道为什么它返回一个NSArray对象而不是NSSet对象,因为键没有排序。哦,好吧。作为替代,您可以使用返回 一个新数组,包含与字典中某个对象的所有匹配项相对应的键。如果找不到与某个对象匹配的对象,则返回空数组
从该数组中,您可以通过调用
objectAtIndex:
方法来获取密钥。Reading.plist:
NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSArray* allmyKeys = [myDictionary allKeys];
NSArray* allmyValues= [myDictionary allValues];
NSArray* allmyKeys = [myDictionary allKeysForObject:myValueObject];
获取所有键和所有值:
NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSArray* allmyKeys = [myDictionary allKeys];
NSArray* allmyValues= [myDictionary allValues];
NSArray* allmyKeys = [myDictionary allKeysForObject:myValueObject];
获取值对象的所有键:
NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSArray* allmyKeys = [myDictionary allKeys];
NSArray* allmyValues= [myDictionary allValues];
NSArray* allmyKeys = [myDictionary allKeysForObject:myValueObject];
要读取应用程序已安装文件夹中的值:
NSString *PListName=@"ExamplePlist";
NSString *_PlistNameWithExtension=[NSString stringWithFormat:@"%@.plist",PlistName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:_PlistNameWithExtension]; //3
NSDictionary *myDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
NSLog(@"%@",[myDictionary description]);
NSArray *AllKeys=[myDictionary allKeys];
贾利亚的方法对我不起作用,于是我尝试了这个方法 如果不同的键具有相同的值怎么办?那么必须发生什么?@WTP:没有键具有相同的值,并且该案例不会发生。我添加了一个示例。我不知道这种方法:)
myDictionary
在这里应该是NSDictionary
,而不是NSMutableDictionary
。