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]}
)
I want to dynamically return that index ie In javascript I would do something like this
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?
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')
valueForKey
returns a nullable object 'id _Nullable'
NOT an NSInteger
which is a long
value.
[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
.
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.