gpt4 book ai didi

objective-c - 向 NSMutablearray 添加数据时出现 EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-11-28 20:30:54 24 4
gpt4 key购买 nike

好吧,我正在解析来自 json 的数据,并将它们动态添加到数组中。如果我添加其中的 12 个数据,就可以了。但是当我要添加第 13 个时,我得到这个错误(不管第 13 个是什么):

EXC_BAD_ACCESS

这是我的代码:

HomeView.h

#import <UIKit/UIKit.h>

@interface HomeView : UIViewController{
NSMutableData *responseData;
}

@property (nonatomic, strong) NSMutableData *responseData;

@end

和我的实现文件:

@implementation HomeView
@synthesize responseData;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);

// convert to JSON
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
NSString *parsed_track=[res objectForKey:@"data1"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data2"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data3"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data4"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data5"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data6"];
[tracks_condition addObject:parsed_track];

parsed_track=[res objectForKey:@"data7"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data8"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data9"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data10"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data11"];
[tracks_condition addObject:parsed_track];
parsed_track=[res objectForKey:@"data12"];
[tracks_condition addObject:parsed_track];

// parsed_track=[res objectForKey:@"data13"]; //HERE COMES THE ERROR
// [tracks_condition addObject:parsed_track];

我想这与内存管理有关,但我找不到。

最佳答案

Are you sure there is an object for key @"data13" 也许这个键不存在并返回nil。在这种情况下,您需要检查 nil 并决定是否添加它。如果您想添加它,只需添加 [NSNull null] 即可,因为您必须使用继承 NSObject 的对象。

编辑:请也尝试在 for 循环中使用它并使用 [NSString stringWithFormat.....]

构造字符串

关于objective-c - 向 NSMutablearray 添加数据时出现 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com