Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我经常需要检索属于 Set 的第一个对象。(使用该对象作为该集合的代表。)
我设想一个集合对象运算符,类似于
@unionOfObjects
但很明显
@firstObject
是否可以创建这样的 Collection 运算符!
目前无法定义自定义集合运算符。然而,由于一些内部魔法,有一个有趣的解决方案:
NSSet *testSet = [NSSet setWithArray:@[@"one", @(1)]]; id object = [testSet valueForKey:@"@anyObject"]; NSLog(@"anyObject (%@): %@", NSStringFromClass([object class]), object);
UPD:忘了提到另一个方便的技巧:你可以@lastObject使用NSArray!
@lastObject
NSArray