使用AFNetworking发布Paging推特

时间:2015-01-18 05:22:57

标签: ios objective-c uitableview twitter

我正尝试使用AFNetworking在现有的推文NSMutableArray上发送推文。但是,每次运行我的应用程序时,我都会收到此错误:

 -[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object
2015-01-18 00:17:29.869 Floadt[91615:3385865] STACK TRACE :
 (
    0   CoreFoundation                      0x00000001055a1f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010523abb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001055a1e6d +[NSException raise:format:] + 205
    3   CoreFoundation                      0x000000010559a6ea -[__NSCFArray insertObject:atIndex:] + 106
    4   CoreFoundation                      0x00000001054c6a43 -[NSMutableArray insertObjects:count:atIndex:] + 179
    5   CoreFoundation                      0x00000001054c6774 -[NSMutableArray insertObjectsFromArray:range:atIndex:] + 372
    6   CoreFoundation                      0x00000001054c6574 -[NSMutableArray addObjectsFromArray:] + 612
    7   Floadt                              0x0000000101e91f85 __57-[TwitterTableViewController fetchNextTwitterPageWithID:]_block_invoke_2 + 117
    8   CoreFoundation                      0x00000001055029c4 __NSArrayEnumerate + 596
    9   Floadt                              0x0000000101e91e56 __57-[TwitterTableViewController fetchNextTwitterPageWithID:]_block_invoke + 230
    10  Floadt                              0x0000000101e73223 __64-[AFJSONRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke91 + 51
    11  libdispatch.dylib                   0x00000001076fbba6 _dispatch_call_block_and_release + 12
    12  libdispatch.dylib                   0x00000001077197f4 _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x00000001077028fb _dispatch_main_queue_callback_4CF + 949
    14  CoreFoundation                      0x0000000105509fe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    15  CoreFoundation                      0x00000001054cceeb __CFRunLoopRun + 2043
    16  CoreFoundation                      0x00000001054cc486 CFRunLoopRunSpecific + 470
    17  GraphicsServices                    0x0000000106f299f0 GSEventRunModal + 161
    18  UIKit                               0x0000000103f82420 UIApplicationMain + 1282
    19  Floadt                              0x0000000101ee5f73 main + 115
    20  libdyld.dylib                       0x000000010774e145 start + 1
    21  ???                                 0x0000000000000001 0x0 + 1

分页方法

-(void)fetchNextTwitterPageWithID:(NSString *)objectID {
    self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/"] key:@"XXXXXXXXXXX" secret:@"XXXXXXXXXXXX"];

    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;
        [responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [tweets addObjectsFromArray:responseObject];
            [self.tableView reloadData];
        }];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

检测页面底部

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        NSDictionary *totalArray = tweets[indexPath.row];
        NSString *cellID = [totalArray objectForKey:@"id"];
        [self fetchNextTwitterPageWithID:cellID];
    }
}

1 个答案:

答案 0 :(得分:0)

根据我对你的代码的理解,这是我对纠正的答案:

-(void)fetchNextTwitterPageWithID:(NSString *)objectID {
    self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/"] key:@"XXXXXXXXXXX" secret:@"XXXXXXXXXXXX"];
    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) {
        NSArray *responseArray = (NSArray *)responseObject;
        [tweets addObjectsFromArray:responseArray];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}