Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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
Objective c 消息无法连接到方法_Objective C - Fatal编程技术网

Objective c 消息无法连接到方法

Objective c 消息无法连接到方法,objective-c,Objective C,我对这有点陌生,所以这可能是初学者的错误。问题是,当我向一个方法发送消息时,它没有连接 以下是调用方法: -(BOOL) login:(LoginInfo *) info{ NSString *url = [NSString stringWithFormat:@"%@/%@?name=%@&password=%@", FAMORABLE_API_URL,FAMORABLE_API_ACTION_LOGIN, info.username, info.password]; NSDicti

我对这有点陌生,所以这可能是初学者的错误。问题是,当我向一个方法发送消息时,它没有连接

以下是调用方法:

-(BOOL) login:(LoginInfo *) info{

NSString *url = [NSString stringWithFormat:@"%@/%@?name=%@&password=%@", FAMORABLE_API_URL,FAMORABLE_API_ACTION_LOGIN, info.username, info.password];

NSDictionary* json =  [self getJson:url];
NSString *token = [json objectForKey:@"Token"]; 

NSLog(@"results: %@", token);

LoginInfo *loginResult = [LoginInfo alloc];
loginResult.token = token;
//TODO
NSLog(@"test 1 %@", loginResult.token);
    [clientService saveLoginInfo:loginResult];

return YES;

}
在最后一行上面,您可以看到我正在发送到clientService中的saveLoginInfo,它在clientService.h中声明,并导入到此文件中

-(void) saveLoginInfo:(LoginInfo *)info{
NSLog(@"test 2 %@", info);
NSLog(@"test 3%@", info.token);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];


[defaults setObject:info.token forKey:KEY_TOKEN];
NSString *string = [defaults stringForKey:KEY_TOKEN];
NSLog(@"test 4%@", string);
// save it
[defaults synchronize];


if (!(info.token)){
    self.currentUser = nil;
    self.isLoggedOn=false;
}else{
    self.currentUser = info;
    self.isLoggedOn=true;
}    
}
这是正在调用的方法。我已经发布了一堆日志,如果我做错了,主要是为了安全起见,但是没有一个日志被执行

你知道什么地方可能出了问题吗

提前谢谢
Tom

[clientService saveLoginInfo:loginResult]行中是
clientService
类中的实例变量

如果是这样,请确保在调用对象之前已在某处实例化了该对象。(在类的
init
方法中,或者如果它是
UIViewController
,则可能是
viewDidLoad

例如


(请确保在
dealloc
方法中释放实例。

是否存在
clientService
字段,还是
nil
?(尝试在调用之前记录,例如
NSLog(@“clientService=%@”,clientService);
)在Objective-C中,发送给
nil
值的消息根本不起任何作用。他甚至可以使用延迟初始化,但如果变量为nil,则让getter实例化变量。忘了提到…Backendservice(我从中调用的类)或多或少是一个函数库。它没有视图,我也找不到init方法。init是在NSObject中声明的方法。假设您的后端服务是NSObject的子类,您可以在类中重写init。我将编辑答案以给出一个示例。请您进一步解释一下@the dude?@user15好吗34948当然。假设clientService是类中的一个属性。如果实现getter,它将类似于:-(MyClientService*)clientService{If(clientService==nil){clientService=[[MyClientService alloc]init];}return clientService;}这样,当您第一次调用self.clientService时,如果它没有实例化,它将实例化它然后返回它。之后,它将始终返回创建的实例。这称为延迟实例化或延迟初始化。编辑:注意,您必须使用self.notation(self.clientService)访问属性
- (void)viewDidLoad {
    [super viewDidLoad];
    clientService = [[MyClientService alloc] init];
}

- (id)init {
    self = [super init];
    if (self) {
        clientService = [[MyClientService alloc] init];
    }
    return self;
}