[英]Key in dictionary gets deallocated before I can use it
我对Objective-C相当陌生。
基本上发生在iPhone 5s及更低版本上(在iPhone6上不是问题),程序在尝试访问已重新分配的字典中的键时崩溃。 ( EXC_BAD_ACCESS )
仅当该字符串超过6个字符时才会发生。 (!!)
我的模型中填充了来自API调用的数据,然后我们尝试使用它填充视图中的字段,特别是此行始终失败:
self.itemSubtitleLabel.text = itemModel.itemSubtitle;
现在,当我调试它时,我可以看到NSDictionary itemModel
具有所有正确的数据,但是当我们填充视图时,就会发生错误。
经过一番阅读后,我一直在使用仪器来捕获该错误。 我收集了有关“僵尸”事件的以下详细信息,但不确定如何处理。
我需要一些帮助,以解决该问题该采取的下一步措施
#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
/**
* 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;
}
- (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);
}];
}
-(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) {
}];
}
原来在我的模型头文件中,该属性被设置为assign
而不是strong
!
如此改变
@property (nonatomic,assign) NSString *itemSubtitle;
至
@property (nonatomic,strong) NSString *itemSubtitle;
谢谢@matt的帮助,至少让我读了有关线程的文章!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.