我的應用程序被刷新的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
函數。我知道這聽起來很複雜,但您的幫助深表謝意。
這裏是堆棧跟蹤:
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
)
我已經更新了答案 –