0

我有一个搜索栏,我想按序列号搜索 json http url。因此,如果我执行以下 json url:

http://www.ddproam.co.za/Central/Asset/AssetsWithSerial?Serial=S00000001

实际输出:

[{"AssetID":1,"AssetName":"Asset 1","AssetDesc":"This is a manually inserted Asset","AssetTypeID":1,"AssetTypeDesc":"This is a manually inserted Asset Type"}]

我正在尝试通过搜索栏搜索序列号 S00000001 以使用 json 调用的 AssetName 填充我的表格单元格。

我已将我的 json 放入一个数组并尝试执行,但我收到一个应用程序崩溃错误,说明:

@property (strong, nonatomic) NSArray *loadedSearches;


'-[__NSArrayI objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1d857700'

这是我的代码:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

NSString *searchQuery = [NSString stringWithFormat:@"http://www.ddproam.co.za/Central/Asset/AssetsWithSerial?Serial='%@'",searchText];

searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [[NSURL alloc] initWithString:searchQuery];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
                                     {
                                         self.loadedSearches = JSON[@"AssetName"];

                                         // refreshing the TableView when the block gets the response
                                         [searchTableView reloadData];
                                     }
                                                                                    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
                                     {
                                         NSLog(@"%@", error.localizedDescription);
                                     }];

[operation start];


}

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//return self.loadedSearches.count;
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.loadedSearches.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

 if (self.loadedSearches[indexPath.row] == 0) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TableCell Tapped" message:@"Yeah you tapped a table cell" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}
NSLog(@"indexPath.row=%d loadedSearches.count=%d", indexPath.row, self.loadedSearches.count);

cell.textLabel.text = self.loadedSearches[indexPath.row][@"AssetName"];
return cell;
}

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
  // Perform segue to candy detail
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TableCell Tapped" message:@"Yeah you tapped a table cell" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

我想我在这里遇到了一个错误:

      self.loadedSearches = JSON[@"AssetName"];
4

1 回答 1

1

我认为您正在尝试NSArray通过密钥访问:JSON[@"AssetName"]。这适用于 anNSDictionary但不适用于NSArray. JSON所以在这里你需要知道你的变量是什么类。如果它确实与您发布的示例数据匹配,则它必须是一个数组,其中包含一个字典:[{}]。您可能希望将其转换为一个简单的数组,以便可以在表格视图中显示它:

NSDictionary *d = [JSON lastObject]; // the dictionary contained in the JSON array
NSMutableArray *a = [NSMutableArray array]; // the array the table view will display

// enumerate on all key/value pair of the dictionary
[d enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    // create a string with key and value separated by a colon
    NSString *s = [NSString stringWithFormat:@"%@: %@", key, obj];
    // add this string to the array
    [a addObject:s];
}];

// store the array, which now contains all the key/value pairs as strings, in loadedSearched
self.loadedSearches = a;
于 2013-05-22T13:43:59.433 回答