Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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
Iphone 在UITableViewController中取消分配目标C类实例属性_Iphone_Objective C_Memory Management_Retain_Reference Counting - Fatal编程技术网

Iphone 在UITableViewController中取消分配目标C类实例属性

Iphone 在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 *_

在iPhone应用程序中,我有一个类实例,该类实例定义为全局类,并在UITableViewController的ViewDidLoad中初始化

当它到达cellForRowAtIndexPath时,实例属性被解除分配,并显示在调试器中

正在从数据库加载属性

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;
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
将导致撞车和其他不良行为


您的更新将防止崩溃,但如果您多次更新,将导致内存泄漏。

非常好。谢谢你的回复!杰出的谢谢你的回复!