如何检查密钥对中的密钥是否可用?
How To Check key in key pair is available or not?
我正在尝试通过 iOS 学习 json。
我正在尝试使用 UITableView
实现 json 数据。在我的标题和副标题的表视图行中显示的值。我可以在标题中加载数据,但是当尝试在副标题中加载时,应用程序崩溃了,因为在副标题 result 中 json 的字段和那么多 key-pair 值给出。所以无法在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;
}
谢谢。
我从你的问题和评论中了解到的事情,这是我一步一步的回答......
首先,我在我的 ViewController.h
class 中取得了一个 NSArray
和一个 NSDictionary
属性。
{
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];
}
}
注意: 我不想在 ViewController.m
中调用 webservice
调用,每当我实例化 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;
}
这取决于你把这段代码放在哪里,但最好我们应该把下面的代码放在你的DatashowingViewController
(我的是ViewController
)的前UIViewController
中并通过ViewController
的信息(就像我所做的那样),以便我们可以重复使用相同的 UIViewController
来显示相同的结果。
我希望这个答案能帮助你实现你的输出。
我正在尝试通过 iOS 学习 json。
我正在尝试使用 UITableView
实现 json 数据。在我的标题和副标题的表视图行中显示的值。我可以在标题中加载数据,但是当尝试在副标题中加载时,应用程序崩溃了,因为在副标题 result 中 json 的字段和那么多 key-pair 值给出。所以无法在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;
}
谢谢。
我从你的问题和评论中了解到的事情,这是我一步一步的回答......
首先,我在我的 ViewController.h
class 中取得了一个 NSArray
和一个 NSDictionary
属性。
{
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];
}
}
注意: 我不想在 ViewController.m
中调用 webservice
调用,每当我实例化 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;
}
这取决于你把这段代码放在哪里,但最好我们应该把下面的代码放在你的DatashowingViewController
(我的是ViewController
)的前UIViewController
中并通过ViewController
的信息(就像我所做的那样),以便我们可以重复使用相同的 UIViewController
来显示相同的结果。
我希望这个答案能帮助你实现你的输出。