在Objective C中检查数据库中的Null字符串的正确方法是什么

时间:2015-06-03 07:37:07

标签: ios objective-c

我想限制数据库在数据库中没有行中的数据而不能显示异常我只想限制Null值

  if ([[Utility checkNullValues:tInfo.product_price]isEqualToString:@""]) {
        [Utility showAlertView:@"Sorry" message:@"No Product You Buy." viewcontroller:self];
      }

这就是错误:

  

2015-06-03 12:35:38.320 NiColi [28363:2438247] *终止应用   由于未被捕获的异常' NSInvalidArgumentException',原因:' *   + [NSString stringWithUTF8String:]:NULL cString'   ***首先抛出调用堆栈:

5 个答案:

答案 0 :(得分:2)

NSString *str = ((char *)sqlite3_column_text(compiledStatement, 1)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)] : nil;

答案 1 :(得分:0)

您可以使用此宏:

#define NULL_VALIDATION(obj) ({ __typeof__ (obj) __obj = (obj); __obj == [NSNull null] ? nil : obj; })

如果传递的对象是nil,则该宏将返回NULL,否则返回对象本身。用法:

if ([NULL_VALIDATION(tInfo.product_price) length] == 0) { 
   [Utility showAlertView:@"Sorry" message:@"No Product You Buy." viewcontroller:self];
}

P.S:不要忘记导入<objc/runtime.h>

答案 2 :(得分:0)

试试这个 -

{{1}}

答案 3 :(得分:0)

我会为你提出最好和最好的解决方案。即使你传递了任何东西,它也会给你很好的答案。所以你可以简单地使用这种方法。

#pragma mark - check string is empty or not
- (void)checkEmpty:(NSString *)check
{
    @try 
    {
      if (check.length==0)
        check = @" ";
      if([check isEqual:[NSNull null]])
        check = @" ";
    }
    @catch (NSException *exception) 
    {
      check = @" ";
    }
}

只需将此方法(带字符串(参数))调用到要将数据发送到数据库的位置。

答案 4 :(得分:-1)

我希望这可以帮助你...

-(BOOL)isNullString:(NSString *)dbString{

    if ([dbString isKindOfClass:[NSNull class]]) {
        return YES;
    }
    else if (dbString == nil){
        return YES;
    }
    else if ([dbString isEqualToString:@""]){
        return YES;
    }
    else if ([dbString isEqualToString:@"null"]){
        return YES;
    }
    else if ([dbString isEqualToString:@"(null)"]){
        return YES;
    }
    else if ([dbString caseInsensitiveCompare:@"NULL"] == NSOrderedSame){
        return YES;
    }
    else if (dbString == 0){
        return YES;
    }
    else{
        return NO;
    }
    return NO;
}

对于空值,它返回 YES ,对于某些值,它返回 NO