2015-05-15 2 views
1

Может кто-нибудь помочь мне переписать этот код в быстройObjC блок в стремительной

[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 

     } 

я получаю error: "'(HMSegmentedControl, NSString, Int, Bool) -> NSAttributedString' не конвертируется в 'HMTitleFormatterBlock'"

+0

Можете ли вы показать код, который объявляет свойство 'titleFormatter'? – wltrup

+0

Это показано выше. titleFormatter - свойство HMSegmentedControl – Ilya

ответ

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 
+0

Так в чем была ваша ошибка? –

3

лет сделать микс с быстрым и 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

благодаря своей работе ... –

Смежные вопросы