# 为某个BUTTON设置文字： # Object C： API: - (void)setTitle:(NSString *)title forState:(UIControlState)state # 实际用法： [myButton setTitle:@"Clicked!" forState:UIControlStateHighlighted]; # 对比RUBY ： @button.text='Clicked'
# 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.
- This declaration is preceded by a minus (-) sign, which indicates that this is an instance method, as opposed to a class method. It requires an object to call it, and instance variables of the object are available to it inside its definition.
- The (void) indicates the return type. This method doesn’t return anything, so its result can’t be assigned to anything.
- This method name is
- The number of colons (:) indicate how many arguments it takes. T
- The first argument is the string to set the title to.
- The second argument is the button state (such as normal or pressed) in which to set the title.
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.