线程Objective-C的参数

时间:2011-12-14 18:22:09

标签: objective-c multithreading

尝试在线程中使用类变量并获取EXC_BAS_ACCESS。 代码段:

@interface ViewController : UIViewController {    
    NSString* accountLoginName;
    NSString* accountPassword; 
}

实施中:

accountLoginName = [NSString stringWithString:textFieldLoginName.text];
accountPassword = [NSString stringWithString:textFieldPassword.text];
[self performSelectorInBackground:@selector(loginAtBackgroundSelector:) withObject:nil]; 


-(void)loginAtBackgroundSelector:(UIAlertView*)alert
{   
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"%@\n%@", accountLoginName, accountPassword);
    [self login];
    [self dismissAlert:alert];
    [pool release];
}

只是尝试写入控制台并在此部分代码中收到错误,但loginAtBackgroundSelector中的错误会不时出现。

-(AlertType)login
{
     NSLog(@"%@\n%@", accountLoginName, accountPassword);
}

1 个答案:

答案 0 :(得分:0)

在界面中尝试此操作:

@interface ViewController : UIViewController {    
    NSString* accountLoginName;
    NSString* accountPassword; 
}
@property(nonatomic, retain) NSString* accountLoginName;
@property(nonatomic, retain) NSString* accountPassword;

这在实现中(在您分配值的位置):

self.accountLoginName = [NSString stringWithString:textFieldLoginName.text];
self.accountPassword = [NSString stringWithString:textFieldPassword.text];
[self performSelectorInBackground:@selector(loginAtBackgroundSelector:) withObject:nil];

另外在dealloc:

-(void)dealloc {
    [accountLoginName release];
    [accountPassword release];
    [super dealloc];
}

如果有帮助,请告诉我。