为什么我的登录条件不起作用?

时间:2011-05-03 14:58:22

标签: objective-c

我正在制作一个简单的登录系统,但它不起作用......为什么?

- (IBAction) login: (id) sender {
    if (username.text == @"Username") {
        …
    }
}

1 个答案:

答案 0 :(得分:6)

使用==比较字符串并不符合您的想法,请参阅此related question。总结一下:

NSString *foo1 = @"foo";
NSString *foo2 = @"foo";
NSString *foo3 = [NSString stringWithString:@"foo"];

foo1 != foo3 // pointer equality
[foo1 isEqual:foo3] // object equality
[foo1 isEqualToString:foo3] // same results as isEqual:, a bit faster
foo1 == foo2 // string literals are optimized to have the same address