ios - 如何将iOS Objective-C类接口(interface)代码翻译成RubyMotion?

标签 ios objective-c rubymotion

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/

相关文章:

ios - 为可调整大小的 UIView 设置最小和最大宽度和高度

ios - UIPanGestureRecognizer 拖动 ImageView - 如果我第二次拖动,比例会放大

ios - 快速调试: app stops at breakpoints and doesn't crash

iOS NSXMLParser - 一致地从 XML 标签派生图像源 URL

objective-c - Ruby Motion 在 Ruby 中手动创建语法上类似 Objective-C 的函数

ios - 错误 ITMS-9000 : "This bundle is invalid. Apple 目前不接受构建的应用程序

ios - 错误 : An appName, 密码重置和电子邮件验证功能需要 publicServerURL 和 emailAdapter

ios - 获取日期组件中的当前月份日期

ios - 在 Objective-c 中,我应该使用什么来返回该参数不能为零?

ios - RubyMotion Parse.com 错误