我可以只读取 plist 中的密钥吗?

发布于 2024-11-14 06:06:20 字数 48 浏览 2 评论 0原文

我可以只从 plist 中读取键而不读取其值吗?如果我知道该值,我可以读取该键吗?

Can I read just the key from plist without its value, also if I know the value can I read the key ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

删除会话 2024-11-21 06:06:20

读取.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];

Reading .plist:

NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];    
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

Getting all keys and all values:

NSArray* allmyKeys = [myDictionary  allKeys];
NSArray* allmyValues= [myDictionary  allValues];

Getting all keys for an values object:

NSArray* allmyKeys = [myDictionary  allKeysForObject:myValueObject];
那请放手 2024-11-21 06:06:20

作为替代方案,您可以使用 allKeysForObject: 返回的方法,

一个新数组,其中包含与字典中所有出现的 anObject 相对应的键。如果没有找到与 anObject 匹配的对象,则返回一个空数组。

您可以通过调用 objectAtIndex: 方法从该数组中获取key

As an alternative, you can use allKeysForObject: method which returns,

A new array containing the keys corresponding to all occurrences of anObject in the dictionary. If no object matching anObject is found, returns an empty array.

From that array you can get the key by invoking the objectAtIndex: method.

苄①跕圉湢 2024-11-21 06:06:20

使用 -[NSDictionary allKeysForObject:]*。


示例

NSArray *keys = [myDict allKeysForObject:@"My Value"];
if ([keys count] != 0) { // to prevent out-of-bounds crashes
  NSString *key = [keys objectAtIndex:0];
  return key;
} else {
  return nil;
}

*不知道为什么它返回 NSArray 对象而不是 NSSet 对象,因为键没有排序。哦,好吧。

Use -[NSDictionary allKeysForObject:]*.


Example

NSArray *keys = [myDict allKeysForObject:@"My Value"];
if ([keys count] != 0) { // to prevent out-of-bounds crashes
  NSString *key = [keys objectAtIndex:0];
  return key;
} else {
  return nil;
}

*Dunno why it returns an NSArray object instead of an NSSet object, because keys are not ordered. Oh well.

三生池水覆流年 2024-11-21 06:06:20

为了读取应用程序安装文件夹中的值:

 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];

Jhaliya 的方法对我不起作用,然后我尝试了这种方法。

In order to read the values at app's installed folder:

 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];

Jhaliya's method has not worked for me, then I tried this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文