Difference between + and - methods in Objective-c

后端 未结 5 1756
野的像风
野的像风 2020-12-14 06:42

What is the difference between methods that are declared with - and methods that are declared with +

e.g

- (void)methodname

+ (void)methodname
         


        
5条回答
  •  [愿得一人]
    2020-12-14 07:30

    Methods prefixed with - are instance methods. This means they can only be invoked on an instance of a class, eg:

    [myStringInstance length];
    

    Methods prefixed with + are class methods. This means they can be called on Classes, without needing an instance, eg:

    [NSString stringWithString:@"Hello World"];
    

提交回复
热议问题