相关疑难解决方法(0)

在Objective-C中测试nil - if(x!= nil)vs if(x)

我在网上找到的大多数例子都写道:

if(x != nil)
    // ...
Run Code Online (Sandbox Code Playgroud)

这有什么问题吗?

if(x)
    // ...
Run Code Online (Sandbox Code Playgroud)

我在一个简单的程序中尝试了两个,但没有找到任何区别.

syntax objective-c

71
推荐指数
2
解决办法
4万
查看次数

如何检测NSString是否为空?

我有一段代码检测如果NSStringNULL,nil等等.然而,它崩溃.这是我的代码:

NSArray *resultstwo = [database executeQuery:@"SELECT * FROM processes WHERE ready='yes' LIMIT 0,1"];
for (NSDictionary *rowtwo in resultstwo) {

NSString *getCaption = [rowtwo valueForKey:@"caption"];

if (getCaption == NULL) {
theCaption = @"Photo uploaded...";
} else if (getCaption == nil) {
theCaption = @"Photo uploaded...";
} else if ([getCaption isEqualToString:@""]) {
theCaption = @"Photo uploaded...";
} else if ([getCaption isEqualToString:@" "]) {
theCaption = @"Photo uploaded...";
}

}
Run Code Online (Sandbox Code Playgroud)

这是错误:

因未捕获的异常而终止应用' NSInvalidArgumentException',原因:' -[NSNull isEqualToString:]:无法识别的选择器发送到实例 …

null objective-c nsnull

54
推荐指数
3
解决办法
8万
查看次数

如何在iOS中检查NSString中的NULL值?

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

以下是我使用的代码:

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];
}    
Run Code Online (Sandbox Code Playgroud)

它总是执行else条件,而不是if条件,即使值是NULL.

null nsstring ios

6
推荐指数
1
解决办法
3万
查看次数

标签 统计

null ×2

objective-c ×2

ios ×1

nsnull ×1

nsstring ×1

syntax ×1