Objective c 目标C为变量分配字典并访问它
很抱歉再次问这个问题,但我还是被卡住了 我有一个城市对象试图从天气获取器对象获取天气Objective c 目标C为变量分配字典并访问它,objective-c,variables,nsdictionary,Objective C,Variables,Nsdictionary,很抱歉再次问这个问题,但我还是被卡住了 我有一个城市对象试图从天气获取器对象获取天气 @interface WeatherFetcher : NSObject { } @property (nonatomic, strong) NSMutableDictionary *weatherData; - (void)fetchWeather:(NSString *)cityName; - (void)handleNetworkErorr:(NSError *)error; - (void)han
@interface WeatherFetcher : NSObject {
}
@property (nonatomic, strong) NSMutableDictionary *weatherData;
- (void)fetchWeather:(NSString *)cityName;
- (void)handleNetworkErorr:(NSError *)error;
- (void)handleNetworkResponse:(NSData *)myData;
@end
这是我分配给weatherData的值
#import "WeatherFetcher.h"
@implementation WeatherFetcher
- (void)fetchWeather:(NSString *)cityName
{
NSString *urlString = @"http://api.openweathermap.org/data/2.5/weather?q=";
urlString = [urlString stringByAppendingString:cityName];
urlString = [urlString stringByAppendingString:@",Aus"];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError)
{
[self handleNetworkErorr:connectionError];
}
else
{
[self handleNetworkResponse:data];
}
}];
}
#pragma mark - Private Failure Methods
- (void)handleNetworkErorr:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Please try again later" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}
#pragma mark - Private Success Methods
- (void)handleNetworkResponse:(NSData *)myData
{
//NSMutableDictionary *data = [NSMutableDictionary dictionary];
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
// now we'll parse our data using NSJSONSerialization
id myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];
// typecast an array and list its contents
NSDictionary *jsonArray = (NSDictionary *)myJSON;
//NSLog([jsonArray description]);
// take a look at all elements in the array
for (id element in jsonArray) {
id key = [element description];
id innerArr = [jsonArray objectForKey:key];
NSDictionary *inner = (NSDictionary *)innerArr;
if ([inner conformsToProtocol:@protocol(NSFastEnumeration)]) {
for(id ele in inner) {
if ([ele conformsToProtocol:@protocol(NSFastEnumeration)]) {
NSDictionary *innerInner = (NSDictionary *)ele;
for(id eleEle in innerInner) {
id innerInnerKey = [eleEle description];
[data setObject:[[inner valueForKey:innerInnerKey] description] forKey:[eleEle description]];
}
}
else {
id innerKey = [ele description];
[data setObject:[[inner valueForKey:innerKey] description] forKey:[ele description]];
}
}
}
else {
[data setObject:[inner description] forKey:[element description]];
}
}
self.weatherData = data;
NSLog([self.weatherData description]) **//there is data**
}
@end
然而,每次我从“城市对象”中调用它时,我什么也得不到
#import <Foundation/Foundation.h>
#import "WeatherFetcher.h"
@interface City : NSObject {
}
@property (nonatomic, strong) NSString *cityName;
@property (nonatomic, strong) NSString *stateName;
@property (nonatomic, strong) UIImage *cityPicture;
@property (nonatomic, strong) NSString *weather;
@property (nonatomic, strong) NSMutableDictionary *weatherData;
-(NSString *)getWeather;
@end
我做错了什么
当按下按钮并且我试图在文本区域中显示此字符串时,city类中的getWeather方法将被调用。我对Objective C没有太多经验,这是我除了Hello World应用程序之外的第一个应用程序
谢谢大家! 您的
WeatherFetcher
是异步的(sendAsynchronousRequest:
)-它设置任务以获取数据,然后(通常)在获取数据之前返回。因此,当您尝试在调用fetchWeather:
后立即访问weatherData
时,它还没有出现
您需要重新设计模型以处理异步性-
getWeather
不能是同步的。例如,您可以让getWeather:
在数据可用时调用一个完成块,并让getWeather
传入一个合适的块。这一次,当有人给您留下评论时,添加另一条评论来回答他们的问题。否则看起来就像你睡着了,人们会生气,不想帮助你。对不起,我正在尝试格式化注释部分的文本,但我似乎无法正确显示代码,这就是我打开一个新问题的原因,并且认为在fetchWeather:
方法中显示异步处理的整个代码可能会有所帮助。你在获得结果之前就试图对结果进行处理。我是一个完全的初学者,你能给我举个例子吗?许多的thanks@Archie-示例是您的weatherFetcher
,您调用sendAsynchronousRequest:queue:completionHandler:
,并在请求完成时传递要调用的块。您需要重复该模式以适合您的应用程序。因此,您可以将fetchWeather:
更改为fetchWeather:completionHandler:
,并通过调用传递给fetchWeather
的块来完成当前的handleNetworkResponse:
,等等。将您的模型从同步方法调用/返回更改为异步模型,在异步模型中,方法调用不返回值,而是将它们传递给完成块-阅读异步设计。
@implementation City {
}
-(NSString *)getWeather {
//return self.weather;
NSString *info = @"";
WeatherFetcher *weatherFetcher = [[WeatherFetcher alloc] init];
[weatherFetcher fetchWeather:self.cityName];
self.weatherData = [weatherFetcher weatherData];
for (id element in self.weatherData) {
info = [info stringByAppendingString:[element description]];
info = [info stringByAppendingString:@"-->"];
info = [info stringByAppendingString:[self.weatherData valueForKey:[element description]]];
info = [info stringByAppendingString:@"\n"];
}
return info;
}
@end