简体   繁体   中英

What is the diffrence between void and +(void) in iphone

I have been using different methods that are used in Objective-C. Can any one give a good explanation of the difference between following methods?

void append(NSString *msg);
-(void) append:(NSString *)msg;
+(void)append:(NSString *)msg)

void append(NSString *msg) is a C function. Unlike Objective-C methods, C functions are called using parentheses rather than the Objective-C bracket notation. C functions are often seen in iOS in lower-level components and frameworks, such as the graphics libraries.

-(void) append:(NSString *)msg is an instance method. This means, that the method must be called on an instance of whatever class it has been written into.

This differs from +(void) append:(NSString *)msg , which is a class method. This means that the method must be called on the class itself, not on any single instance of the class. Class methods are usually reserved for utility methods that are general in nature, and not instance-specific.

-(void) append:(NSString *)msg; 

is an instance method.

+(void) append:(NSString *)msg;

is a Class method.

void append(NSString *msg);

is a C function.

void append(NSString *msg); // c function
-(void) append:(NSString *)msg; // instance method
+(void)append:(NSString *)msg;// class method

This denotes an instance method. You must hold a valid instance of the class to call this method.

-(void)

This denotes a class method. You do not need an instance of the class to call this method.

+(void)
void append(NSString *msg); // C Style function declaration.

-(void) append:(NSString *)msg;

it is the instance method that can be called by the class instance like.

you have NSString class object like.

NSString *strObj=@"hi";

To make strObj in uppercase you call NSString class method - (NSString *)uppercaseString;

[strObj uppercaseString]

+(void)append:(NSString *)msg)

it is the class method or Static Method. Example:NSString *strObj1=[NSString string];

Here: string method is class method and it is declared as: + (id)string; That will return autoreleased string.

-(void) append:(NSString *)msg; // IT is instans method it's always call with object of class.

+(void)append:(NSString *)msg;//it is class method always call with class name .

for ex. ' alloc ' is a class method call with class name.like

[ClassName alloc];


void append(NSString *msg); it is a cFunction.

Imagine you have a Test-Class like this

@interface Test : NSObject

// c function
void append(NSString *msg);

// instance method
- (void)append:(NSString *)msg;

// class method
+ (void)append:(NSString *)msg;

@end

Then you can implement your functions like that:

#import "Test.h"

@implementation Test

void append(NSString *msg)
{
    // there is no self inside of a C-function!
    NSLog(@"%@", msg);
}

- (void)append:(NSString *)msg;
{
    // self in a instance method points to the instance
    NSLog(@"%@, %@", msg, self);
}

+ (void)append:(NSString *)msg
{
    // self in a class method points to the class
    NSLog(@"%@, %@", msg, self);
}

@end

finally you can call the functions in the following way:

// C function: append(@"hello");
append(@"hello");

// instance method: - (void)append:(NSString *)msg;
[[[Test alloc] init] append:@"hello"];

// class method: + (void)append:(NSString *)msg;
[Test append:@"hello"];

see also What is the difference between class and instance methods?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM