我已经将UITableViewController子类化了,我正在使用一个按钮将我的表切换到编辑模式。一切都在以这种方式使用Objective-C:
if (self.isEditing) {
[sender setTitle:@"Edit" forState:UIControlStateNormal];
[self setEditing:NO animated:YES];
} else {
[sender setTitle:@"Done" forState:UIControlStateNormal];
[self setEditing:YES animated:YES];
}
但现在我想实现一个Swift版本:
if self.isEditing {
sender.setTitle("Edit", forState: .Normal)
self.setEditing(false, animated: true)
} else {
sender.setTitle("Done", forState: .Normal)
self.setEditing(true, animated: true)
}
我收到了错误:
'MyViewController'没有名为'isEditing'的成员
如果唯一的区别是我使用的是Swift而不是Objective-C,为什么会员失踪?
答案 0 :(得分:3)
实际上,除了您使用的语言之外的所有内容都不相同。 Apple也使用Swift而不是Objective-C来实现UITableViewController及其超类UIViewController。您使用的属性在Objective-C和Swift之间的声明略有不同。两者都命名为editing
,但缺点是:只有Objective-C有一个名为isEditing
的getter。
Objective-C声明是:
@property(nonatomic, getter=isEditing) BOOL editing
但Swift宣言只是:
var editing: Bool
因此,您必须使用editing
代替isEditing
。
来源:iOS开发者库 - 预发布:http://goo.gl/AOoFQl
如果尚未选择:“选择语言:两者”以查看Swift和Objective-C声明。如果您想将代码从Objective-C转换为Swift,这非常有用。