简体   繁体   English

我如何在 xcode 中获取数组?

[英]How i can fetch array in xcode?

How do I fetch the values of my dictionary not a loop.如何获取字典的值而不是循环。 I know how to get a single value using:我知道如何使用以下方法获取单个值:

NSString *valueStr = [dict objectForKey:@"Key2"];

I need to fetch by looping all keys, I need to search a value in the dictionary.我需要通过循环所有键来获取,我需要在字典中搜索一个值。

Since it looks like you are trying to use a NSDictionary you would want to do something like this:由于看起来您正在尝试使用NSDictionary,因此您可能想要执行以下操作:

for (NSString *key in [dict allKeys]){
    NSLog(@"%@", [dict objectForKey:key]);
}

or if you dont want to use looping you can the following:或者,如果您不想使用循环,则可以执行以下操作:

NSArray *keys = [dict allKeys];
NSArray *values = [dict allValues];
for (NSString *key in <#dictionary#>)
{
    <#!loopcontents#>
    [dictionary objectForKey:key]
}

you seem to have a NSDictionary , you can access all keys/values with:您似乎有一个NSDictionary ,您可以通过以下方式访问所有键/值:

NSArray *keys = [dict allKeys];
NSArray *values = [dict allValues];

refering https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html参考https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html

of course you can loop over that key-value pairs with当然,您可以使用以下方法遍历该键值对

for (NSString *key in [dict allKeys]) {

    //get value
    NSString *value = [dict objectForKey:key];

    //print to log
    NSLog (@"%@, %@", key, value);
}

Combining answers from Omarj and Rene:结合 Omarj 和 Rene 的回答:

for (NSString *key in [dict allKeys])
{
    NSString *value = [dict objectForKey:key];
}

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

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