任何人都可以帮我在swift中重写这段代码
[segmentedControl1 setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor blueColor]}];
return attString;
}];
HMSegmentedControl类的一部分:
@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'”
答案 0 :(得分:3)
你用swift和Objective-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
答案 1 :(得分:0)
我理解了我的错误
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