Objective c 目标c:如果陈述工作不正常
这不是全部代码,但我正试图让它查看textview框,看看是否已经输入了值。如果不是,我想让它做第一位如果不是,我想让它去其他。。。然而,无论我做什么,它只是崩溃或不工作Objective c 目标c:如果陈述工作不正常,objective-c,Objective C,这不是全部代码,但我正试图让它查看textview框,看看是否已经输入了值。如果不是,我想让它做第一位如果不是,我想让它去其他。。。然而,无论我做什么,它只是崩溃或不工作 -(IBAction) sendPressed{ if((youtubeurl.text = @"")){ //if it doesnt have value NSLog(@"no youtube clip"); youtubelink = @""; }
-(IBAction) sendPressed{
if((youtubeurl.text = @"")){
//if it doesnt have value
NSLog(@"no youtube clip");
youtubelink = @"";
}
else{
//if youtube url has been entered
NSLog(@"youtube clip found");
completeurl = youtubeurl.text;
youtubelink = [completeurl substringFromIndex:16];
}
if语句正在执行赋值。您可能打算让它执行比较。此外,您不应使用
==
来比较NSString
s。试着这样做:
- (IBAction)sendPressed {
if([youtubeurl.text isEqualToString:@""]) {
//if it doesnt have value
NSLog(@"no youtube clip");
youtubelink = @"";
}
else{
//if youtube url has been entered
NSLog(@"youtube clip found");
completeurl = youtubeurl.text;
youtubelink = [completeurl substringFromIndex:16];
}
}
这是正确的if语句。单个=是赋值,而不是相等检查。此外,不能将字符串与==进行比较。这样可以比较指针
另一个选项是检查字符串长度:
if (youtubeurl.text.length > 0)
youtubelink是什么类型的?如果这不是NSString,那么将字符串分配给它是一个坏主意,(这将是youtubelink.text=@“”)可能是的重复项
if (youtubeurl.text.length > 0)