我正在尝试在我的iOS应用中使用Instagram API for Pagination。但是每当我将新图片添加到现有数组时,我都会收到此错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
这是关于如何获取初始Instagram图像集的代码:
- (void)fetchInstagramPics {
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
BOOL *status = [user boolForKey:@"isInstagramLoggedIn"];
if (status) {
[[InstagramClient sharedClient] getPath:@"users/self/feed"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"Response: %@", responseObject);
self.timelineResponse = [responseObject mutableCopy];
[instaPics addObjectsFromArray:responseObject[@"data"]];
[self updateArrays];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", error);
}];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData]; });
}
以下是我尝试将更多图片添加到instaPics
数组中的方法:
- (void)nextInstagramPage:(NSIndexPath *)indexPath{
NSDictionary *page = self.timelineResponse[@"pagination"];
NSString *nextPage = page[@"next_url"];
[[InstagramClient sharedClient] getPath:[NSString stringWithFormat:@"%@",nextPage] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.timelineResponse = [responseObject mutableCopy];
[self.timelineResponse addEntriesFromDictionary:responseObject];
[instaPics addObjectsFromArray:responseObject[@"data"]];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", error);
}];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y == roundf(scrollView.contentSize.height-scrollView.frame.size.height)) {
NSLog(@"we are at the endddd");
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self nextInstagramPage:indexPath];
}
}
以下是此类的Header文件中定义的所有属性:
@property (strong, nonatomic) NSMutableArray *instaPics;
@property (strong, nonatomic) AFOAuth1Client *twitterClient;
@property (strong, nonatomic) IBOutlet UINavigationItem *navBar;
@property (strong, nonatomic) NSMutableArray *tweets;
//编辑:现在有了细胞的构造:
if (indexPath.row % 2 == 0) {
static NSString *CellIdentifier = @"TweetCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
NSDictionary *tweet = tweets[indexPath.row];
return cell;
}else {
static NSString *CellIdentifier = @"InstagramCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
NSDictionary *entry = instaPics[indexPath.row];
return cell;
}
}
答案 0 :(得分:0)
您必须初始化NSMutableArray,在instaPics = [[NSMutableArray alloc] init];
fetchInstagramPics