Iphone 在UITableViewController中取消分配目标C类实例属性
在iPhone应用程序中,我有一个类实例,该类实例定义为全局类,并在UITableViewController的ViewDidLoad中初始化 当它到达cellForRowAtIndexPath时,实例属性被解除分配,并显示在调试器中 正在从数据库加载属性 Foo.hIphone 在UITableViewController中取消分配目标C类实例属性,iphone,objective-c,memory-management,retain,reference-counting,Iphone,Objective C,Memory Management,Retain,Reference Counting,在iPhone应用程序中,我有一个类实例,该类实例定义为全局类,并在UITableViewController的ViewDidLoad中初始化 当它到达cellForRowAtIndexPath时,实例属性被解除分配,并显示在调试器中 正在从数据库加载属性 Foo.h NSString *prop1; @property(nonatomic, retain)NSString *prop1; -(void)shouldLoadProperties; Foo *foo; NSString *_
NSString *prop1;
@property(nonatomic, retain)NSString *prop1;
-(void)shouldLoadProperties;
Foo *foo;
NSString *_prop1;
@property(nonatomic, retain)NSString *prop1;
-(void)shouldLoadProperties;
Foo.m
@synthesize prop1;
-(void)shouldLoadProperties {
<FMDatabase stuff here>
FMResultSet *rs = [self executeQuery:sql];
prop1 = [rs stringForColumn:@"col1"]; //loads db value "Test" into prop1
}
-(void)viewDidLoad {
foo = [[[Foo alloc] init] retain];
[foo shouldLoadProperties];
//Breakpoint here shows that foo.prop1 is set to "Test"
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//foo is is still allocated, but foo.prop1 has been
//deallocated; shows as <freed object>
NSLog(@"Prop 1 is %@", foo.prop1); //throws exception
}
@synthesize prop1 = _prop1;
TestTableViewController.m
@synthesize prop1;
-(void)shouldLoadProperties {
<FMDatabase stuff here>
FMResultSet *rs = [self executeQuery:sql];
prop1 = [rs stringForColumn:@"col1"]; //loads db value "Test" into prop1
}
-(void)viewDidLoad {
foo = [[[Foo alloc] init] retain];
[foo shouldLoadProperties];
//Breakpoint here shows that foo.prop1 is set to "Test"
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//foo is is still allocated, but foo.prop1 has been
//deallocated; shows as <freed object>
NSLog(@"Prop 1 is %@", foo.prop1); //throws exception
}
@synthesize prop1 = _prop1;
这是正确的还是我遗漏了什么?这里的问题是,您没有将
prop1
用作属性,而是用作类中的变量。你可以也应该给这些不同的名字。通常在变量名的开头加下划线:
foo.h
NSString *prop1;
@property(nonatomic, retain)NSString *prop1;
-(void)shouldLoadProperties;
Foo *foo;
NSString *_prop1;
@property(nonatomic, retain)NSString *prop1;
-(void)shouldLoadProperties;
foo.m
@synthesize prop1;
-(void)shouldLoadProperties {
<FMDatabase stuff here>
FMResultSet *rs = [self executeQuery:sql];
prop1 = [rs stringForColumn:@"col1"]; //loads db value "Test" into prop1
}
-(void)viewDidLoad {
foo = [[[Foo alloc] init] retain];
[foo shouldLoadProperties];
//Breakpoint here shows that foo.prop1 is set to "Test"
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//foo is is still allocated, but foo.prop1 has been
//deallocated; shows as <freed object>
NSLog(@"Prop 1 is %@", foo.prop1); //throws exception
}
@synthesize prop1 = _prop1;
现在,要实际使用属性,请使用getter和setter。这将保留您的价值,并在适当时释放它
[self setProp1:[rs stringForColumn:@"col1"]]; //loads db value "Test" into prop1
及
都是有效的,并且彼此相等
_prop1 = [rs stringForColumn:@"col1"]; //loads db value "Test" into prop1
将导致撞车和其他不良行为
您的更新将防止崩溃,但如果您多次这样做,将泄漏内存。这里的问题是您没有将
prop1
用作属性,而是将其用作类中的变量。你可以也应该给这些不同的名字。通常在变量名的开头加下划线:
foo.h
NSString *prop1;
@property(nonatomic, retain)NSString *prop1;
-(void)shouldLoadProperties;
Foo *foo;
NSString *_prop1;
@property(nonatomic, retain)NSString *prop1;
-(void)shouldLoadProperties;
foo.m
@synthesize prop1;
-(void)shouldLoadProperties {
<FMDatabase stuff here>
FMResultSet *rs = [self executeQuery:sql];
prop1 = [rs stringForColumn:@"col1"]; //loads db value "Test" into prop1
}
-(void)viewDidLoad {
foo = [[[Foo alloc] init] retain];
[foo shouldLoadProperties];
//Breakpoint here shows that foo.prop1 is set to "Test"
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//foo is is still allocated, but foo.prop1 has been
//deallocated; shows as <freed object>
NSLog(@"Prop 1 is %@", foo.prop1); //throws exception
}
@synthesize prop1 = _prop1;
现在,要实际使用属性,请使用getter和setter。这将保留您的价值,并在适当时释放它
[self setProp1:[rs stringForColumn:@"col1"]]; //loads db value "Test" into prop1
及
都是有效的,并且彼此相等
_prop1 = [rs stringForColumn:@"col1"]; //loads db value "Test" into prop1
将导致撞车和其他不良行为
您的更新将防止崩溃,但如果您多次更新,将导致内存泄漏。非常好。谢谢你的回复!杰出的谢谢你的回复!