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

标签 objective-c ios nsstring asihttprequest nsmutablestring

我有一个 web 服务和 ios 应用程序。我使用 ASIHTTPRequest.h、ASIFormDataRequest.h header 来处理与我的 php 脚本/mysql 数据库的连接

在其中一个 View Controller 中,我需要向我的 Web 服务发送多个请求并处理每个响应,并且 View Controller 的 View 和方法需要在每个请求后刷新。

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

投票我.h

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

投票.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 的字符串更改为“like” 所以当响应ASIFormDataRequest 如果条件可以正常工作则到达

在断点处我看到 checkRequest Variable is not a CFString

当我使用 Nsstring 而不是 NSMutableString 时,结果相同

我知道我需要 retain 字符串,然后 release 但我不使用 alloc 我仍然需要 retain/release

我怎样才能实现我的目标?有更好的解决方案来检查 if 语句或修复上面的 NSStringNSmutableString 问题?

最佳答案

当您将某物定义为属性 (checkRequest) 时,在代码中引用它时使用 self.checkRequest 除非您有一个非常 有充分的理由不这样做。如果您直接访问变量,您放置在属性语句中的那些属性将被忽略。

关于objective-c - 如何在 If 语句中使用 NSMutableString 作为条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11319664/

相关文章:

ios - 在后台 Swift 中播放 YouTubePlayer 声音

ios - 显示日历动态并显示月份

arrays - 自定义字符串排序: How do I place these strings in an array in this particular order

ios - NSString 中出现奇怪的竖线字符

ios - Xcode std::__1::basic_ostream undefined symbol

ios - 在父对象内映射对象数组 - Mantle iOS

ios - 以编程方式从带有动画的 customCell 类的 tableView 中删除一行

objective-c - 单例不立即更新变量

ios - 如何向 NSOrderedSet 添加值

iphone - iOS 陀螺仪 API