OC语言day01-13方法和函数的区别

pragma mark 方法和函数的区别

pragma mark 概念

/**
 函数和方法的区别
 1. 函数属于整个文件, 方法属于某一个类
    方法如果离开类就不行
 
 2. 函数可以直接调用, 方法 必须用 对象 或者类来调用
    注意: 虽然 函数 属于整个文件, 但是如果把函数 写在类的声明中 会不识别
 
 3. 不能把函数当做方法来调用, 也不能 把方法当作函数来调用
 
 方法的注意点:
 > 方法可以没有声明只有实现。
 > 方法可以只有声明没有实现, 编译不会报错,但是运行会报错
 
 如果方法只有声明没有实现, 那么运行会报:
reason: '+[Person demo]: unrecognized selector sent to class 0x100001140'
 发送一个 不能识别 的消息, 在Person类中 没有+开头的demo 方法
 
 区别 
 上面是一个 class(代表类) 下面是一个 instance(代表实例)
 reason: '-[Person test]: unrecognized selector sent to instance 0x100503820'
 
 类也有一个注意点:
 类可以只有实现 没有声明
 注意: 在开发中不建议这样写
 */

pragma mark 代码

#import <Foundation/Foundation.h>
#pragma mark 类
//@interface Person : NSObject
//- (void)test;   // 对象方法 声明
//+ (void)demo;   // 类方法 声明
//
//
//@end


#warning 类可以只有实现 没有声明 只需要在 @implementation Person 加上 : NSObject ---(具有创建对象的条件)
//@implementation Person
@implementation Person : NSObject
// 对象方法实现
- (void)test
{
    NSLog(@"test");
}

//// 类方法实现
+ (void)demo
{
    NSLog(@"demo");
}


@end

#warning 函数的声明
extern void add();      // 外部函数的 声明
static void  minus();   // 内部函数的 声明

// extern 写在.m 表示 定义一个外部函数为 add
// 如果extern写在.h中,代表声明一个外部函数
// 外部函数
extern void add()
{
    printf("add\n");
}
// static 写在.m 表示 定义一个内部函数为 minus
// 如果static写在.h中,代表声明一个内部函数
// 内部函数
static void  minus()
{
    printf("minus\n");
}

#pragma mark main函数
int main(int argc, const char * argv[])
{
    add();

#warning 如果方法只有声明 没有实现 调用了方法 会出现错误
    Person *p = [Person new];
    [p test];
//    [Person demo];
    /*
     最主要看的是
     Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[Person demo]: unrecognized selector sent to class 0x100001140'
     
     */
    /*
     2016-05-15 11:17:58.534 方法和函数的区别[814:33441] +[Person demo]: unrecognized selector sent to class 0x100001140
     2016-05-15 11:17:58.536 方法和函数的区别[814:33441] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[Person demo]: unrecognized selector sent to class 0x100001140'
     *** First throw call stack:
     (
     0   CoreFoundation                      0x00007fff847b2e32 __exceptionPreprocess + 178
     1   libobjc.A.dylib                     0x00007fff93ca44fa objc_exception_throw + 48
     2   CoreFoundation                      0x00007fff8481c24d +[NSObject(NSObject) doesNotRecognizeSelector:] + 205
     3   CoreFoundation                      0x00007fff84723661 ___forwarding___ + 1009
     4   CoreFoundation                      0x00007fff847231e8 _CF_forwarding_prep_0 + 120
     
     6   libdyld.dylib                       0x00007fff836825ad start + 1
     )
     libc++abi.dylib: terminating with uncaught exception of type NSException
     
     */
    
#warning 调用了类方法 以函数的形式调用 出现错误
//    demo();
//    test();
    /*
     说明 test 这个函数在main.m 里面没有定义
     Undefined symbols for architecture x86_64:
     "_test", referenced from:
     _main in main.o
     ld: symbol(s) not found for architecture x86_64
     clang: error: linker command failed with exit code 1 (use -v to see invocation)
     */
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容