2015-08-27 31 views
3

目前,我们正在使用我们的项目该库只读属性... https://github.com/OliverLetterer/SLExpandableTableView如何符合Objective-C的协议与斯威夫特

一个如何去符合在雨燕UIExpandingTableViewCell协议?

这里有一个副本...

typedef enum { 
    UIExpansionStyleCollapsed = 0, 
    UIExpansionStyleExpanded 
} UIExpansionStyle; 

@protocol UIExpandingTableViewCell <NSObject> 

@property (nonatomic, assign, getter = isLoading) BOOL loading; 

@property (nonatomic, readonly) UIExpansionStyle expansionStyle; 
- (void)setExpansionStyle:(UIExpansionStyle)style animated:(BOOL)animated; 

@end 

我试过以下,但仍表示,它不符合它...

class SectionHeaderCell: UITableViewCell, UIExpandingTableViewCell { 

    @objc var loading: Bool 
    @objc private(set) var expansionStyle: UIExpansionStyle 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
    } 

    required init(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func setExpansionStyle(style: UIExpansionStyle, animated: Bool) { 


    } 
} 

是否因为路UIExpansionStyle是在不使用NS_ENUM的情况下定义的?

混淆

+1

相关http://stackoverflow.com/questions/24151197/getter-and-setter-variable-in-swift – Mats

+0

任何人的任何想法? – kaylanx

回答

1

简答

使用的东西在精神:

var loading:Bool { 
    @objc(isLoading) get { 
     return self._isLoading 
    } 
    set(newValue){ 
     _isLoading = newValue 
    } 
} 

长的答案

创建一个新的,新的项目,做了pod init ,添加一个Podfile类似于下面的那个,并运行pod install

platform :ios, '8.0' 
target 'SO-32254051' do 
pod 'SLExpandableTableView' 
end 

使用Objective-C命名属性属性斯威夫特

由于developer.apple.com文档阅读:

使用@objc(<#名#>)属性提供的Objective-C必要时用于属性和方法的名称。

符合性问题就是这样的:自定义getter名称。

完整的解决方案

采用协议

class TableViewCell: UITableViewCell, UIExpandingTableViewCell { ... } 

定义本地存储

var _isLoading:Bool = false 
var _expansionStyle:UIExpansionStyle = UIExpansionStyle(0) 

实施命名的getter和setter

var loading:Bool { 
    @objc(isLoading) get { 
     return self._isLoading 
    } 
    set(newValue){ 
     _isLoading = newValue 
    } 
} 

private(set) var expansionStyle:UIExpansionStyle { 
    get{ 
     return _expansionStyle 
    } 
    set(newValue){ 
     _expansionStyle = newValue 
    } 
} 

func setExpansionStyle(style: UIExpansionStyle, animated: Bool) { 
    self.expansionStyle = style 
    // ... 
} 

使用枚举速记

你还别说是不是因为UIExpansionStyle不使用NS_ENUM定义的方法是什么?在你的问题。这实际上完全是一个不同的问题,你可以在库中轻松修复,然后执行git push并提出请求。

由于enum未定义为波纹管,因此​​不能使用.Collapsed速记。

typedef NS_ENUM(NSUInteger, UIExpansionStyle) { 
    UIExpansionStyleCollapsed = 0, 
    UIExpansionStyleExpanded 
}; 

,并随后为此在斯威夫特

var _expansionStyle:UIExpansionStyle = UIExpansionStyle.Collapsed 

编译,连接,内置&跑去。

相关问题