为什么我的比较if语句不工作? [英] Why is my comparing if statement not working?

查看:165
本文介绍了为什么我的比较if语句不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的代码(在可可中)不工作?

Why is the following code (in cocoa) not working?

NSString *extension = [fileName pathExtension];
NSString *wantedExtension = @"mp3";
if(extension == wantedExtension){
//work
}

在Xcode这只是运行没有警告或错误,但不做我认为它应该做的。

in Xcode this just runs without warnings or errors but doesn't do what I think it SHOULD do.

推荐答案

if ([extension isEqualToString:wantedExtension]) {
...
}

==比较指针。 isEqual:和isEqualToString:比较字符串,虽然isEqualToString是更好的,如果你知道extension和wantedExtension是NSString(在这种情况下你这样做)。

"==" compares the pointers. isEqual: and isEqualToString: compare the strings, although isEqualToString is better if you know both extension and wantedExtension are NSString (which you do in this case).

像我一样的旧的C ++和Java程序员,你可能会更快乐地把一个已知不是null,wantedextension,第一。在目标C中,由于发送消息(即调用方法)返回0或false,因此不必要。

Actually, if you're an old C++ and Java programmer like me, you might be happier putting the one that is known not to be null, "wantedextension", first. In Objective C that is not necessary because "sending a message" (ie calling a method) to a nil returns 0 or false.

if ([wantedExtension isEqualToString:extension]) {
   ...
}

这篇关于为什么我的比较if语句不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆