Objective-C中一些关键字 学好必知

移动开发 iOS
本文介绍的是Objective C中一些关键字 学好必知,本文属于帮助性质的一片文章,帮助快速有效的去学习Objective C,先来看内容。

本文介绍的是Objective-C中一些关键字 学好必知,本文属于帮助性质的一片文章,帮助快速有效的去学习Objective-C,先来看内容。

关于变量的作用域

  1. protected —Methods defined in the class and any subclasses can directly access the instance variables that follow.This is the default case. 

该类和所有的子类中的方法可以直接访问这样的变量,这是默认的。

  1. private —Methods defined in the class can directly access the instance variables that follow, but subclasses cannot. 

该类中的方法可以访问这样的变量,子类不可以。

  1. public —Methods defined in the class and any other classes or modules can di- rectly access the instance variables that follow. 

除了自己和子类中的方法外,也可以被其他类或者其他模块中的方法所访问。开放性最大。

  1. package —For 64-bit images, the instance variable can be accessed anywhere within the image that implements the class. 

对于64位图像,这样的成员变量可以在实现这个类的图像中随意访问。

全局变量(extern)

在程序的开始处,没有在一个方法里面写了

  1. int gMoveNumber=0

那么我们说这个变量就是全局变量,也就是说在这个模块中的任何位置可以访问这个变量。

这样的变量也是外部全局变量,在其他文件中也可以访问它。但是访问的时候要重新说明下这个变量,说明的方法是:

  1. extern int gMoveNumber; 

当然我们也可以在声明的时候加上extern关键字。

  1. extern int gMoveNumber=0

这样的话在其他的类中使用还是需要重新说明一下了,而且这时候编译器会给出警告。

如果这个全局变量只是在自己的类中使用,或者其他的类使用的它情况也比较小,那么我们把它定义成第一种情况,如果在外部文件使用的也比较多的话,那么我们把它定义成第二种情况。

这种定义其实违背了封装性。

静态变量(static)

因为全局变量是全局的,影响封装,所以有时候要用静态变量。

  1. static int gMoveNumber; 

这是这个变量是这个类中的静态变量。如果不定义初始值的话为零。

如果静态变量定义在方法中,那么这个变量在方法执行完之后还是有效的,如果在第一次调用的时候改变了这个变量的值,那么在第二次调用的时候,这个变量的值是被改变过的值。

如果被定义在类中,那么这种改变也是有效的,就是作用域发生了改变。一个在方法中,一个在类中。

  1.   atomic和nonatomic 

nonatomic是告诉系统不要使用mutex(互斥)锁定。这种锁定会导致系统的性能低下,所以一般在多线程的时候使用atomic,平时多数用nonatomic。

  1.   @synthesize和@dynamic  
  2.   @synthesize will generate getter and setter methods for your property.  
  3.   @dynamic just tells the compiler that the getter and setter methods are implemented not by the 
  4. class itself but somewhere else (like the superclass)  
  5.   Uses for @dynamic are e.g. with subclasses of NSManagedObject (CoreData) or when you want to create an 
  6. outlet for a property defined by a superclass that was not defined as an outlet:  
  7.   Super class: 

C代码

  1.   @property(nonatomic, retain)NSButton* someButton;  
  2.   ...  
  3.   @synthesize someButton;  
  4.   @property(nonatomic, retain)NSButton* someButton;  
  5.   ...  
  6.   @synthesize someButton;  
  7.   Subclass:  
  8.   C代码  
  9.   @property(nonatomic, retain)NSButton* someButton;  
  10.   ...  
  11.   @dynamic someButton; 

小结:关于Objective-C中一些关键字 学好必知的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-19 13:49:19

Objective-C 数据类型

2011-08-01 11:49:05

Objective-C

2013-07-10 11:31:10

iOS面试题Objective-CiOS开发

2011-08-04 13:55:10

Cocoa Objective- 文件

2009-08-13 09:49:16

C#关键字

2012-01-18 10:13:50

Objective-CiOSself

2013-08-26 14:58:48

App Store关键字优化App营销

2013-08-26 15:19:44

应用商店AppStore关键字选取

2009-09-02 09:24:03

C# this关键字

2009-08-21 14:58:56

C# this关键字

2011-08-10 18:07:29

Objective-C反射

2013-03-27 12:54:00

iOS开发Objective-C

2011-05-11 11:20:26

Objective-C

2013-06-20 10:40:32

Objective-C实现截图

2011-05-11 15:58:34

Objective-C

2009-06-22 15:36:00

如何学好java

2009-08-13 13:04:29

C# lock关键字

2009-08-13 17:44:34

C# using关键字

2009-08-06 17:52:23

C#增加that关键字

2011-06-14 13:26:27

volatile
点赞
收藏

51CTO技术栈公众号