如何在If语句中使用NSMutableString作为条件

问题描述:

我有一个webservice和ios应用程序。我用ASIHTTPRequest.h, ASIFormDataRequest.h标题来处理连接到我的PHP脚本/ MySQL数据库如何在If语句中使用NSMutableString作为条件

在我需要发送多个请求给我的web服务和处理每个响应viewcontrollers之一,需要的ViewController的观点和方法,每个请求后刷新。

ASIHTTPRequest只有一个(void)requestFinished:(ASIHTTPRequest *)request事件,所以我需要处理的(void)requestFinished:(ASIHTTPRequest *)request在我的块反应来处理我想出了主意,也许我可以使用一个字符串,当请求完成,我可以在if语句中使用此字符串请求我写了下面的代码,但我的条件我requestfinish方法if ([checkRequest rangeOfString:@"like"].location != NSNotFound)不工作

VoteMe.h

@interface VoteMe : UIViewController<UITextFieldDelegate>{ 
NSMutableString *checkRequest; 
} 
@property (retain, nonatomic) NSMutableString *checkRequest; 

Vote.m

@synthesize checkRequest; 
- (void)viewDidLoad 
{ 
[self showPicture]; 
} 

-(void)showPicture 
{ 
    checkRequest =[NSMutableString stringWithString:@"showPicture"]; 

    //request a random picture url, from server 
    NSURL *url = [NSURL URLWithString:showpicture]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

    [request setDelegate:self]; 
    [request startAsynchronous]; 
} 
-(IBAction)voteLike:(id)sender{ 

    checkRequest =[NSMutableString stringWithString:@"like"]; 

    NSURL *url = [NSURL URLWithString:voteup]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

    [request setDelegate:self]; 
    [request startAsynchronous]; 

    [self showPicture]; 

} 
- (void)requestFinished:(ASIHTTPRequest *)request 
{  

    if ([checkRequest rangeOfString:@"like"].location != NSNotFound) { 
    //do smth 
    } 

    if ([checkRequest rangeOfString:@"showPicture"].location != NSNotFound) { 
    //do someth else 
    } 
} 

以上代码的问题,当-(IBAction)voteLike:(id)sender被调用时应该改变的checkRequest字符串“喜欢”所以当ASIFormDataRequest响应到达时,如果条件可以正常工作

在破发点,我看到checkRequest Variable is not a CFString

当我使用的Nsstring代替NSMutableString它是同样的结果

我知道我需要retain字符串,然后release事后但我不使用alloc应该还是需要retain/release

我该如何实现我的目标?要么有更好的解决方案来检查陈述或修复NSStringNSmutableString以上问题?

当你定义的东西作为一个属性(checkRequest),使用self.checkRequest当你提到它的代码,除非你有一个非常的理由。如果您直接访问变量,那么放在属性语句中的属性将被忽略。

请求结束时,checkRequest不是字符串的原因是因为您永远不会保留checkRequest。您已创建一个保留属性,但在您直接访问实例变量时不会使用它。

对于checkRequest使用你的财产予以保留,你必须写

self.checkRequest = [NSMutableString stringWithString:@"like"];