Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift中的ObjC块_Swift_Closures_Block - Fatal编程技术网

swift中的ObjC块

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

有人能帮我用swift重写这段代码吗

    [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的属性,那么您的错误是什么?