我对objective-c很新。
基本上发生的事情是iPhone 5s及更低版本(iPhone6上没有问题),程序崩溃,因为它试图访问已重新分配的字典中的密钥。 ( EXC_BAD_ACCESS )
仅当该字符串超过6个字符时才会发生这种情况。 (!!)
我的模型中填充了来自API调用的数据,然后我们尝试使用它来填充视图中的字段,特别是此行始终失败:
self.itemSubtitleLabel.text = itemModel.itemSubtitle;
现在,当我调试它时,我可以看到NSDictionary itemModel
拥有所有正确的数据,但是当我们填充视图时,会发生错误。
在做了一些阅读之后,我一直在用仪器来捕捉这个错误。我收集了以下关于" Zombie"的细节。事件,但不知道该怎么做。
我想帮助我解决这个问题需要采取的下一步措施
#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) {
}];
}
答案 0 :(得分:0)
原来我的Model头文件中的属性设置为assign
而不是strong
!
如此改变
@property (nonatomic,assign) NSString *itemSubtitle;
到
@property (nonatomic,strong) NSString *itemSubtitle;
感谢@matt的帮助,至少它让我读到了线程!