使用Objective-C的Realm和tableView iOS应用程序:线程1 EXC\u BAD\u访问(代码=,地址=0x30)
我想将我的领域数据库加载到我的tableView中,我的代码如下:使用Objective-C的Realm和tableView iOS应用程序:线程1 EXC\u BAD\u访问(代码=,地址=0x30),ios,objective-c,uitableview,realm,Ios,Objective C,Uitableview,Realm,我想将我的领域数据库加载到我的tableView中,我的代码如下: #import "ViewController.h" #import "customCell.h" #import "Person.h" @interface ViewController () @property RLMRealm *realm; @property RLMResults *person; @end @implementation ViewController @synthesize realm, pers
#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 *one = [Person new];
one.firstName = @"Allen";
one.lastName = @"X";
我想这里可以省略人h。
所以每次我编译并运行它时,它都会这样说:
我试了一千次都没弄明白为什么。有人能帮这个可怜的新手吗
因此,在遵循您的建议后,红色错误消失,但绿色错误出现。这是:
您尚未初始化并分配Person对象,然后您正试图填充其属性,因此显然会遇到访问冲突 换成这样:
#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 *one = [Person new];
one.firstName = @"Allen";
one.lastName = @"X";
请发布另一个问题,这不是一个论坛,而是一个问答格式,不管怎样,您仍然试图访问一个未正确初始化的对象。亲自删除断点。h