iOS:Objective-C 关键字1

-- self、super、instancetype、id、nsnil、nsnull、static;

========================self、super========================

--参考:http://www.cocoachina.com/macdev/objc/2011/0124/2602.html

@interface Person:NSObject {  
}
- (void) setName:(NSString*) yourName;
@end
@interface PersonMe:Person {  
}
- (void) setAge:(NSUInteger) age;
- (void) setName:(NSString*) yourName andAge:(NSUInteger) age;
@end
@implementation PersonMe
- (void) setName:(NSString*) yourName andAge:(NSUInteger) age {
    [self setAge:age];
    [super setName:yourName];
   NSLog(@"self ' class is %@", [self class]);
   NSLog(@"super' class is %@", [super class]);
}
@end
// 调用
 PersonMe* me = [[PersonMe alloc] init];
 [me setName:@"asdf" andAge:18];
//输出  self 's class is PersonMe  和  super ' s class is PersonMe 
 例2:self不一定就是通常看到的viewcontroller对象,如果该方法在某个UIButton子类中,它可能表示某个button对象。self表示当前方法调用者
[button addTarget:self action:@selector(clicked/clicked:/clicked:event:) forControlEvents:UIControlEventTouchUpInside];
 
// 例3 :self表示当前方法的调用者
+ (void)initialize {
  if (self == [ClassName self]) {
    // ... do the initialization ...
  }
}
        说明:self 是类的 隐藏的参数,指向 当前当前调用方法的类,另一个隐藏参数是 _cmd,代表 当前类方法的 selector。这里只关注这个 self。super 是个啥?super 并不是隐藏的参数,它只是一个 “编译器指示符”,它和 self 指向的是相同的消息接收者,拿上面的代码为例,不论是用 [self setName] 还是 [super setName],接收“setName”这个消息的 接收者都是 PersonMe* me 这个对象。不同的是, 当使用 self 调用方法时,会从当前类的方法列表中开始找,如果没有,就从父类中再找;而当使用 super 时,super 告诉编译器,从父类的方法列表中开始找。然后调用父类的这个方法

       深入分析

        这种机制到底底层是如何实现的?其实当调用类方法的时候,编译器会将方法调用转成一个 C 函数方法调用,Apple 的 objcRuntimeRef 上说:
       Sending Messages
       When it encounters a method invocation, the compiler might generate a call to any of several functions to perform the actual message dispatch, depending on the receiver, the return value, and the arguments. You can use these functions to dynamically invoke methods from your own plain C code, or to use argument forms not permitted by NSObject’s perform… methods. These functions are declared in /usr/include/objc/objc-runtime.h.
■ objc_msgSend sends a message with a simple return value to an instance of a class.
■ objc_msgSend_stret sends a message with a data-structure return value to an instance of  a class.
■ objc_msgSendSuper sends a message with a simple return value to the superclass of an instance of a class.
■ objc_msgSendSuper_stret sends a message with a data-structure return value to the superclass of an instance of a class.
       可以看到会转成调用上面 4 个方法中的一个,由于 _stret 系列的和没有 _stret 的那两个类似,先只关注 objc_msgSend 和 objc_msgSendSuper 两个方法。当使用 [self setName] 调用时,会使用 objc_msgSend 的函数,先看下 objc_msgSend 的函数定义:
       id objc_msgSend(id theReceiver, SEL theSelector, ...)
       第一个参数是消息接收者,第二个参数是调用的具体类方法的 selector,后面是 selector 方法的可变参数。我们先不管这个可变参数,以 [self setName:] 为例,编译器会替换成调用 objc_msgSend 的函数调用,其中 theReceiver 是 self,theSelector 是 @selector(setName:),这个 selector 是从当前 self 的 class 的方法列表开始找的 setName,当找到后把对应的 selector 传递过去。
       而当使用 [super setName] 调用时,会使用 objc_msgSendSuper 函数,看下 objc_msgSendSuper 的函数定义:
       id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
      第一个参数是个objc_super的结构体,第二个参数还是类似上面的类方法的selector,先看下objc_super这个结构体是什么东西:
struct objc_super {
id receiver;
Class superClass;
};
       可以看到这个结构体包含了两个成员,一个是 receiver,这个类似上面 objc_msgSend 的第一个参数 receiver,第二个成员是记录写 super 这个类的父类是什么,拿上面的代码为例,当编译器遇到 PersonMe 里 setName:andAge 方法里的 [super setName:] 时,开始做这几个事:
构建 objc_super 的结构体,此时这个结构体的第一个成员变量 receiver 就是 PersonMe* me,和 self 相同。而第二个成员变量 superClass 就是指类 Person,因为 PersonMe 的超类就是这个 Person。
        调用 objc_msgSendSuper 的方法,将这个结构体和 setName 的 sel 传递过去。函数里面在做的事情类似这样:从 objc_super 结构体指向的 superClass 的方法列表开始找 setName 的 selector,找到后再以 objc_super->receiver 去调用这个 selector,可能也会使用 objc_msgSend 这个函数,不过此时的第一个参数 theReceiver 就是 objc_super->receiver,第二个参数是从 objc_super->superClass 中找到的 selector。里面的调用机制大体就是这样了。

========================instancetype、id========================

--参考:http://blog.csdn.net/wzzvictory/article/details/16994913

概念:instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象。我们都知道未知类型的的对象可以用id关键字表示,那为什么还会再有一个instancetype?

----关联返回类型(related result types)

根据Cocoa的命名规则,满足下述规则的方法:

1、类方法中,以alloc或new开头

2、实例方法中,以autorelease,init,retain或self开头

会返回一个方法所在类类型的对象,这些方法就被称为是关联返回类型的方法。换句话说,这些方法的返回结果以方法所在的类为类型,请看下面的例子:

@interface NSObject  
+ (id)alloc;  
- (id)init;  
@end    
@interface NSArray : NSObject  
@end  
NSArray *array = [[NSArray alloc] init];  
     按照Cocoa的命名规则,语句[NSArray alloc] 的类型就是NSArray*因为alloc的返回类型属于关联返回类型。同样,[[NSArray alloc]init] 的返回结果也是NSArray*

----instancetype作用

@interface NSArray  
+ (id)constructAnArray;  
@end 
        当我们使用如下方式初始化NSArray时: [NSArray constructAnArray]; 根据Cocoa的方法命名规范,得到的返回类型就和 方法声明的返回类型一样,是id .
@interface NSArray  
+ (instancetype)constructAnArray;  
@end
//实际例子
+ (instancetype)newRegionWithName:(NSString *)regionName {
	APLRegion *newRegion = [[self alloc] init];
	newRegion.name = regionName;
	newRegion.mutableTimeZoneWrappers = [[NSMutableArray alloc] init];
	regions[regionName] = newRegion;
	return newRegion;
}

       上文中实际例子self 代表着方法调用者,表示APLRegion类,跟一般情况下表示对象不同

       当使用相同方式初始化NSArray时:[NSArray constructAnArray];到的返回类型和方法所在类的类型相同,是NSArray* .

       总结一下,instancetype的作用,就是使那些非关联返回类型的方法返回所在类的类型!

       好处:能够确定对象的类型,能够帮助编译器检测代码书写问题。

比较:instancetype和id的异同:
--相同点:都可以作为方法的返回类型;

--不同点:1.instancetype可以返回和方法所在类相同类型的对象,id只能返回未知类型的对象;2.instancetype只能作为返回值,不能像id那样作为参数,比如下面的写法:

- (void)setValue:(id)value     // instancetype 不能作为参数
{ ... }  

========================nil, Nil, NULL,NSNull========================

参考:http://blog.csdn.net/yhawaii/article/details/7442529

--nil: A null pointer to an Objective-C object. ( #define nil ((id)0) )
--Nil: A null pointer to an Objective-C class.
--NULL: A null pointer to anything else, is for C-style memory pointers. ( #define NULL ((void *)0) )
NSNull: A class defines a singleton object used to represent null values in collection objects (which don't allow nil values). [NSNull null]: The singleton instance of NSNull.

         Technically they're all the same,but in practice they give someone reading your code some hints about what's going on; just like naming classes with a capital letter and instances with lowercase is recommended, but not required

======================== static || 全局 ========================

ps: 成员变量:在h文件中@interface  @end中声明或者在m文件中@implementation上用@interface  @end添加的;

        全局:在@implementation外定义的变量(在@implementation中定义也是可以但是一般不这么干)。

static int hu=3;//全局变量
NSString*str1= @"str";//全局变量
@implementation ViewController
@end 

       全局变量分为两种:

  1) static 修饰的 也称私有全局变量,只在该源文件中可用static只在声明的类中可见。在Objective-C 的语法中声明后的static静态变量在其他类中是不能通过类名直接访问的,它的作用域只能是在声明的这个.m文件中 。不过可以调用这个类的方法间接的修改这个静态变量的值

        2)没有static 修饰的 ,在任何源文件中都可以用,注意:定义没有static的全局变量,命名一定要很独特,才能区分与其他源文件中的全局变量,如果出现跟其他源文件同名的全局变量,则会报错误

undefined symbols for architecture x86_64:
"xx" ,referenced from:
      _main in main.0
ld:symbol(s) not found for architecture
clang:error:linker command failed with exit code 1
         在其他文件如果要用该全局变量,在前面加extern NSString*str1;即可使用了 。

备注:

       objective c中静态变量使用static关键字进行定义。 在c中的static作用是“静态”的意思,作用局部变量改变其生存周期,作用全局变量改变其作用域(生存周期已经够长了)。和c++不同,objective c不支持类静态成员变量(也就是不支持class variables);但objective c支持全局变量,用法和c/c++中相同。所以,在OC中作用,对于static简单数据类型,完全适用c的那些规则;对于static类变量,参照c规则,作用一样,局部的改变生存周期,全局的改变作用域(类似类静态成员)。

         (个人猜想)为什么OC中不支持类静态成员变量?因为完全可以通过 static + 类全局变量来实现(而且也有方便的类方法(+)来操作它),对于实现单例或者其他功能完全没有影响,这样也实现了和c的统一。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值