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

ここで何が間違っていますか?

ボタンが押されたときに都市クラスの getWeather メソッドが呼び出され、この文字列をテキスト領域に表示しようとしています。私は Objective C の経験があまりなく、これが Hello World アプリ以外の私の最初のアプリです。

ありがとうございました!

4

1 に答える 1