字典中的键在我可以使用之前被取消分配

时间:2016-11-27 19:46:14

标签: ios objective-c iphone memory-management

我对objective-c很新。

基本上发生的事情是iPhone 5s及更低版本(iPhone6上没有问题),程序崩溃,因为它试图访问已重新分配的字典中的密钥。 ( EXC_BAD_ACCESS

仅当该字符串超过6个字符时才会发生这种情况。 (!!)

我的模型中填充了来自API调用的数据,然后我们尝试使用它来填充视图中的字段,特别是此行始终失败:

self.itemSubtitleLabel.text = itemModel.itemSubtitle;

现在,当我调试它时,我可以看到NSDictionary itemModel拥有所有正确的数据,但是当我们填充视图时,会发生错误。

在做了一些阅读之后,我一直在用仪器来捕捉这个错误。我收集了以下关于" Zombie"的细节。事件,但不知道该怎么做。

enter image description here

我想帮助我解决这个问题需要采取的下一步措施

更多代码:

型号:

#import <Foundation/Foundation.h>
#import "ItemModel.h"

@implementation ItemModel

+(ItemModel*)populateDataWithObject:(NSDictionary*)responseData {
    ItemModel *newItem          = [[ItemModel alloc] init];
    newItem.heading             = [responseData objectForKey:@"heading"];
    newItem.storyID             = [responseData objectForKey:@"storyID"];
    newItem.thumbnail           = [responseData objectForKey:@"thumbnail"];
    newItem.itemSubtitle        = [responseData valueForKey:@"subtitle"];
    newItem.logo                = [responseData objectForKey:@"logo"];

    return newItem;
}


@end

调用API并填充单元格:

/**
 * getting all items
 */
- (void)getAllItems:(BOOL)withLoader {
    if (withLoader) {
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
    }

    [[ItemManager sharedManager] getItems:nil Success:^(NSArray * array) {
        [SVProgressHUD dismiss];
        allItemsArray = array;
        [self.itemsCollectionView reloadData];
        [refreshControl endRefreshing];
        reloadForFiltersSettings = NO;
        NSLog(@"SUCCESS");
    } failure:^(NSString *error) {
        NSLog(@"FAIL");
        reloadForFiltersSettings = NO;
        [SVProgressHUD dismiss];
        [refreshControl endRefreshing];
        allItemsArray = nil;
        [self.itemsCollectionView reloadData];

        if ([error isEqualToString:@"401"]) {
            [self reLogin];
        } else {
            if (self.storyTypeBaseView.hidden == YES) {
                UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"Oops!" message:[NSString stringWithFormat:@"%@",@"Nothing found"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];

                [alertView show];
            }
        }
    }];
}


/**
 * Loading items into cells for the "All" view
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


    ItemHeaderCell *cell = (ItemHeaderCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ItemHeaderCellNib forIndexPath:indexPath];

    ItemModel * itemModel = allItemsArray[indexPath.row];

    //loading data into cell
    [cell populateItemForCellWithData:itemModel withIndex: indexPath];

    if (itemModel.storyID) {
        cell.contentView.hidden = NO;
    } else {
        cell.contentView.hidden = YES;
    }

    return cell;
}

ItemManager getItems:

- (void)getItems:(NSMutableDictionary*) param Success:(void (^)(NSArray *headerModel ))success failure:(void (^)(NSString *error))failure {
    if (![_baseManager isNetworkAvailable]) {
        [_baseManager noNetworkError: failure];
        return;
    }

    [_baseManager GET: GET_ITEMS parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray * array = responseObject;

        if (array.count > 0) {
            NSMutableArray * mutableArray = [[NSMutableArray alloc] init];

            for (int i = 0; i < array.count; i++) {
                NSDictionary * dataDictionary = array[i];
                [mutableArray addObject:[ItemModel populateDataWithObject:dataDictionary]];
            }

            if (success) success(mutableArray);
        } else {
            failure(nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSString * statusCode = [_baseManager handleServerError: error withRequest: operation];

        if (failure) failure(statusCode);
    }];
}

ItemHeaderCell:

-(void)populateItemForCellWithData:(ItemModel*)itemModel withIndex:(NSIndexPath *)indexPath {
    [self.itemTitleLabel applyAttributedText: itemModel.heading];
    self.itemSubtitleLabel.text = itemModel.itemSubtitle;

    self.imageView.image = [UIImage imageNamed:@""];

    NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSURL *urlPt = [NSURL URLWithString:itemModel.logo];
    NSString *path = [docsPath stringByAppendingPathComponent:[urlPt lastPathComponent]];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:path]) {
        NSData *imgData = [NSData dataWithContentsOfFile:path];
        UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
        self.imageView.image =thumbNail;
    }

    NSURL *url =[NSURL URLWithString:itemModel.logo];
    NSMutableURLRequest *urlReq=[NSMutableURLRequest requestWithURL:url];

    [self.imageView setImageWithURLRequest:urlReq placeholderImage:[UIImage imageNamed:@""] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        [self.imageView setImage:image];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

    }];
}

1 个答案:

答案 0 :(得分:0)

原来我的Model头文件中的属性设置为assign而不是strong

如此改变

@property (nonatomic,assign) NSString *itemSubtitle;

@property (nonatomic,strong) NSString *itemSubtitle;

感谢@matt的帮助,至少它让我读到了线程!