2014-01-25 60 views
-3

如何比較布爾型返回soap與布爾值,如「false」。我嘗試了這些:使用「isEqual」比較布爾類型

if ([id isEqual:false]){ 
     [self ShowMsg]; 
    } 

或本

if ([id isEqualToValue:false]) { 
     [self ShowMsg]; 
    } 

或本

if (id == false){ 
     [self ShowMsg]; 
    } 

「標識」 信息來自一個SOAP請求,例如:

SoapRequest* _request = [SoapRequest create: _target action: _action service: self soapAction: @"http://tempuri.org/MyClass" postData: _envelope deserializeTo: @"Boolean"]; 

但是,當我進行這些比較時,它們無論如何都不起作用。

謝謝。

+0

什麼的'SoapRequest'類是什麼樣子?此外,請不要使用帶有bool常量的'=='或'!='... – nhgrif

+3

您實際上在Objective-C代碼中沒有名爲'id'的變量,是嗎?這甚至編譯? 'id'是Objective-C中的一個類型。 – user1118321

+0

感謝您的回覆。不,我使用「id」就像一個例子,但是變量的名字是另一個。 –

回答

1

布爾值是原始值。在Objective-C中它有YES和NO。 如果你想在隨後的真實情況,你的代碼應該是比較布爾值,

if ([yourServerBoolValue boolValue]){ //Compiler understand [yourServerBoolValue boolValue] == YES 
     [self ShowMsg]; 
} 

檢查NO /假條件

if (![yourServerBoolValue boolValue]){ //Compiler understand [yourServerBoolValue boolValue] == NO 
      [self ShowMsg]; 
    } 
+0

感謝您的回覆。這是行不通的。這是完整的類: - (void)ExampleHandler:(id)retorno if(![retorno YES]){ [self showMsg]; } } –

+0

你的代碼與@Reformer的答案不一樣。試試'if(![retorno boolValue])...'。 –

+0

我試過了,但是錯誤是:「預期標識符」: if(![retorno YES]){ [self ExibeAlertaDeEmailJaCadastrado]; } 或者:(![RETORNO真]) 如果{ [自ExibeAlertaDeEmailJaCadastrado] } –

0

布爾的是原語的OBJ-C,對它們進行比較,只需使用==運營商或運營商!=,這裏有一些樣本:

if(booleanName == YES) 
{ 
    //Do something, the boolean is true 
} 

if(booleanName != YES) 
{ 
    //Do something, the boolean is false 
} 
+0

我已經嘗試過使用這個例子,但它不能編譯,錯誤是「隱式轉換int到Id不允許使用ARC」。從我的肥皂網服務收到的回報是@「假」... –

+0

這是完整的: - (void)ExampleHandler:(id)retorno {if(retorno!= YES){[self ShowMsg ]。 }} –