Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 从数组中获取要应用于标签的字符串_Objective C_Nsstring_Uilabel_Nsarray - Fatal编程技术网

Objective c 从数组中获取要应用于标签的字符串

Objective c 从数组中获取要应用于标签的字符串,objective-c,nsstring,uilabel,nsarray,Objective C,Nsstring,Uilabel,Nsarray,我正在获取json对象。然后向下过滤到一个对象,这将生成一个对象的数组。以下是一个例子: company companyname: richs diner state: iowa city: antioch company companyname: dines state: california city: LA 我将上述信息过滤到一家公司。 然后过滤以仅将城市应用于标签,但似乎无法将一个单词数组更改为字符串 我想将每个值应用于标签。但我得到下面的错误。 有什么想法吗 以下是示例代码:

我正在获取json对象。然后向下过滤到一个对象,这将生成一个对象的数组。以下是一个例子:

company
companyname: richs diner
state: iowa
city: antioch

company
companyname: dines
state: california
city: LA
我将上述信息过滤到一家公司。 然后过滤以仅将城市应用于标签,但似乎无法将一个单词数组更改为字符串

我想将每个值应用于标签。但我得到下面的错误。 有什么想法吗

以下是示例代码:

     - (void)fetchedData:(NSData *)responseData {

//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1
                      options:kNilOptions
                      error:&error];

NSArray* getCompaniesArray = [json objectForKey:@"CompaniesCD"]; //2  get all company info

//NSDictionary* getCompaniesArray = [json objectForKey:@"CompaniesCD"]; //2  get all company info convert to dictionary insted

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"companyName = %@", selectedCompany];//added create filter to only selected state


NSArray *filteredArray = [getCompaniesArray filteredArrayUsingPredicate:predicate];//apply the predicate filter on the array


NSString *city = [filteredArray valueForKey:@"entityformSubmissionID"]; //print array to the string  //error
//NSString *city = [filteredArray objectAtIndex:0];//error
//NSString *city = filteredArray[0];//error

NSLog(@"here is your result: %@", city);//return result.  Works just fine

cityLabel.text = city;  //this does not apply the string to the label results in error

    }
我的错误是:

[\uu NSArrayI length]:发送到实例的无法识别的选择器 0x7fea5405f960 2015-12-28 21:49:58.027 J[3203:933300]***终止 应用程序由于未捕获异常“NSInvalidArgumentException”,原因: '-[\uu NSArrayI length]:发送到实例的选择器无法识别 0x7fea5405f960'


使用索引访问数组。你不能用密钥访问数组吗

NSString *city = filteredArray[0];//index 0 or other index 
cityLabel.text = city; 
在这一行…:

NSString *city = [filteredArray valueForKey:@"entityformSubmissionID"]; //print array to the string  //error
…假设发送到数组的
valueForKey:
返回字符串。这是错误的。它返回一个对象数组,数组中的对象使用键
entityformSubmissionID
保存这些对象。好吧,听起来比实际情况更复杂。(如果您在Q中添加了一个示例,则更容易解释。)

假设数组的成员是字典。每个字典都为键
entityformSubmissionID
存储了一个值。然后,您将获得该密钥的数组

例如:

// The filtered array with 4 objects (dictionaries)
[
  // The first object
  {
    @"entityformSubmissionID" : @"A",
    …
  },
  {
    @"entityformSubmissionID" : @"B",
   …
  },
  {
    @"entityformSubmissionID" : @"C",
    …
  },
  {
    @"entityformSubmissionID" : @"D",
    …
  }
]
应用
valueForKey:
将返回一个ID数组:

[
  @"A",
  @"B",
  @"C",
  @"D"
]
这显然不是细绳。您可以选择以下值之一:

NSString *cities = [filteredArray valueForKey:@"entityformSubmissionID"]; 
if( [cities count] > 0 )
{
  cityLabel.text = cities[0];
}

哦,我试过了,也犯了同样的错误。还有其他想法吗?从ArrayI获取带索引的字符串如果您获得的是正确的companyName,则使用“companyName=='%@'”更新您的谓词,这可能会对您有所帮助。