isEqualToString不返回true [英] isEqualToString not returning true

查看:102
本文介绍了isEqualToString不返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使您在运行程序时键入停止,也不会打印停止语句。 initWithUTF8String:方法是否引入了额外的格式?

The "Stopping" statement never gets printed even if you type 'stop' when running the program. Is the initWithUTF8String: method introducing extra formatting?

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    char holderText[256];

    fgets(holderText, 256, stdin);
    NSString *words = [[NSString alloc] initWithUTF8String:holderText];


    if ([words isEqualToString:@"stop"]) {

        NSLog(@"STOPPING");

    }
    NSLog(@"This is what you typed: %@", words);

    [pool drain];
    return 0;
}


推荐答案

即使代码看起来也不错尽管您泄漏了 words 字符串。您需要在该分配调用的末尾添加 [自动释放]

The code looks fine, even though you're leaking the words string. You need to add an [autorelease] on the end of that alloc call.

您可以尝试 initWithCString ,并修剪新行和空白。

You could try initWithCString and also trim new lines and surrounding whitespace.

NSString *words = [[[NSString alloc] initWithCString:holderText] autorelease];
words = [words stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

这篇关于isEqualToString不返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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