指向整数转换的不兼容指针从结果类型为“NSInteger”(又名“long”)的函数返回“id _Nullable”

安妮123

我想要一个对象,它保留一个数组的索引,其中 id 是唯一键,它的值是 index ( {[id]:[index]})

我想动态返回该索引,即在 javascript 中我会做这样的事情

const a = [{
  id: '451', 
  name: 'varun'
}]


const b = {
    '451': 0 
}

const c = '451' 

if (b[c]) return b[c] 
else return -1 

它在 obj c 中的等价物是什么?

目前我正在这样做

@implementation Participants {
    NSMutableDictionary *participantsKey;
}. // Equivalent to const b above


- (NSInteger)doesParticipantExist:(NSString*)id {
    if ([participantsKey valueForKey: id]) {
      return [participantsKey valueForKey: id];
    } else {
      return -1;
    }
  }

但这正在引发以下警告

指向整数转换的不兼容指针从结果类型为“NSInteger”(又名“long”)的函数返回“id _Nullable”

塔伦泰吉

valueForKey返回一个可为空的对象'id _Nullable'NOT anNSInteger是一个long值。

[participantsKey valueForKey: id]

您的函数的返回类型是NSInteger,这就是为什么它说它不能将可'id _Nullable'空的对象转换为NSInteger.

您可以采取以下措施来解决此问题。

- (NSInteger)doesParticipantExist:(NSString*)id {
    if ([participantsKey valueForKey:id]) {
        // Fix here
        return [(NSNumber*)[participantsKey valueForKey:id] integerValue];
    } else {
        return -1;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章