Objective c 如何实现目标C类的块属性
鉴于: 如何实现这一类别?我被这个块属性语法搞糊涂了。 这解释了类型注释: 这是我开始实施的内容:Objective c 如何实现目标C类的块属性,objective-c,properties,block,categories,Objective C,Properties,Block,Categories,鉴于: 如何实现这一类别?我被这个块属性语法搞糊涂了。 这解释了类型注释: 这是我开始实施的内容: @interface NSArray (Sample) @property (nonnull, nonatomic, readonly) NSArray *_Nonnull (^mapped)(id __nullable (^block)(id __nonnull)); @end 后来我试过: @implementation NSArray (Sample) typedef id __nu
@interface NSArray (Sample)
@property (nonnull, nonatomic, readonly) NSArray *_Nonnull (^mapped)(id __nullable (^block)(id __nonnull));
@end
后来我试过:
@implementation NSArray (Sample)
typedef id __nullable (^block)(id __nonnull);
...
@end
后来:
从技术上讲,我认为上述内容将履行延期合同,但根据bbum的评论,我试图确定创建此类延期的意图可能是什么。分门别类:
@implementation NSArray (Sample)
typedef NSArray *_Nonnull (^mapped)( id __nullable (^block)(id __nonnull) );
-(mapped)mapped {
return ^( id __nullable (^block)(id __nonnull) ){
return @[@"what", @"the", @"heck"];
};
}
@end
不清楚你想做什么。您是否试图在
NSArray
上实现map
类型函数?如果是这样,那么就不需要@属性
,这意味着您正在存储与它们实例关联的内容。您可能只需要一个方法:
@implementation NSArray (Sample)
typedef NSArray *_Nonnull (^mapped)( id __nullable (^block)(id __nonnull) );
-(mapped)mapped {
//Normally someMapped block definition would be injected/set by the setter -(void) setMapped:(mapped) aMapped {
mapped someMapped = ^(id __nonnull someId) {
NSMutableArray * new = [[NSMutableArray alloc] init];
for( NSMutableDictionary* dict in self) {
NSMutableString * str = [dict objectForKey:someId];
[str stringByAppendingString:@".png"];
[new addObject:str];
}
return [new copy];
};
return someMapped;
}
@end
顺便说一句,NSArray上有大量实现map reduce filter的示例。这是一个非常有用的块语法参考@particleman是的,我已经看到了这一点,但我仍然没有在我的OP中得到它。它与@interface声明中所述的属性完全相同。我在一次编码挑战中遇到了这样一个问题,所以我也在试图弄清楚它想做什么。但是改变意图不是答案。@bhartsb那么关于挑战的问题是错误的。这种情况经常发生。正确的答案是改变意图,因为意图是错误的。绝对没有理由存储块,因此,使用
@属性
是不正确的。@bhartsb请将编码挑战的作者推荐给我。我很乐意帮助他们纠正这个挑战。块名称映射应该被映射,但我认为这不会有任何区别。我会在LinkedIn上给你发PM。问题是“实现以下类别”和“特别注意界面:这是你需要满足的合同”。我修改了我的OP,以显示我认为符合合同要求的替代方式,但这样做对我来说并不熟悉。因为我不明白它的意思。
typedef id __nullable (^bt_MapBlockT)(id __nonnull);
- (NSArray *)bt_map:(bt_MapBlockT) block;
// written in browser... probably hosed the syntax slightly.