Objective-C 使用“类别”接口(interface)向现有类添加新方法:
@interface ClassName (CategoryName)
@end
添加“hello”方法的示例:
@interface XYZPerson (HelloWorld)
- (NSString *)hello;
@end
RubyMotion 没有“类别”,也没有完全相同的接口(interface)。
RubyMotion 代码如何提供类似的功能?
最佳答案
RubyMotion 具有类似的功能。
使用典型的 Ruby 重新打开一个类来添加一个方法:
class XYZPerson
def hello
# return a string
end
end
如果你喜欢模块化代码,或者在多个地方使用相同的代码,你可以使用 Ruby 模块:
module Hello
def hello
# return a string
end
end
class XYZPerson
include Hello
end
请注意,RubyMotion 文件加载顺序很重要。例如,如果您多次定义同一个方法,新定义将覆盖旧定义。
关于ios - 如何将iOS Objective-C类接口(interface)代码翻译成RubyMotion?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25251689/