境界和的tableView iOS应用使用Objective-C:线程1个EXC_BAD_ACCESS(代码=,地址=的0x30)
问题描述:
我想我的境界数据库加载到我的tableView和我的代码是这样的:境界和的tableView iOS应用使用Objective-C:线程1个EXC_BAD_ACCESS(代码=,地址=的0x30)
#import "ViewController.h"
#import "customCell.h"
#import "Person.h"
@interface ViewController()
@property RLMRealm *realm;
@property RLMResults *person;
@end
@implementation ViewController
@synthesize realm, person;
- (void)viewDidLoad {
[super viewDidLoad];
Person *one;
one.firstName = @"Allen";
one.lastName = @"X";
realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm addObject:one];
[realm commitWriteTransaction];
self.person = [Person allObjects];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{
return [person count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cellIdentifier";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.firstName.text = [self.person[indexPath.row] firstName];
cell.lastName.text = [self.person[indexPath.row] lastName];
return cell;
}
@end
我想Person.h在这里可以省略。 所以我每次编译和运行它,它会这样说:
我试了一千次,从来没有想出为什么。有人能帮助这个可怜的菜鸟吗?
所以在遵循你的建议后,红色的错误消失了,但是绿色的错误消失了。那就是:
答
您没有初始化,并分配Person对象,然后你想,这样你就明显得到一个访问冲突来填充其属性。
更改为这样的事情:
Person *one = [Person new];
one.firstName = @"Allen";
one.lastName = @"X";
+0
删除断点请张贴另一个问题,这不是一个论坛,但一个问题答案的格式,反正你还在试图访问一个不正确初始化目的 – aleroot
在person.h –