如何检查密钥对中的密钥是否可用?

时间:2017-12-29 09:18:09

标签: ios objective-c json uitableview nsdictionary

我正在尝试用iOS学习json。 我正在尝试使用UITableView实施json数据。在我的tableview中,Title和subtitle中的行显示了值。我可以在标题中加载数据,但是当尝试加载字幕时,应用程序会崩溃,因为在字幕结果中,json的字段和许多键对值都被赋予了。因此无法在tableview的子标题中显示。

我的问题是如何加载字幕,如果点击许多键值对中的键可以将其重定向到其他tableview以显示该数据。

我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid%3D1100661&format=json"]];
    NSError *error=nil;
    id response=[NSJSONSerialization JSONObjectWithData:data options:
                 NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error]; 
    if (error) {
        NSLog(@"%@",[error localizedDescription]);
    } else {
        _query = [response objectForKey:@"query"];
        NSLog(@"%@",_query);
        _keyArray = [_query allKeys];   
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_keyArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

    NSString *key = [_keyArray objectAtIndex:indexPath.row];
    //NSString *dictionary = [_query objectForKey:key];
    NSString *dictionary = [_query objectForKey:key];
    cell.textLabel.text = key;
    cell.detailTextLabel.text = dictionary;  
    return cell; 
}

感谢。

1 个答案:

答案 0 :(得分:1)

我从你的问题和评论中理解的事情,这是我一步一步的答案......

首先,我在我的NSArray课程中选择了NSDictionaryViewController.h个属性。

{
NSArray *_allKeys;
}
 @property(nonatomic,strong) NSDictionary *query;

然后在ViewController.m我为query属性创建了一个setter方法,我将数据设置为_query_allKeys,就像这样......

 -(void)setQuery:(NSDictionary *)query
{
    _query = query;
    _allKeys = [_query allKeys];
    if ([self isViewLoaded]) {
        [tableView reloadData];
    }

}

现在在UITableView数据源方法cellForRow中,我已更新您的代码..

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSString *key = _allKeys[indexPath.row];
    id value = _query[key];
    cell.textLabel.text = key;
    if ([value isKindOfClass: [NSString class]]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.detailTextLabel.text = (NSString*)value;
    }
    else if ([value isKindOfClass: [NSDictionary class]])
    {
        cell.detailTextLabel.text = @"More Info";
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if ([value isKindOfClass: [NSArray class]])


       {
            cell.detailTextLabel.text = @"Multiple Entries Found";
             cell.accessoryType = UITableViewCellAccessoryNone;

// do something show array data with prototype custom cell
        }
        return cell;
    }

现在在UITableView委托方法didSelect中,我为同一个ViewController创建了一个新实例(我们可以重用它,因为它具有相同的UI布局),并传递值_query ...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = _allKeys[indexPath.row];
    id value = _query[key];
    if ([value isKindOfClass: [NSDictionary class]])
    {
        ViewController *nextVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
        nextVC.query = value;
        [self.navigationController pushViewController:nextVC animated:YES];
    }
}

注意:每当我实例化webservice实例时,我都不想在ViewController.m中拨打ViewController来电。所以我将webservice代码放在AppDelegate didFinishLaunch方法中。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid%3D1100661&format=json"]];
    NSError *error=nil;
    id response=[NSJSONSerialization JSONObjectWithData:data options:
                 NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error];
    if (error) {
        NSLog(@"%@",[error localizedDescription]);
    } else {
     NSDictionary*   _query = [response objectForKey:@"query"];

        dispatch_async(dispatch_get_main_queue(), ^{
            ViewController *vc = (ViewController*)[(UINavigationController*)self.window.rootViewController topViewController];
            vc.query = _query;
        });
    }
    // Override point for customization after application launch.
    return YES;
}

在您放置此代码的位置由您决定,但最好我们应将以下代码放在您UIViewController的前一个DatashowingViewController中(我的是ViewController)并将信息传递给ViewController(就像我所做的那样),以便我们可以重复使用相同的UIViewController来显示相同​​的结果。

我希望这个答案可以帮助您实现输出。

相关问题