[英]Incompatible pointer to integer conversion returning 'id _Nullable' from a function with result type 'NSInteger' (aka 'long')
I want to have an object which keeps indexes of an array where id is the unique key and it's value is index ( {[id]:[index]}
)我想要一个对象,它保留一个数组的索引,其中 id 是唯一键,它的值是索引( {[id]:[index]}
)
I want to dynamically return that index ie In javascript I would do something like this我想动态返回该索引,即在 javascript 中我会做这样的事情
const a = [{
id: '451',
name: 'varun'
}]
const b = {
'451': 0
}
const c = '451'
if (b[c]) return b[c]
else return -1
What would be it's equivalent in obj c?它在 obj c 中的等价物是什么?
Currently I am doing this目前我正在这样做
@implementation Participants {
NSMutableDictionary *participantsKey;
}. // Equivalent to const b above
- (NSInteger)doesParticipantExist:(NSString*)id {
if ([participantsKey valueForKey: id]) {
return [participantsKey valueForKey: id];
} else {
return -1;
}
}
but this is throwing following warning但这正在引发以下警告
Incompatible pointer to integer conversion returning 'id _Nullable' from a function with result type 'NSInteger' (aka 'long')指向整数转换的不兼容指针从结果类型为“NSInteger”(又名“long”)的函数返回“id _Nullable”
valueForKey
returns a nullable object 'id _Nullable'
NOT an NSInteger
which is a long
value. valueForKey
返回一个可为空的对象'id _Nullable'
不是一个long
值的NSInteger
。
[participantsKey valueForKey: id]
Your function's return type is NSInteger
and that's why it's saying it can't convert a nullable object 'id _Nullable'
into NSInteger
.你的函数的返回类型是NSInteger
,这就是为什么它说它不能将一个可为空的对象'id _Nullable'
转换为NSInteger
。
Here's what you can do to fix the issue.您可以采取以下措施来解决此问题。
- (NSInteger)doesParticipantExist:(NSString*)id {
if ([participantsKey valueForKey:id]) {
// Fix here
return [(NSNumber*)[participantsKey valueForKey:id] integerValue];
} else {
return -1;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.