如何实现 Objective C 类别的块 属性
How to implement block property of Objective C category
鉴于:
@interface NSArray (Sample)
@property (nonnull, nonatomic, readonly) NSArray *_Nonnull (^mapped)(id __nullable (^block)(id __nonnull));
@end
如何实施这一类别?我对这个 block 属性 语法感到困惑。
这解释了类型注释:https://developer.apple.com/swift/blog/?id=25
这是我开始实施的:
@implementation NSArray (Sample)
typedef id __nullable (^block)(id __nonnull);
...
@end
后来试过这个:
@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
稍后还有:
从技术上讲,我认为以上内容将履行扩展的合同,但根据 bbum 的评论,我试图确定创建此类扩展的意图可能是什么。拆开:
- 属性 用于带有闭包参数的闭包,returns NSArray。
- 在实现中,我们根据只读 属性 属性为此闭包创建 getter。
通常我们会 inject/set 带有 setter 的块,但是为了履行合同,我们可以将其构造为实例变量 "someMapped",如下所示。
@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
类型的函数?如果是这样,那么就不需要 @property
,这意味着您正在存储与它们实例相关联的内容。你可能只想要一个方法:
typedef id __nullable (^bt_MapBlockT)(id __nonnull);
- (NSArray *)bt_map:(bt_MapBlockT) block;
// written in browser... probably hosed the syntax slightly.
有大量关于在 NSArray 上实现 map-reduce-filter-whatever 的示例,顺便说一下。
鉴于:
@interface NSArray (Sample)
@property (nonnull, nonatomic, readonly) NSArray *_Nonnull (^mapped)(id __nullable (^block)(id __nonnull));
@end
如何实施这一类别?我对这个 block 属性 语法感到困惑。 这解释了类型注释:https://developer.apple.com/swift/blog/?id=25
这是我开始实施的:
@implementation NSArray (Sample)
typedef id __nullable (^block)(id __nonnull);
...
@end
后来试过这个:
@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
稍后还有:
从技术上讲,我认为以上内容将履行扩展的合同,但根据 bbum 的评论,我试图确定创建此类扩展的意图可能是什么。拆开:
- 属性 用于带有闭包参数的闭包,returns NSArray。
- 在实现中,我们根据只读 属性 属性为此闭包创建 getter。
通常我们会 inject/set 带有 setter 的块,但是为了履行合同,我们可以将其构造为实例变量 "someMapped",如下所示。
@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
类型的函数?如果是这样,那么就不需要 @property
,这意味着您正在存储与它们实例相关联的内容。你可能只想要一个方法:
typedef id __nullable (^bt_MapBlockT)(id __nonnull);
- (NSArray *)bt_map:(bt_MapBlockT) block;
// written in browser... probably hosed the syntax slightly.
有大量关于在 NSArray 上实现 map-reduce-filter-whatever 的示例,顺便说一下。