0

我正在创建一个多用户 iPhone 应用程序,并且正在尝试完成用户登录过程中的编码。我可以成功创建一个帐户,并将用户输入的数据存储到 Core Data DB 中,并将 pin 存储到 Keychain 中,所以现在我正在尝试完成登录过程。下面列出的以下代码是我到目前为止所拥有的,我收到一个错误,“帐户”没有可见的@interface 声明选择器“密码:”

- (IBAction)processLogin:(id)sender {

// hide keyboard
[_textFieldUsername resignFirstResponder];
[_textFieldPin resignFirstResponder];


// First - make activity indicator visible, then start animating, then turn of wrong user / pin label
_welcomeActivityIndicator.hidden = FALSE;
[_welcomeActivityIndicator startAnimating];
[_wrongUserPin setHidden:YES];

// check if username and pin text fields are populated
if ([_textFieldUsername.text length ] == 0 &&  [_textFieldPin.text length ] == 0)
{
    [_welcomeActivityIndicator stopAnimating];
    [_wrongUserPin setHidden:NO];   
}

// CORE DATA
// NSManagedObjectContext *context = _managedObjectContext;

NSFetchRequest *request= [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:_managedObjectContext];
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"username=%@",self.textFieldUsername.text];

// TODO check pin
Account *pinAccount = [[Account alloc] init];

// GETTING ERROR ON BELOW LINE OF CODE! - ERR0R - No visible @interface for 'Account' declares the selector 'password:'
[pinAccount password:_textFieldPin.text];


[request setEntity:entity];
[request setPredicate:predicate];

NSError *error = nil;

NSArray *array = [_managedObjectContext executeFetchRequest:request error:&error];
if (array != nil) {
    NSUInteger count = [array count]; // may be 0 if the object has been deleted.
    NSLog(@"Username may exist, %@",count);
}

else {
    NSLog(@"Username does not exist.");
}
}

Account 类文件mh如下所示:

帐户.m

- (NSString*)password 
{
if (self.username)
    return [KeychainHelper getPasswordForKey:self.username];
return nil;
}

- (void)setPassword:(NSString*)aPassword 
{
if (self.username) [KeychainHelper setPassword:aPassword forKey:self.username];


}
- (void)prepareForDeletion
{
if (self.username) [KeychainHelper removePasswordForKey:self.username];
}
@end

帐户.h

#import "AccountBase.h"

@interface Account : AccountBase {

}

// nonatomic - don't worry about multithreading

@property (nonatomic, assign) NSString *password;

- (void)setPassword:(NSString*)aPassword;

@end

我将不胜感激任何想法或想法,并感谢您的阅读。

4

3 回答 3

0

嗯,小错字?采用:

[pinAccount setPassword:_textFieldPin.text];

代替:

[pinAccount password:_textFieldPin.text];

? 或者我可能遗漏了一些东西......

[更新]

  • 您正在调用一个名为(NSString *)password的选择器。
  • 您的错误消息说选择器在界面中不可见。(我认为这意味着您打错了字,实际上是指“setPassword”。)

相反,我认为错误是在您的 .h 文件中,(NSString *) 密码选择器未在界面中定义,因此任何外部代码都看不到它。

即您可以将此行添加到您的 .h 文件中:

- (NSString *)password;

(我可能会将选择器从密码重命名为不同的名称,以免与属性混淆。)

于 2012-06-24T06:10:30.877 回答
0

它没有调用 Account.h 接口中声明的方法。并且[pinAccount password:_textFieldPin.text];正在调用一个与Account 的属性共享同名的函数,即该属性的getter。

于 2012-06-25T06:17:02.140 回答
0

我能够使用以下代码检查/验证引脚。

// CORE DATA
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:_managedObjectContext];

// set entity for request
[request setEntity:entity];

// filter results using a predicate
NSPredicate *pred =[NSPredicate predicateWithFormat:(@"username = %@"), _textFieldUsername.text];

// set predicate for the request
[request setPredicate:pred];

NSError *error = nil;

// store DB usernames in results array
NSArray *results = [_managedObjectContext executeFetchRequest:request error:&error];

NSLog(@"The returned results are %@",results);


// check text field against results stored in DB
for (Account *anAccount in results) {
    if ([anAccount.username isEqualToString:_textFieldUsername.text]){
        NSLog(@"Your username exists");
        if ([anAccount.password isEqualToString:_textFieldPin.text]){
            NSLog(@"Your pin is correct");
于 2012-06-28T02:28:19.903 回答