NSString是空的

你如何testing一个NSString是否为空? 或全部空白或零? 用一个方法调用?

你可以尝试这样的事情:

@implementation NSString (JRAdditions) + (BOOL)isStringEmpty:(NSString *)string { if([string length] == 0) { //string is empty or nil return YES; } if(![[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) { //string is all whitespace return YES; } return NO; } @end 

查看ADC上的NSString引用。

这就是我使用的扩展NSString:

 + (BOOL)isEmptyString:(NSString *)string; // Returns YES if the string is nil or equal to @"" { // Note that [string length] == 0 can be false when [string isEqualToString:@""] is true, because these are Unicode strings. if (((NSNull *) string == [NSNull null]) || (string == nil) ) { return YES; } string = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; if ([string isEqualToString:@""]) { return YES; } return NO; } 

我用,

 + (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; } 

我讨厌在这个特殊的老火上再添加一个日志,但是我对编辑别人的答案很有兴趣 – 特别是当它是选定的答案的时候。

雅各布问了一个后续问题:我怎样才能用一个方法调用?

答案是,通过创build一个类别 – 基本上扩展了一个基本的Objective-C类的function – 并为所有其他代码编写一个“速记”方法。

然而,从技术上讲,一个带有空白字符的string并不是空的 – 它只是不包含任何可见的字形(在过去的几年中,我一直使用一个名为isEmptyString的方法:在阅读完这个问题之后,和评论集)。

要创build一个类别,请转到选项+单击 – >新build文件…(或文件 – >新build – >文件…或者只是命令+ n) – >selectObjective-C类别。 select一个名称的类别(这将有助于命名空间,并减less可能的未来冲突) – 从“类别”下拉selectNSString – 保存文件的地方。 (注:该文件将自动命名为NSString + YourCategoryName.h和.m。)

我个人感谢Objective-C的自我logging本质; 因此,我在NSString上创build了下面的类别方法,修改我原来的isEmptyString:方法,并select一个更适合声明的方法(我相信编译器稍后压缩代码 – 也许有点太多了)。

标题(.h):

 #import <Foundation/Foundation.h> @interface NSString (YourCategoryName) /*! Strips the string of white space characters (inlcuding new line characters). @param string NSString object to be tested - if passed nil or @"" return will be negative @return BOOL if modified string length is greater than 0, returns YES; otherwise, returns NO */ + (BOOL)visibleGlyphsExistInString:(NSString *)string; @end 

实施(.m):

 @implementation NSString (YourCategoryName) + (BOOL)visibleGlyphsExistInString:(NSString *)string { // copying string should ensure retain count does not increase // it was a recommendation I saw somewhere (I think on stack), // made sense, but not sure if still necessary/recommended with ARC NSString *copy = [string copy]; // assume the string has visible glyphs BOOL visibleGlyphsExist = YES; if ( copy == nil || copy.length == 0 || [[copy stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0 ) { // if the string is nil, no visible characters would exist // if the string length is 0, no visible characters would exist // and, of course, if the length after stripping the white space // is 0, the string contains no visible glyphs visibleGlyphsExist = NO; } return visibleGlyphsExist; } @end 

要调用该方法,请确保将#import NSString + MyCategoryName.h文件到.h或.m(我更喜欢类别.m)类,您正在运行此类validation,并执行以下操作:

 NSString* myString = @""; // or nil, or tabs, or spaces, or something else BOOL hasGlyphs = [NSString visibleGlyphsExistInString:myString]; 

希望涵盖所有的基地。 我记得当我第一次开始为Objective-C开发的时候,类别是其中的一个“是吧? 对我来说是考验 – 但现在我用它们来提高可重用性。

编辑:我想,从技术上讲,如果我们剥离人物,这个:

 [[copy stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0 

真的是所有需要的(它应该做类别方法所做的一切,包括副本),但我可能在这个分数上是错的。

我使用这个定义,因为它与零string以及空string一起工作:

 #define STR_EMPTY(str) \ str.length == 0 

其实现在它是这样的:

 #define STR_EMPTY(str) \ (![str isKindOfClass:[NSString class]] || str.length == 0) 

根据Jacob Relkin的回答和Jonathan的评论:

 @implementation TextUtils + (BOOL)isEmpty:(NSString*) string { if([string length] == 0 || ![[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) { return YES; } return NO; } @end 

应该更容易:

 if (![[string stringByReplacingOccurencesOfString:@" " withString:@""] length]) { NSLog(@"This string is empty"); } 

也许你可以尝试这样的事情:

 + (BOOL)stringIsEmpty:(NSString *)str { return (str == nil) || (([str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]).length == 0); }