廢話少說,直接上代碼
subscribeNext:^(NSNumber *x) {
self.loginBtn.enabled = YES;
** BOOL sucess = x;**
if (sucess) {
LELWelcomViewController *vc = [[LELWelcomViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
}else{
NSLog(@"signIn result is %@", x);
}
在上面的代碼中,
NSNumber *x 被傳遞過來的時候是 0,但是當它賦值給BOOL success 時, success 的值變為 1 了,這與預想轉換成 0 的結果剛好相悖.
這里就產生問題啦 ,為什么x 在賦值給 success 時會被轉換成1呢?
經過一番思考后得出答案,BOOL值進行轉換時,其基準是判斷對象是否存在,如果對象存在的時候,即為1,不存在,則為0; 而 NSNumber 恰好就是一個對象,所以即使它為0的情況下,在編譯器的眼里依然視為對象存在,被轉換過成1.
因此在這里要想將0轉換成 NO, 必須先將 NSNumber 類型的 IntegerValue取出來,然后賦給 success.
改造后,如下
subscribeNext:^(NSNumber *x) {
self.loginBtn.enabled = YES;
** BOOL sucess = x.integerValue;**
if (sucess) {
LELWelcomViewController *vc = [[LELWelcomViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
}else{
NSLog(@"signIn result is %@", x);
}
因此,以后進行條件是0和1的判斷時,切記將對象類型轉換成值類型,否則會得到相反的結果.