I have a set of someProtocol objects, I'd like to make a set from a NSString nested property on this object:
// Protocol SomeProtocol
@protocol SomeProtocol<NSObject>
@property(nonatomic, readonly) id<SomeSubProtocol> someSubProtocolObject;
@end
// Protocol SomeSubProtocol
@protocol SomeSubProtocol<NSObject>
@property(nonatomic, readonly) NSString *Id;
@end
I have a set of SomeProtocols:
NSSet<id<SomeProtocol>> *setOfSomeSubProtocols;
I'd like to get a NSSet of the Id properties:
NSSet<NSString *> *idSet = ?; // Calculate from arrayOfSomethings.
I've tried:
idSet = [setOfSomeSubProtocols valueForKeyPath @"someSubProtrocolObject.id"];
But I'd prefer something that throws a compiler error if the properties change...
In Swift, I would have suggested to use
map(), but it doesn't exists in Objective-C. You can search or implement yourself a version ofmapin Objective-C.But, what you can do, is a manual loop, since it's a normal approach of the issue.
It's quite simple and does the job.
You could as said before, implement a
maponNSSet, something like this:@implementation NSSet(Map)
With call: