我的应用在刷新Twitter推文时遇到此错误:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
*** First throw call stack:
以下是代码:
- (void)fetchInstagramPics {
instaPics = [[NSMutableArray alloc] init];
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];
[self.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]; });
}
- (void)updateArrays {
instaPics = self.timelineResponse[@"data"];
totalFeed = [tweets arrayByAddingObjectsFromArray:instaPics];
}
- (void)fetchTimeline {
[self fetchInstagramPics];
[self fetchTweetsAuth];
[self updateArrays];
[self.tableView reloadData];
}
- (void)refetchTimeline {
[self fetchInstagramPics];
[self fetchTweets];
[self updateArrays];
[self.tableView reloadData];
}
- (void)fetchTweetsAuth {
self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/"] key:@"4oFCF0AjP4PQDUaCh5RQ" secret:@"NxAihESVsdUXSUxtHrml2VBHA0xKofYKmmGS01KaSs"];
[self.twitterClient authorizeUsingOAuthWithRequestTokenPath:@"/oauth/request_token" userAuthorizationPath:@"/oauth/authorize" callbackURL:[NSURL URLWithString:@"floadt://success"] accessTokenPath:@"/oauth/access_token" accessMethod:@"POST" scope:nil success:^(AFOAuth1Token *accessToken, id responseObject) {
[self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self.twitterClient getPath:@"statuses/home_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *responseArray = (NSArray *)responseObject;
[responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Success: %@", obj);
tweets = responseArray;
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
} failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
}
- (void)fetchTweets{
[self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self.twitterClient getPath:@"statuses/home_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *responseArray = (NSArray *)responseObject;
[responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Success: %@", obj);
tweets = [tweets copy];
tweets = responseArray;
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
- (void)refresh:(UIRefreshControl *)refreshControl {
[self refetchTimeline];
[refreshControl endRefreshing];
}
tweets
数组与instaPics
数组共享,该数组位于名为totalArray
的数组下,这就是我加入fetchInstagramPics
函数的原因。我知道这听起来很复杂,但你的帮助非常受欢迎。
这是stacktrace:
STACK TRACE :
(
0 CoreFoundation 0x02b6b5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x027978b6 objc_exception_throw + 44
2 CoreFoundation 0x02b6b3bb +[NSException raise:format:] + 139
3 CoreFoundation 0x02bf2365 -[__NSCFArray insertObject:atIndex:] + 101
4 CoreFoundation 0x02b2f2d0 -[NSMutableArray insertObjects:count:atIndex:] + 208
5 CoreFoundation 0x02b2ef69 -[NSMutableArray insertObjectsFromArray:range:atIndex:] + 425
6 CoreFoundation 0x02b2ed15 -[NSMutableArray addObjectsFromArray:] + 661
7 MyApp 0x0003c6c6 __42-[StreamViewController fetchInstagramPics]_block_invoke + 278
8 MyApp 0x0001e73b __64-[AFJSONRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke91 + 43
9 libdispatch.dylib 0x035187f8 _dispatch_call_block_and_release + 15
10 libdispatch.dylib 0x0352d4b0 _dispatch_client_callout + 14
11 libdispatch.dylib 0x0351b75e _dispatch_main_queue_callback_4CF + 340
12 CoreFoundation 0x02bd0a5e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
13 CoreFoundation 0x02b116bb __CFRunLoopRun + 1963
14 CoreFoundation 0x02b10ac3 CFRunLoopRunSpecific + 467
15 CoreFoundation 0x02b108db CFRunLoopRunInMode + 123
16 GraphicsServices 0x045309e2 GSEventRunModal + 192
17 GraphicsServices 0x04530809 GSEventRun + 104
18 UIKit 0x0192ad3b UIApplicationMain + 1225
19 MyApp 0x00062b1d main + 141
20 libdyld.dylib 0x037bf70d start + 1
)
答案 0 :(得分:0)
在-[AppDelegate didFinishLaunchingWithOptions:]
:
#ifdef DEBUG
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
#endif
在文件底部的AppDelegate中,在所有方法的下方:
#ifdef DEBUG
void uncaughtExceptionHandler(NSException *ex)
{
NSLog(@"CRASH :\n %@", ex.reason);
NSLog(@"STACK TRACE :\n %@", ex.callStackSymbols);
exit(0);
}
#endif
这样您将在控制台中看到堆栈跟踪。之后你会看到什么功能导致崩溃并修复它,或者通过发布堆栈跟踪来编辑你的问题让我们看看它。
编辑查看堆栈跟踪后:
您执行[self.instaPics addObjectsFromArray:responseObject[@"data"]];
,但是您instaPics = [[NSMutableArray alloc] init];
。我确定您的self.instaPics
是不可变的NSArray
。我不知道你的逻辑应该如何运作,你必须self.instaPics = [[NSMutableArray alloc] init];
或[instaPics addObjectsFromArray:responseObject[@"data"]];