【问题标题】:Getting error like "Implicit declaration of function 'IS_NOT_NIL_OR_NULL' is invalid in C99"出现类似“函数'IS_NOT_NIL_OR_NULL'的隐式声明在C99中无效”之类的错误
【发布时间】:2015-10-27 04:38:43
【问题描述】:

当我试图限制 coredata 中的 nill 值时,我遇到了类似

的错误

函数“IS_NOT_NIL_OR_NULL”的隐式声明在 C99

这是我的代码:

 if ([[context executeFetchRequest:fetchRequest error:NULL] count] == 0) {
        // Create a new managed object
        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"demo" inManagedObjectContext:context];
         if (IS_NOT_NIL_OR_NULL(self.Name))
    {
        [newDevice setValue:self.Name forKey:@"nameofentry"];
    } else 
     {
        // Handle else case .  get self.name value is null here .

     }

         if (IS_NOT_NIL_OR_NULL(self.WebsiteName))
    {
        [newDevice setValue:self.WebsiteName forKey:@"sitename"];
    } else 
     {
        // Handle else case .  get self.WebsiteName value is null here .

     }


          if (IS_NOT_NIL_OR_NULL(self.Feedlink))
    {
        [newDevice setValue:self.Feedlink forKey:@"urloffeed"];
    } else 
     {
        // Handle else case .  get self.Feedlink value is null here .

     }

        NSError *error = nil;
        // Save the object to persistent store
        if (![context save:&error]) {
            NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
        }

    }

编辑:

【问题讨论】:

  • 什么是IS_NOT_NIL_OR_NULL?你不能只是胡编乱造。
  • 我想限制在 coredata 中输入 NULL 值。我从这个问题中得到了参考。 stackoverflow.com/questions/33339061/…
  • 那一定是某个人创建的宏。它不是标准 API。
  • 尝试使用__nonnull
  • @trick14 _nonnull 不被接受。

标签: ios objective-c core-data


【解决方案1】:

根据引用的问题和您找到IS_NOT_NIL_OR_NULL 引用的答案,我猜一种可能的解决方案是将宏定义为:

#define IS_NOT_NIL_OR_NULL(x) (x && x != (id)[NSNull null])

将此添加到 .m 文件的顶部,紧跟在 import 语句之后,或者将其添加到您可以导入的通用 .h 文件中。

然后代码如:

if (IS_NOT_NIL_OR_NULL(self.WebsiteName)) {

会变成:

if ((self.WebsiteName && self.WebsiteName != (id)[NSNull null])) {

我相信这就是您正在寻找的。​​p>

【讨论】:

  • 收到类似comparision of distinct pointer type (NSstring *) and NSNull的警告
  • 仍然收到此警告。我已在您编辑时更新了#define。
  • 它对我有用,如我的回答所示。复制并粘贴它。
  • 您需要在代码中包含(id) 演员表。我已更新此答案以包含它。
  • @krina_patel 您添加了宏 (#define) 但您没有使用它。
【解决方案2】:

它只是一个宏定义为。

 #define IS_NOT_NIL_OR_NULL(value)           (value != nil && value != Nil && value != NULL && value != (id)[NSNull null])

【讨论】:

  • 检查 nilNilNULL 是多余的。这些都是一样的。您只需要其中之一加上[NSNull null] 支票。
  • 是的,这验证了我所说的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 2013-11-28
  • 2018-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多