如何在iOS中检查NSString中的NULL值? [英] How to check the NULL value in NSString in iOS?

查看:108
本文介绍了如何在iOS中检查NSString中的NULL值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 NSString ,我想检查它是否有 NULL 值。如果是,那么 if 条件应该执行。否则它应该执行 else 条件。

I have an NSString and I want to check if it has a NULL value. If it does, then the if condition should execute. Else it should execute the else condition.

以下是我正在使用的代码:

Below is the code which I am using:

if ([appDelegate.categoryName isEqual:[NSNull null]])
{
    select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID=Category.CategoryID where ContentMaster.ContentTagText='%@'", appDelegate.tagInput];
}
else
{
    select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN Category ON ContentMaster.CategoryID=Category.CategoryID LEFT JOIN Topic ON ContentMaster.TopicID=Topic.TopicID where ContentMaster.ContentTagText='%@' && Category.CategoryName='%@' && Topic.TopicName='%@'", appDelegate.tagInput, appDelegate.categoryName, appDelegate.topicName];
}    

它始终执行 else 条件,从不 if 条件,即使值 NULL

It always executes the else condition, and never the if condition, even when the value is NULL.

推荐答案

在Objective-C和Cocoa中,可能没有设置属性 - 即 nil - 或者它可以设置为 nil 的对象表示,它是 NSNull 的实例。您可能想要检查以下任一条件,如下所示:

In Objective-C and Cocoa, the property may not be set—that is, it's nil—or it may be set to the object representation of nil, which is an instance of NSNull. You probably want to check for either of these conditions, like this:

NSString* categoryName = appDelegate.categoryName;
if (categoryName == nil || categoryName == (id)[NSNull null]) {
  // nil branch
} else {
  // category name is set
}

如果 categoryName 这将执行nil分支code>属性设置为 nil (所有属性的默认值),或者是否已显式设置为 NSNull 单身。

This will execute the nil branch if the categoryName property is set to nil (the default for all properties), or if it's been explicitly set to the NSNull singleton.

这篇关于如何在iOS中检查NSString中的NULL值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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