解析"&引用;和null,并检查iphone中的空字段

解析"&引用;和null,并检查iphone中的空字段,iphone,json,ios5,Iphone,Json,Ios5,下面是我在NSDictionary中获得的JSON输出 谁能说出一种方法来识别“值和空值?我正在尝试检查该值是否为空,并将其分配给UIlabel User = { address = ""; birthday = "<null>"; gender = 0; "mobile_number" = "<null>"; name = ambili; "phone_number"

下面是我在
NSDictionary
中获得的JSON输出

谁能说出一种方法来识别
值和空值?我正在尝试检查该值是否为空,并将其分配给
UIlabel

User =     {
        address = "";
        birthday = "<null>";
        gender = 0;
        "mobile_number" = "<null>";
        name = ambili;
        "phone_number" = "";
        picture = "http://lb.fo********inds.com/images/users/profile_pics/no_picture.jpg";
    };
    status = OK;
}
用户={
地址=”;
生日=”;
性别=0;
“手机号码”=“”;
姓名=ambili;
“电话号码”=“”;
图片=”http://lb.fo********inds.com/images/users/profile_pics/no_picture.jpg”;
};
状态=正常;
}

您可以通过以下方式检查一个字符串是否等于另一个字符串:

if ([myString isEqualToString:@""] || [myString isEqualToString:@"<null>"]) {
    NSLog(@"empty or null");
}
if([myString IsequalString:@”“][myString IsequalString:@”“])){
NSLog(@“空或空”);
}

由于您多次使用此功能,因此它是类扩展的良好候选。使用类扩展时,可以使用自定义函数,如:

if ([birthday hasJSONData])
{
    // Do something
}
为此,创建一个新的NSString类扩展,如下所示:

头文件(NSString+Kurian_Utils.h):

#import <Foundation/Foundation.h>

@interface NSString (Kurian_Utils)

- (BOOL)hasJSONData;

@end
#import "NSString+Kurian_Utils.h"

@implementation NSString (Kurian_Utils)

// If the NSString is nil, calling this function will return NO because it is sent to nil.
 - (BOOL)hasJSONData
{
    if ([self length] == 0)
    {
        return NO;
    }

    if ([self localizedCaseInsensitiveCompare:@"<null>"] == NSOrderedSame)
    {
        return NO;
    }

    return YES;
}

@end
#导入
@接口NSString(Kurian_Utils)
-(BOOL)hasJSONData;
@结束
实现文件(NSString+Kurian_Utils.m):

#import <Foundation/Foundation.h>

@interface NSString (Kurian_Utils)

- (BOOL)hasJSONData;

@end
#import "NSString+Kurian_Utils.h"

@implementation NSString (Kurian_Utils)

// If the NSString is nil, calling this function will return NO because it is sent to nil.
 - (BOOL)hasJSONData
{
    if ([self length] == 0)
    {
        return NO;
    }

    if ([self localizedCaseInsensitiveCompare:@"<null>"] == NSOrderedSame)
    {
        return NO;
    }

    return YES;
}

@end
#导入“NSString+Kurian_Utils.h”
@实现NSString(Kurian_Utils)
//如果NSString为nil,调用此函数将返回NO,因为它被发送到nil。
-(BOOL)hasJSONData
{
如果([自身长度]==0)
{
返回否;
}
if([self-localizedCaseInsensitiveCompare:@”“]==SensorDeredName)
{
返回否;
}
返回YES;
}
@结束

然后只需
#将“NSString+Kurian_Utils.h”
导入到您要使用的文件中
hasJSONData

最后,我找到了一种简单的方法,它运行良好。我很高兴与大家分享

 id phoneString = [Contentds valueForKeyPath:@"User.phone"];
    if (phoneString != [NSNull null])
    {
    label.text=[Contentds valueForKeyPath:@"User.phone"];
    }

使用字符串comparisonalready检查字符串,该字符串具有两个其他比较,如单元格行索引路径和行选择。另外6行的这2个比较使代码非常麻烦。这是一个json值。如果您从后端管理,不允许发送jsoni,那么这将是一个更好的选择。在这个字段上已经有了另外两个比较,另外两个检查是很麻烦的。是否存在任何通用解决方案?对于包含“”的值或空字符串,这将不正确。此外,您正在比较指针值,而不是字符串的实际内容。请不要以这种方式比较字符串,因为这将导致将来非常奇怪和难以跟踪的错误!感谢Inafziger提供正确的信息。但对我来说,它检查我在上面显示的代码中的所有内容都很好。这里的未来风险是什么?当您与==比较时,您正在检查两个对象是否指向内存中的同一位置。这有时适用于不同的对象,因为iOS有时会将具有相同值的对象缓存到同一对象中。但这并不能保证,有时也不能保证,这取决于对象的创建方式。更糟糕的是,这种行为可能在将来的任何时候发生变化,并破坏您的程序,因为它是一种未记录的“特性”。