Cog/Utils/NSDictionary+Optional.m
Christopher Snowhill 01ea0435d0 Metadata: Add helper for optional values
Add NSDictionary initWithOptionalObjects:forKeys:count: helper, to work
around several places that may have been setting nil dictionary valies
on placeholder dictionaries. Hopefully this fixes the crash someone
logged recently.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
2025-06-20 19:44:00 -07:00

15 lines
482 B
Objective-C

#import "NSDictionary+Merge.h"
@implementation NSDictionary (Optional)
+ (NSDictionary *)initWithOptionalObjects:(const id _Nonnull [_Nullable])objects forKeys:(id<NSCopying> const[])keys count:(NSUInteger)cnt {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
for(NSUInteger i = 0; i < cnt; ++i) {
if(keys[i] && objects[i]) {
[dictionary setObject:objects[i] forKey:keys[i]];
}
}
return [NSDictionary dictionaryWithDictionary:dictionary];
}
@end