Back

rubymotion在运行时的过程分析: 2. Object C interface( rubymotion at run-time : object c interface)

发布时间: 2014-10-02 01:01:00

Object C 定义方法的例子:

# define instance method
@class Foo
- (id)foo;
@end

# class method: 
@class Foo
+ (id)foo;
@end

Object C 定义一个方法:sharedInstanceWithObject:andObject

@class Test
+ (id)sharedInstanceWithObject:(id)obj1 andObject:(id)obj2;
@end

如何在 rubymotion中调用它呢?

instance = Test.sharedIntanceWithObject(obj1, andObject:obj2)

RubyMotion还提供了某些 selector: 的简化;

Object C:                RubyMotion
setFoo:                  foo = 
isFoo                    foo?
objectForKey:            []
setObject:forKey:        []=

# 下面是一个使用例子:(view 是 UIView的一个instance) 
view.hidden = true unless view.hidden?

关于 Super: 关键字:

# 下面的 OC代码是 在 VideoController的  play方法中,调用 MediaController的 play 方法:
@interface VideoController : MediaController
@end

@implementation VideoController
- (void)play:(NSString *)song {
  [super play:song];
  // Customize here...
}
@end

# 对应的 ruby 代码如下:
class VideoController < MediaController
  def play(song)
    # 这里用的是 super, 而不是super() . 因为需要调用“同样的参数给super" 
    super  
    # Customize here...
  end
end

Back