Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-访问API返回的数组会给EXC\u BAD\u访问权限(代码=1)_Ios_Objective C_Memory Management_Exc Bad Access - Fatal编程技术网

iOS-访问API返回的数组会给EXC\u BAD\u访问权限(代码=1)

iOS-访问API返回的数组会给EXC\u BAD\u访问权限(代码=1),ios,objective-c,memory-management,exc-bad-access,Ios,Objective C,Memory Management,Exc Bad Access,我正在尝试在iOS应用程序中创建一个类似Facebook/9gag的评论页面。简单地说,在列表视图中会有一个图像列表,并且会有一个评论按钮,当用户按下它时,会显示一个评论列表视图,就像在Facebook和9gag中一样 在我的项目中,我创建了几个类来实现这一点,它们是: APIOperator,它定义要调用的api的路径,并在向服务器发出请求时调用 MyPhotoCell,它是一个自定义UITableViewCell类,构成照片列表视图,由一个图像和一个注释按钮组成 CommentViewCon

我正在尝试在iOS应用程序中创建一个类似Facebook/9gag的评论页面。简单地说,在列表视图中会有一个图像列表,并且会有一个评论按钮,当用户按下它时,会显示一个评论列表视图,就像在Facebook和9gag中一样

在我的项目中,我创建了几个类来实现这一点,它们是:

  • APIOperator,它定义要调用的api的路径,并在向服务器发出请求时调用
  • MyPhotoCell,它是一个自定义UITableViewCell类,构成照片列表视图,由一个图像和一个注释按钮组成
  • CommentViewController,它是显示的注释列表的控制器
  • CommentCell,它是一个自定义UITableViewCell,构成显示的注释列表
  • 以下是有关该问题的代码段:

    首先,当用户按下其中一个MyPhotoCell中的comment按钮时,它将调用CommentListViewController的创建:

    //MyPhotoCell.m
    - (void)click_btn_comment:(id)sender 
    {
        CommentViewController *comment_view = [[CommentViewController alloc] initWithNibName:@"CommentViewController" bundle:nil];
        comment_view.targetPhotoid = self.photoItem.photoid;
        [[MyAppCoreData coreData].navController pushViewController:comment_view animated:YES];
    }
    
    以下是:

    //CommentViewController.h
    @property (assign, nonatomic) NSInteger targetPhotoid;
    @property (strong, nonatomic) NSMutableArray *arr_commentList;
    
    //CommentViewController.m
    @interface SecondCreation_CommentViewController ()<UITableViewDataSource, UITableViewDelegate>
    {
       ASIHTTPRequest *request_comment;
    }
    
    @property (strong, nonatomic) UITableView *tbl_comment;
    @property (strong, nonatomic) NSNumber *page;
    
    @end
    
    - (void)viewWillAppear:(BOOL)animated
    {
        if(!self.tbl_comment){
            self.tbl_comment = [[UITableView alloc] init];
            self.tbl_comment.delegate = self;
            self.tbl_comment.dataSource = self;
    
            self.tbl_comment.frame = CGRectMake(0, 55, PHOTOWIDTH, SCREEN_HEIGHT - 100);
            [self.view addSubview:self.tbl_comment];
        }
    
        if([NSArray checkEmptyArray:self.arr_commentList]){
            self.page = 0;
            [self sendRequest:1];
        }
    
    }
    
     - (void)addDisplayData:(NSArray *)arr_addData withReset:(BOOL)reset
    {
        //NSLog here shows arr_addData has a count of 6 (6 comments inside, which is normal)
        //NSLog here shows arr_commentList has a count of 0 (which is also expected)
        if (reset) {
            [self.arr_commentList removeAllObjects];
        }else {
            if (!self.arr_commentList) {
                self.arr_commentList = [NSMutableArray array];
            }
        }
        [self.arr_commentList addObjectsFromArray:arr_addData];
        //NSLog here shows arr_addData still has a count of 6
        //NSLog here shows arr_commentList has a count of 6
    }
    
    - (void)sendRequest:(NSInteger)page
    {
        __block typeof(self)blockSelf = self;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            blockSelf -> request_comment = [APIOperator getCommentListForPhoto:@(self.targetPhotoid) andPage:page WithCompletionHandler:^(APISTATUS apiStatus, CommentListItem *commentListItem, ASIHTTPRequest *request) {
                if (request != blockSelf -> request_comment) {
                    return;
                }
                dispatch_async(dispatch_get_main_queue(), ^(void) {
                    if (apiStatus == APISTATUS_SUCCESS) {
                        //I tried to access and print out the content of the returned commentList here, and it gives EXC_BAD_ACCESS (code = 1 ......)
                        //e.g. NSLog(((CommentItem *)[commentListItem.commentList objectAtIndex:0]).message);
    
                        //I tried to assign the returned commentList to my controller through various ways like:
                        //[blockSelf.arr_commentList addObjectsFromArray:commentListItem.commentList];
                        //[blockSelf.arrcommentList = commentListItem.commentList;
                        //[blockSelf addDisplayData:commentListItem.commentList withReset:YES];
                        //I tried to access the content of controller's arr_commentList, it will give EXC_BAD_ACCESS (code = 1, .....) too
                    } else {
    
                    }
                });
            }];
        });
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *identifier = @"commentcell";
        CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
        if(!cell)
            cell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    
        //acessing the local arr_commentList here will also gives EXC_BAD_ACCESS (code = 1.....)
    
        return cell;
    }
    

    我在互联网上搜索,发现EXC_BAD_ACCESS code=1是关于访问/释放一个之前已经释放/释放的变量,但我仍然无法在这里找到问题所在。我找不到它在哪里发布/取消发布,为什么它在发布/取消发布时仍然给我6的计数。希望我能在这里得到一些关于是什么导致这个问题以及如何处理它的帮助,因为我已经处理这个问题很长时间了,但仍然不知道问题是什么以及该怎么办。非常感谢

    您应该为当前的开发方案启用僵尸对象,这将准确地告诉您正在访问哪个发布的对象。它可能不是您想象的那样。我启用了zombie对象,并向释放的实例0xda4ba00发送了***-[CFString retain]:消息,因此我猜重新部署的commentListItem.commentList在我在控制器中访问它时会被释放?commentList是字符串吗?如果不是,那就不太可能发布什么。在启用僵尸的情况下,它在哪一行出现?这可能看起来很愚蠢,但是你的
    CommentItem
    对象对
    消息的引用是否很弱?哦,我的天哪!我刚刚检查了我的类,发现我将消息设置为
    assign
    ,我认为我将其设置为
    strong
    。将其更改为
    strong
    则一切正常。真的非常感谢!
    //APIOperator.m
    + (ASIHTTPRequest *)getCommentListForPhoto:(NSNumber *)photoid andPage:(NSInteger)page WithCompletionHandler:(void(^)(APISTATUS apiStatus, CommentListItem *apiItem, ASIHTTPRequest *request))completionBlock
    {
        NSString *str_page = [NSString stringWithFormat:@"%@", @(page)];
        //API_URL here is the url to call the api in the server
        NSString *str_API = [NSString stringWithFormat:@"%@",API_URL];
        ASIHTTPRequest *_request = [ASIHTTPRequest createAPIRequest:str_API];
    
        __block ASIHTTPRequest *request = _request;
        __block void(^tempBlock)(APISTATUS apiStatus, CommentListItem *apiItem, ASIHTTPRequest *request) = completionBlock;
        [_request setCompletionBlock:^{
    
            if ([request responseStatusCode] == 200 || [request responseStatusCode] == 302 || [request responseStatusCode] == 304) {
    
                DEBUGMSG(@"%d %@",[request responseStatusCode], request.url)
                JSONParse *jsonParse = [[JSONParse alloc] initWithData:[request responseData]];
                CommentListItem *obj = (CommentListItem *)[jsonParse parser2OneObject:@"CommentListItem" withPath:nil];
    
                //I tried to print out the contents of the comments here, and everything are as expected
                NSLog(((CommentItem *)[obj.commentList objectAtIndex:0]).message);
    
                jsonParse = nil;
                if (tempBlock) {
                    tempBlock(APISTATUS_SUCCESS,obj,request);
                }
    
                tempBlock = nil;
                request = nil;
    
            } else {
    
                DEBUGMSG(@"%d %@",[request responseStatusCode], request.url)
                if (tempBlock) {
                    tempBlock(apiStatus,nil,request);
                }
                tempBlock = nil;
                request = nil;
            }
    
        }];
        [_request setFailedBlock:^{
    
            DEBUGMSG(@"%d %@",[request responseStatusCode], request.url)
            if (tempBlock) {
                tempBlock(apiStatus,nil,request);
            }
            tempBlock = nil;
            request = nil;
    
        }];
        [_request performSelector:@selector(startAsynchronous)];
    
        return _request;
    }