Objective

在之前我们学的东西里, 我们一直都在使用实例方法, 那么除了实例方法之外, 还有没有另外一种方法呢? 答案是有的, 除了实例方法, 还有一种方法叫做类方法, 所谓的类方法其实就是以类名开头, 不需要创建对象, 直接调用的一种方法, 下面让我们一起来探究一下~~


例子:

#import <Foundation/Foundation.h>

@interface Preson : NSObject
+ (void)printfClass;
@end

@implementation Preson
+ (void)printfClass
{
    NSLog(@"该类是Preson");
}
@end

int main()
{
    [Preson printfClass];
    return 0;
}

输出的结果:

Cain:2.第二天 Cain$ cc 05-类方法.m -framework Foundation
Cain:2.第二天 Cain$ ./a.out 
2015-01-17 10:01:42.427 a.out[13853:1530323] 该类是Preson


在某些场合使用类方法可以提高我们的性能, 让我们的程序可以更好的运行~

PS: 在编译的时候, 类一加载就会分配好存储空间, 所以不需要再创建对象, 就可以直接调用类方法.




但是有一个注意点, 不要使用对象调用类方法, 否则就会报错, 比如:

#import <Foundation/Foundation.h>

@interface Preson : NSObject
+ (void)printfClass;
@end

@implementation Preson
+ (void)printfClass
{
    NSLog(@"该类是Preson");
}
@end

int main()
{
    Preson *p = [Preson new];
    
    [p printfClass];
    
    return 0;
}

编译链接:

Cain:2.第二天 Cain$ cc 05-类方法.m -framework Foundation
05-类方法.m:20:8: warning: instance method '-printfClass' not found
      (return type defaults to 'id') [-Wobjc-method-access]
    [p printfClass];
       ^~~~~~~~~~~
05-类方法.m:3:12: note: receiver is instance of class declared here
@interface Preson : NSObject
           ^
1 warning generated.

运行结果:

Cain:2.第二天 Cain$ ./a.out 
2015-01-17 10:29:26.968 a.out[13892:1535384] -[Preson printfClass]: unrecognized selector sent to instance 0x7f9f3ac106f0
2015-01-17 10:29:26.969 a.out[13892:1535384] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Preson printfClass]: unrecognized selector sent to instance 0x7f9f3ac106f0'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff9587464c __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x00007fff9b68b6de objc_exception_throw + 43
	2   CoreFoundation                      0x00007fff958776bd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
	3   CoreFoundation                      0x00007fff957bea84 ___forwarding___ + 1028
	4   CoreFoundation                      0x00007fff957be5f8 _CF_forwarding_prep_0 + 120
	5   a.out                               0x000000010d2d1f2a main + 90
	6   libdyld.dylib                       0x00007fff955795c9 start + 1
	7   ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6


这个错误又是和之前的那个错误一样, "你所发送给对象的消息无法识别"

2015-01-17 10:29:26.968 a.out[13892:1535384] -[Preson printfClass]: unrecognized selector sent to instance 0x7f9f3ac106f0


原因其实很简单, 让我们回想一下, 实例方法是以" - "开头的, 而类方法则是以" + "开头的, 它们之间不能相互兼容, 所以使用对象去调用类方法会报错.





同样, 如果用类去调用实例方法, 也会如此:

#import <Foundation/Foundation.h>

@interface Preson : NSObject
- (void)test;
@end

@implementation Preson
- (void)test
{
    NSLog(@"我是实例方法");
}
@end

int main()
{
    [Preson test];
    return 0;
}

编译链接:

05-类方法.m:22:13: warning: class method '+test' not found (return type defaults to 'id')
      [-Wobjc-method-access]
    [Preson test];
            ^~~~
1 warning generated.

运行结果:

Cain:2.第二天 Cain$ ./a.out 
2015-01-17 10:45:18.310 a.out[13933:1542292] +[Preson test]: unrecognized selector sent to class 0x109a01180
2015-01-17 10:45:18.312 a.out[13933:1542292] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[Preson test]: unrecognized selector sent to class 0x109a01180'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff9587464c __exceptionPreprocess + 172
	1   libobjc.A.dylib                     0x00007fff9b68b6de objc_exception_throw + 43
	2   CoreFoundation                      0x00007fff958775bd +[NSObject(NSObject) doesNotRecognizeSelector:] + 205
	3   CoreFoundation                      0x00007fff957bea84 ___forwarding___ + 1028
	4   CoreFoundation                      0x00007fff957be5f8 _CF_forwarding_prep_0 + 120
	5   a.out                               0x0000000109a00efd main + 45
	6   libdyld.dylib                       0x00007fff955795c9 start + 1
	7   ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6


同样是这个错误:

2015-01-17 10:45:18.310 a.out[13933:1542292] +[Preson test]: unrecognized selector sent to class 0x109a01180


但这个是没有找到对应的类方法错误, 而上面那个是没有找到对应的实例方法错误~~


还有另外一种错误:

#import <Foundation/Foundation.h>

@interface Preson : NSObject
{
    int age;
}
+ (void)test;
@end

@implementation Preson
+ (void)test
{
    NSLog(@"%d", age);
}

@end

int main()
{
    [Preson test];
    
    return 0;
}

编译链接:

Cain:2.第二天 Cain$ cc 05-类方法.m -framework Foundation
05-类方法.m:13:18: error: instance variable 'age' accessed in class method
    NSLog(@"%d", age);
                 ^
1 error generated.

之前就说了, 类方法依靠的不是对象, 所以也不能在类方法中访问实例变量(成员变量), 只有实例方法才能访问实例变量.



但实例方法和类方法可以同名, 因为类型不一样, 所以可以同名并存, 比如:

#import <Foundation/Foundation.h>

@interface Preson : NSObject
+ (void)test;
- (void)test;
@end

@implementation Preson
+ (void)test
{
    NSLog(@"1111");
}
- (void)test
{
    NSLog(@"3333");
}
@end

int main()
{

    [Preson test];
    
    Preson *p = [Preson new];
    
    [p test];
    
    return 0;
}

编译链接运行结果:

Cain:2.第二天 Cain$ cc 05-类方法.m -framework Foundation
Cain:2.第二天 Cain$ ./a.out 
2015-01-17 11:06:49.746 a.out[13980:1551760] 1111
2015-01-17 11:06:49.748 a.out[13980:1551760] 3333


但是为了书写规范, 最好不要写同名的类方法和实例方法.




类方法的好处:

1> 不依赖于对象, 执行效率高, 能使用类方法的时候就尽量使用类方法.

场合:

1> 当方法内部不需要使用到实例变量的时候, 就可以改用类方法.




好了这次就讲到这里, 下次我们继续~~~

原文地址:https://www.cnblogs.com/iOSCain/p/4282853.html