swift中的ObjC块
有人能帮我用swift重写这段代码吗swift中的ObjC块,swift,closures,block,Swift,Closures,Block,有人能帮我用swift重写这段代码吗 [segmentedControl1 setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) { NSAttributedString *attString = [[NSAttributedString alloc] initWithString:tit
[segmentedControl1 setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor blueColor]}];
return attString;
}];
HMC分段控制类的一部分:
@interface HMSegmentedControl : UIControl
....
@property (nonatomic, copy) HMTitleFormatterBlock titleFormatter;
....
@end
typedef NSAttributedString *(^HMTitleFormatterBlock)(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected);
我的代码是:
segmentedControl1.titleFormatter = {(segmentedControl: HMSegmentedControl, title: NSString, index: Int, selected: Bool) -> NSAttributedString in
}
我得到一个错误:“'(HMSegmentedControl,NSString,Int,Bool)->NSAttributedString'不能转换为'HMTitleFormatterBlock'”我理解了我的错误
var titleFormatterBlock: HMTitleFormatterBlock = {(control: AnyObject!, title: String!, index: UInt, selected: Bool) -> NSAttributedString in
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor blueColor]}];
return attString;
}
segmentedControl.titleFormatter = titleFormatterBlock
你做了一个快速和客观的组合-C
let titleFormatterBlock: HMTitleFormatterBlock = {(control: AnyObject!, title: String!, index: UInt, selected: Bool) -> NSAttributedString in
let attString = NSAttributedString(string: title, attributes: [NSForegroundColorAttributeName: UIColor.blueColor()
])
return attString;
}
segmentedControl.titleFormatter = titleFormatterBlock
可以显示声明
titleFormatter
属性的代码吗?如上所示。titleFormatter是HMSegmentedControls的属性,那么您的错误是什么?