0

很抱歉再次问这个问题,但我仍然卡住了。

我有一个城市对象试图从天气获取器对象中获取天气

@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

UI 通过按下按钮调用 getWeather 以获取要在屏幕上显示的字符串值

@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

我在这里做错了什么?

当按下按钮并且我试图在文本区域中显示此字符串时,会调用 city 类中的 getWeather 方法。我对 Objective C 没有太多经验,这是我除 Hello World 应用程序之外的第一个应用程序。

谢谢!

4

1 回答 1

1

WeatherFetcher异步的 ( sendAsynchronousRequest:) - 它设置一个任务来获取数据,然后(通常)获取数据之前返回。因此,当您尝试weatherData在调用它之后立即访问fetchWeather:它时还没有。

您需要重新设计模型以处理异步性 -getWeather不能同步。例如,您可以在数据可用并传入合适的块fetchWeather:时调用完成块。getWeather

于 2013-08-26T22:11:39.460 回答