objective-c 如何测试 NSString 是否为零?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/482276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:05:09  来源:igfitidea点击:

How can I test an NSString for being nil?

iphoneobjective-cstringnull

提问by Kyle Hayes

Can I simply use

我可以简单地使用

if(myString == nil)

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

出于某种原因,我知道一个字符串为空,这个语句失败了。

采纳答案by Kyle Hayes

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.

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

Thanks!

谢谢!

回答by Sophie Alpert

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.

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

回答by karim

You can find more on objective C string here.

您可以在此处找到有关目标 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;  
}

回答by sinh99

you may check your getting string using this

您可以使用此检查您的获取字符串

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

回答by yujingz

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

注意 length = 0 没有必要意味着它是 nil

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

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

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

回答by Steve Harrison

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

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

if (!myString) { 
    //do something
}

If myStringwas assigned from a dictionary or array, you may also wish to check for NSNULLlike this:

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

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

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

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

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

Often, you may want to consolidate the expressions:

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

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

回答by Daniel Smith

Check NSAttributedString is empty:

检查 NSAttributedString 是否为空:

let isEmpty = atrributedString.string.isEmpty

回答by snibbe

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.

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