ruby v.s. object-c

发布时间: 2014-10-07 11:17:00

refer to:


# 为某个BUTTON设置文字:
# Object C: API:
- (void)setTitle:(NSString *)title forState:(UIControlState)state

# 实际用法:
[myButton setTitle:@"Clicked!" forState:UIControlStateHighlighted];

# 对比RUBY :

对于 metaprogramming: 

# Object C: 
[world say:@"hello"];
[world performSelector:@selector(say:) withObject:@"hello"];
objc_sendMSG(id object, SEL selector)

# ruby:
world.send(:say, "hello")


So RubyMotion isn’t a superiority thing. I’d certainly prefer to be 100% fluent in objective-c from day one. Apple’s documentation appears to be pretty steller. The problem for me however, is that everything just looks so hard in obj-c. Method declarations are pretty intimidating. So here I go to look up how to set the title of a UIButton and I find - (void)setTitle:(NSString *)title forState:(UIControlState)state. Now come on. That’s a little much to set a title of a button right? A guideline I have always tried to follow with my own code is to only create complicated methods for complicated tasks.

Here’s an example of this method in use: [myButton setTitle:@"Clicked!" forState:UIControlStateHighlighted];. So after you break it down things seems little more reasonable, but it feels like it shouldn’t be this hard.

The specification of a class in Objective-C requires two distinct pieces: the interface and the implementation. The interface portion contains the class declaration and defines the instance variables and methods associated with the class. The interface is usually in a .h file. The implementation portion contains the actual code for the methods of the class. The implementation is usually in a .m file.