What is the synonym of this function in obj-c?
Hi all What is the synonym of this f开发者_StackOverflow中文版unction in obj-c?
void FuncOne (int a , int b);
Thanks
Short answer is:
void FuncOne (int a , int b);
Any valid C code is also valid Objective-C. As a bonus any valid C++ code is also valid Objective-C++.
For a longer answer I would like to use a better function example using a Java class as example:
public class Rect {
public void setColor(int color, boolean animated);
}
This class and it's method would in proper Objective-C be:
@interface Rect : NSObject {
}
-(void)setColor:(int)color animated:(BOOL)animated;
@end
I am sure you can figure out the conversion, it maps quite well 1 to 1. What you have to take care of in the Objective-C world is that each and every method argument is named. This might seem like a small thing but is there to improve the readability of your code by a factor of 1000! Take using the above example in Java as an example:
myRect.setColor(Color.RED, true);
What does the second argument mean? Is it enabling the color, does it signal that the color has an alpha component that should be respected (True for most JavaME APIs), or that the color should be set in an animated fashion? You can not be sure unless you look it up in the documentation.
For proper Objective-C you never have this problem:
[myRect setColor:RED animated:YES];
You have to be quite stupid not to understand what is going on just by reading the code as it is.
Your hardest task in learning Objective-C will not be to learn to convert whatever you know now 1 to 1 into Objective-C. Your hardest task will be to learn the mindset of Objective-C that allows you to be far more productive.
If you want to use a "standalone" function in objective-c code then you must just use plain c function as you posted in your question.
If you ask about class methods then their syntax is the following (parameterNames are optional):
// Instance method
- (ReturnType) methodName:(ParameterType1)parameter1 parameterName2:(ParameterType2)parameter2;
// Class method
+ (ReturnType) methodName:(ParameterType1)parameter1 parameterName2:(ParameterType2)parameter2;
- (void)funcOne:(int)a :(int)b
精彩评论