如何测试 NSString 是否为零?

发布于 2024-07-13 12:50:25 字数 94 浏览 6 评论 0原文

我可以简单地使用

if(myString == nil)

出于某种原因,我知道为空的字符串导致此语句失败。

Can I simply use

if(myString == nil)

For some reason a string that I know is null, is failing this statement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

半枫 2024-07-20 12:50:26

似乎调试器中的字符串报告为 (null),但这是由于它的分配方式所致,我修复了它,现在它报告为 nil。 这解决了我的问题。

谢谢!

It seems that my string in the debugger was reporting as (null) but that was due to how it was being assigned, I fixed it and now it is reporting as nil. This fixed my issue.

Thanks!

别想她 2024-07-20 12:50:26

您可以隐式检查 nil (已分配,但未初始化):

if (!myString) { 
    //do something
}

如果 myString 是从字典或数组分配的,您可能还希望检查 NSNULL 像这样:

if ([myString isEqual:[NSNull null]]) {
    //do something
}

最后(正如 Sophie Alpert 提到的),您可以检查空字符串(空值):

if ([myString length] == 0) {
    //do something
}

通常,您可能想要合并表达式:

if (!myString || [myString length] == 0) {
    //do something
}

You can implicitly check for nil (allocated, but not initialized) with this:

if (!myString) { 
    //do something
}

If myString was assigned from a dictionary or array, you may also wish to check for NSNULL like this:

if ([myString isEqual:[NSNull null]]) {
    //do something
}

Finally (as Sophie Alpert mentioned), you can check for empty strings (an empty value):

if ([myString length] == 0) {
    //do something
}

Often, you may want to consolidate the expressions:

if (!myString || [myString length] == 0) {
    //do something
}
素染倾城色 2024-07-20 12:50:26

我今天遇到了这个问题。 尽管将字符串指定为 nil:NSString *str = nil;,测试 if (str == nil) 返回 FALSE! 然而,将测试更改为 if (!str) 有效。

I encountered this problem today. Despite assigning a string to be nil: NSString *str = nil;, the test if (str == nil) returned FALSE! Changing the test to if (!str) worked, however.

南城旧梦 2024-07-20 12:50:26

检查 NSAttributedString 是否为空:

let isEmpty = atrributedString.string.isEmpty

Check NSAttributedString is empty:

let isEmpty = atrributedString.string.isEmpty
养猫人 2024-07-20 12:50:25

有没有可能您的字符串实际上不是nil,而只是一个空字符串? 您可以尝试测试是否[myString length] == 0

Is it possible that your string is not in fact nil, and is instead just an empty string? You could try testing whether [myString length] == 0.

我一向站在原地 2024-07-20 12:50:25

您可以在此处找到有关 Objective C 字符串的更多信息。

+ (BOOL ) stringIsEmpty:(NSString *) aString {

    if ((NSNull *) aString == [NSNull null]) {
        return YES;
    }

    if (aString == nil) {
        return YES;
    } else if ([aString length] == 0) {
        return YES;
    } else {
        aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString length] == 0) {
            return YES;
        }
    }

    return NO;  
}

+ (BOOL ) stringIsEmpty:(NSString *) aString shouldCleanWhiteSpace:(BOOL)cleanWhileSpace {

    if ((NSNull *) aString == [NSNull null]) {
        return YES;
    }

    if (aString == nil) {
        return YES;
    } else if ([aString length] == 0) {
        return YES;
    } 

    if (cleanWhileSpace) {
        aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString length] == 0) {
            return YES;
        }
    }

    return NO;  
}

You can find more on objective C string here.

+ (BOOL ) stringIsEmpty:(NSString *) aString {

    if ((NSNull *) aString == [NSNull null]) {
        return YES;
    }

    if (aString == nil) {
        return YES;
    } else if ([aString length] == 0) {
        return YES;
    } else {
        aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString length] == 0) {
            return YES;
        }
    }

    return NO;  
}

+ (BOOL ) stringIsEmpty:(NSString *) aString shouldCleanWhiteSpace:(BOOL)cleanWhileSpace {

    if ((NSNull *) aString == [NSNull null]) {
        return YES;
    }

    if (aString == nil) {
        return YES;
    } else if ([aString length] == 0) {
        return YES;
    } 

    if (cleanWhileSpace) {
        aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString length] == 0) {
            return YES;
        }
    }

    return NO;  
}
半衬遮猫 2024-07-20 12:50:25

你可以使用这个检查你的获取字符串

   if(myString==(id) [NSNull null] || [myString length]==0 || [myString isEqualToString:@""])
    {
     //String is null or bad response
    }

you may check your getting string using this

   if(myString==(id) [NSNull null] || [myString length]==0 || [myString isEqualToString:@""])
    {
     //String is null or bad response
    }
如歌彻婉言 2024-07-20 12:50:25

注意 length = 0 并不一定意味着它是 nil

NSString *test1 = @"";
NSString *test2 = nil;

他们不一样。 虽然两者的长度都是0。

Notice length = 0 doesn't necessary mean it's nil

NSString *test1 = @"";
NSString *test2 = nil;

They are not the same. Although both the length are 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文