Differences between Objective-C methods and functions

Source: Internet
Author: User

Differences between Objective-C methods and functions
The difference between the Objective-C method and the function is that all functions of the object do not depend on the object.

Method Function
-(Void) test; Void test ();
The method starts with minus sign- -
The type must be included in (). -
The declaration must be written between @ interface-@ end and between @ implementation-@ end. It can be written anywhere in the file.
It can only be called by objects -
Access member variables directly You cannot directly access member variables.
Objective-C Member method and class method difference member method is to begin with a minus sign "-" class method with a plus sign "+"
Member Method Class Method
-(Void) test; + (Void) test;
The member method must use the object to call the class method. You can directly use the class name to call the summary OC member method, just like the non-static method in Java, you need to use an object to call the OC class method, just like the static method in Java. You can use the class name to directly call the general tool class. Use the class method to compare multiple methods with return values Objective-C.

Statement

@interface NumUtil : NSObject- (double)pi;@end

Implementation

@implementation NumUtil- (double)pi{    return 3.14;}@end

Call

NumUtil *n = [NumUtil new];NSLog(@"%f",[n pi]);
Java

Java class

This is probably the case.

public class NumUtil{    public double pi(){        return 3.14;    }}

Call

NumUtil numUtil = new NumUtil;numUtil.pi();
Objective-C with a parameter

Statement

@interface NumUtil : NSObject- (int)square:(int)num;@end

Implementation

@implementation NumUtil- (int)square:(int)num;{    return num * num;}@end

Call

NumUtil *n = [NumUtil new];NSLog(@"%d",[n square:2]);
Java

Java class

This is probably the case.

public class NumUtil{    public int square(int num){        return num * num;    }}

Call

NumUtil numUtil = new NumUtil;numUtil.square(2);
Objective-C method with multiple parameters

Statement

@interface NumUtil : NSObject- (int)addNum1:(int)num1 addNum2:(int)num2;     @end

Implementation

@implementation NumUtil- (int)addNum1:(int)num1 addNum2:(int)num2{    return num1 + num2;}@end

Call

NumUtil *n = [NumUtil new];NSLog(@"%d",[n addNum1:1 addNum2:2]);
Java

Java class

This is probably the case.

public class NumUtil{    public int add(int num1, int num2){        return num1 + num2;    }}

Call

NumUtil numUtil = new NumUtil;numUtil. add(1, 2);
Note the Objective-C method name.

The method name of OC, which is somewhat different from that of Java, is well understood by Java.
OC is not hard to understand, just get used to the naming method of Java, it is a bit uncomfortable to see OC

For example:

-(Double) pi;

The method name isPi

-(Int) square :( int) num;

The method name with parameters is a bit special.
First, the colon ":" must be followed by the colonParameters
It can be understood that there are several colons, and there are several parameters.
SetAfter SpaceToBefore ParameterIs the method name.
The method name for this method isSquare:, (Note: There is a colon)

-(Int) addNum1 :( int) num1 addNum2 :( int) num2;

According to the above method
The method name of this method isAddNum1: addNum2:

In fact, it is very easy to understand, that is, to splice the description of each parameter with a colon to form a method name for easy reading.

It is also convenient to view the method name in XCode.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.