我目前正在尝试寻找Twitter Feed,但是当我尝试添加分页到现有NSMutableArray时收到的新项目时,我收到了此错误:
Floadt[28482:2207263] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
*** First throw call stack:
(
0 CoreFoundation 0x0000000106263c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001059a0bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000106263b9d +[NSException raise:format:] + 205
3 CoreFoundation 0x000000010625c46a -[__NSCFArray insertObject:atIndex:] + 106
4 CoreFoundation 0x0000000106186923 -[NSMutableArray insertObjects:count:atIndex:] + 179
5 CoreFoundation 0x0000000106186654 -[NSMutableArray insertObjectsFromArray:range:atIndex:] + 372
6 CoreFoundation 0x0000000106186454 -[NSMutableArray addObjectsFromArray:] + 612
7 Floadt 0x0000000102375d89 __57-[TwitterTableViewController fetchNextTwitterPageWithID:]_block_invoke + 217
8 Floadt 0x0000000102348e43 __64-[AFJSONRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke91 + 51
9 libdispatch.dylib 0x000000010844d186 _dispatch_call_block_and_release + 12
10 libdispatch.dylib 0x000000010846c614 _dispatch_client_callout + 8
11 libdispatch.dylib 0x0000000108454a1c _dispatch_main_queue_callback_4CF + 1664
12 CoreFoundation 0x00000001061cb1f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
13 CoreFoundation 0x000000010618cdcb __CFRunLoopRun + 2043
14 CoreFoundation 0x000000010618c366 CFRunLoopRunSpecific + 470
15 GraphicsServices 0x0000000107d4ba3e GSEventRunModal + 161
16 UIKit 0x00000001046d4900 UIApplicationMain + 1282
17 Floadt 0x000000010242d46f main + 111
18 libdyld.dylib 0x00000001084a0145 start + 1
19 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
分页JSON正常返回。当用户滚动到TableView的底部时,我只想在原始项目下面添加分页项目。
检索已分页推文的方法
-(void)fetchNextTwitterPageWithID:(NSString *)objectID {
self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/"] key:@"tA5TT8uEtg88FwAHnVpBcbUoq" secret:@"L5whWoi91HmzjrE5bNPNUgoMXWnImvpnkIPHZWQ4VmymaoXyYV"];
NSDictionary *parameters = @{
@"max_id" :objectID
};
AFOAuth1Token *twitterToken = [AFOAuth1Token retrieveCredentialWithIdentifier:@"TwitterToken"];
[self.twitterClient setAccessToken:twitterToken];
[self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self.twitterClient getPath:@"statuses/home_timeline.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSMutableArray *responseArray = (NSMutableArray *)responseObject;
NSLog(@"Response: %@", responseObject);
tweets = [tweets copy];
[tweets addObjectsFromArray:responseArray];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
查找Twitter用户的方法
- (void)lookupTwitterUser:(NSString *)user {
self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/"] key:@"tA5TT8uEtg88FwAHnVpBcbUoq" secret:@"L5whWoi91HmzjrE5bNPNUgoMXWnImvpnkIPHZWQ4VmymaoXyYV"];
NSDictionary *parameters = @{
@"screen_name" :user
};
AFOAuth1Token *twitterToken = [AFOAuth1Token retrieveCredentialWithIdentifier:@"TwitterToken"];
[self.twitterClient setAccessToken:twitterToken];
[self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self.twitterClient getPath:@"users/lookup.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
userLookup = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
检测更多用户的方法位于页面底部
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tweets count] == (indexPath.row+1)) {
NSDictionary *totalArray = tweets[indexPath.row];
NSString *cellID = [totalArray objectForKey:@"id"];
NSLog(@"%@",cellID);
[self fetchNextTwitterPageWithID:cellID];
}
}
答案 0 :(得分:1)
错误消息足够清楚。您正在尝试改变不可变数组。根据堆栈跟踪,错误在fetchNextTwitterPageWithID:
方法中,您调用addObjectsFromArray:
。
可疑线是这一行:
NSMutableArray *responseArray = (NSMutableArray *)responseObject;
很可能responseObject
不是可变数组,而是不可变数组。
将行更改为:
NSMutableArray *responseArray = [responseObject mutableCopy];
此外,这一行是一个问题:
tweets = [tweets copy];
同样,你想要一个可变副本:
tweets = [tweets mutableCopy];