EXC_BAD_ACCESS。完成块

时间:2016-10-03 08:49:19

标签: ios objective-c iphone afnetworking objective-c-blocks

答案

当我调用fetchDailyForecast它看起来像

时,我犯了一个错误
  [self.fetchController fetchDailyForecast:self.currentLocation.coordinate completionBlock:^(DailyModel *newModel) {
        _dailyCondition = newModel;
        NSLog(@"newModel = %@", _dailyCondition);
    }];

END

我尝试使用bloc&s等等但是有一个bad_access exaction。首先,我从MangeDataController.m

调用方法
  

[self.fetchController   fetchDailyForecast:self.currentLocation.coordinate   completionBlock:(void(^)(DailyModel *))_ dailyCondition];

其中dailyCondition是DailyModel的实例。 其次,这是FetchController.m中的 fetchDailyForecast 方法实现。

-(void)fetchDailyForecast:(CLLocationCoordinate2D)coordinate completionBlock:(void(^)(DailyModel *))completionBlock {
    NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/forecast/daily?lat=%f&lon=%f&units=imperial&cnt=7%@", coordinate.latitude, coordinate.longitude, _key];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [self fetchJSONfromURL:urlString completionBlock:^(NSDictionary *weatherData) {
        completionBlock([[DailyModel alloc] dataFromDictionaryDaily:weatherData]); --- (here the bad access);
    }];
}

第三,在 fatchDailyForecast 中,我在这里调用 fetchJSONfromURL 实现:

-(void)fetchJSONfromURL:(NSString *)urlString completionBlock:(void (^)(NSDictionary *))completionBlock {
    NSLog(@"url = %@", urlString);
    [_manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responceObject) {
        if (responceObject) {
            completionBlock(responceObject);
        } else {
            NSLog(@"responce object error");
        }
    }
          failure:^(NSURLSessionTask *operation, NSError *error){

              NSLog(@"error %@", error);
          }];
}

P.S。我认为我的错误是我试图通过一天 _dailyCondition ,但应该传递一些不同的东西。谢谢你的帮助!

**编辑------- DailyModel.m **

#import "DailyModel.h"

@implementation DailyModel


-(id)dataFromDictionaryDaily:(NSDictionary *)dic {
     NSArray *arrayWithDictionarys = [dic objectForKey:@"list"];
    NSDictionary *dictionaryWithWeekDays = [self indexKeyedDictionaryFromArray:arrayWithDictionarys];
   // NSLog(@"dictionary with dayweeks = %@", dictionaryWithWeekDays);
    _arrayWithTemp = [NSMutableArray new];
    _arrayWithDate = [NSMutableArray new];
    _arrayWithIcon = [NSMutableArray new];

    for (int i = 0; i <= 6; i++) {
        NSNumber* key = [NSNumber numberWithInt:i];
        [self addValuesToArrayWithTemp:dictionaryWithWeekDays key:key index:i];
        [self addValuesToArrayWithDate:dictionaryWithWeekDays key:key index:i];
        [self addValueToArrayWithImage:dictionaryWithWeekDays key:key index:i];
    }
    return self;
}

- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
    NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
    [array enumerateObjectsUsingBlock:
     ^(id obj, NSUInteger idx, BOOL *stop){
         NSNumber *index = [NSNumber numberWithInteger:idx];
         [mutableDictionary setObject:obj forKey:index];
     }];
    NSDictionary *result = [NSDictionary.alloc initWithDictionary:mutableDictionary];
    return result;
}

-(void) addValuesToArrayWithTemp:(NSDictionary *)inputDic key:(NSNumber *)key index:(int)ind {
    NSDictionary *currentDic = [inputDic objectForKey:key];
    NSDictionary *temp = [currentDic objectForKey:@"temp"];
    _tempLow = [temp objectForKey:@"min"];
    _tempHigh = [temp objectForKey:@"max"];
    _tempLow = [self convertToCelsius:_tempLow];
    _tempHigh = [self convertToCelsius:_tempHigh];
    NSString *tempString = [NSString stringWithFormat:@"%.02f / %.02f", _tempLow.floatValue , _tempHigh.floatValue];
    [_arrayWithTemp insertObject:tempString atIndex:ind];
}

-(NSNumber *)convertToCelsius:(NSNumber *)far {
    double f =  (far.doubleValue - 32) / 1.8;
    NSNumber *celsius = [NSNumber numberWithDouble:f];

    return celsius;
}

-(void) addValuesToArrayWithDate:(NSDictionary *)inputDic key:(NSNumber *)key index:(int)ind {
    NSDictionary *currenDic = [inputDic objectForKey:key];
    NSNumber *tempNumber = [currenDic objectForKey:@"dt"];
    double unixTimeStamp = tempNumber.doubleValue;
    NSString* weekDay = [self convertToWeekDay:unixTimeStamp];
    [_arrayWithDate insertObject:weekDay atIndex:ind];
}

-(NSString *)convertToWeekDay:(double) unixtime {

    NSTimeInterval _interval = unixtime;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setDateFormat:@"EEEE"];
    NSString *dateString = [formatter stringFromDate:date];

    return dateString;
}

-(void) addValueToArrayWithImage:(NSDictionary *)inputDic key:(NSNumber *)key index:(int)ind {
    NSDictionary *currenDic = [inputDic objectForKey:key];
    NSArray *weatherArray = [currenDic objectForKey:@"weather"];
    NSDictionary *weather = weatherArray[0];

    _icon = [weather objectForKey:@"icon"];
    _icon = [self currentImageString:_icon];

    [_arrayWithIcon insertObject:_icon atIndex:ind];
}

-(NSString *)currentImageString:(NSString *)ico {
    NSDictionary *dic = @{
                          @"01d" : @"weather-clear",
                          @"02d" : @"weather-few",
                          @"03d" : @"weather-few",
                          @"04d" : @"weather-broken",
                          @"09d" : @"weather-shower",
                          @"10d" : @"weather-rain",
                          @"11d" : @"weather-tstorm",
                          @"13d" : @"weather-snow",
                          @"50d" : @"weather-mist",
                          @"01n" : @"weather-moon",
                          @"02n" : @"weather-few-night",
                          @"03n" : @"weather-few-night",
                          @"04n" : @"weather-broken",
                          @"09n" : @"weather-shower",
                          @"10n" : @"weather-rain-night",
                          @"11n" : @"weather-tstorm",
                          @"13n" : @"weather-snow",
                          @"50n" : @"weather-mist",
                          };
    ico = [dic objectForKey:ico];
    return ico;
}
@end

编辑2

我将条件添加到(void)fetchDailyForecast:(CLLocationCoordinate2D)坐标completionBlock:(void(^)(DailyModel *))completionBlock ,现在应用程序没有崩溃,但它&#39 ; s仍然不起作用,nslog说@&#34;没有&#34;

  

[self fetchJSONfromURL:urlString completionBlock:^(NSDictionary   * weatherData){if(completionBlock){completionBlock(model = [[DailyModel alloc] dataFromDictionaryDaily:weatherData]); } else {   的NSLog(@&#34;无&#34); }};

0 个答案:

没有答案