Obj-C中的关键字

原文:http://www.learn-cocos2d.com/2011/10/complete-list-objectivec-20-compiler-directives/

@class

Used for class forward declarations. Declares class as known without having to import the class’ header file.

@class ClassName ;

Note, unlike with @protocol and @selector you can not write the following to get the Class object by name:

// ERROR: this doesn't work!
Class c  = @ class (ClassName ) ;

Instead use:

Class c  =  [ClassName  class ] ;

@defs

The @defs directive returns the layout of an Objective-C class, it allows you to create a C struct with the same layout as the Objective-C class. If you don’t know yet, Objective-C classes are basically just C structs with additional methods. Makes sense if you consider that Objective-C is merely a set of extensions to the C language.

struct  { @defs ( NSObject )  }

You will only ever need @defs for some hardcore, low-level Objective-C operations or optimizations, as in this article which speeds up Objective-C message sends.

@protocol @required @optional @end

Marks the start of a protocol declaration. A @protocol can optionally declare that it must conform to other protocols.

@protocol ProtocolName  <aProtocol , anotherProtocol >
@required
   // method declarations
@optional
   // method declarations
@end

Like with @selector you can use @protocol to get a protocol object by name:

- ( void ) aMethod
{
    Protocol  *aProtocol  = @protocol (ProtocolName ) ;
}

Dependent Directives:

  • @required (default) – Declares the methods following the @required directive as required (default).
  • @optional – Declares the methods following the @optional directive as optional. Classes implementing this protocol can decide whether to implement an optional method or not. Classes making use of the protocol must test optional protocol methods for existence. For example:
    [object respondsToSelector :@selector (optionalProtocolMethod ) ] ;
  • @end – Marks the end of the protocol declaration.

@interface @public @package @protected @private @property @end

Marks the start of a class or category declaration.

Class declaration:

While SuperClassName is optional, Objective-C classes should derive from NSObject either directly or indirectly. The @interface for a class declaration can optionally declare that it conforms to other protocols.

@interface ClassName  : SuperClassName  <aProtocol , anotherProtocol >
{
@public
   // instance variables
@package
   // instance variables
@protected
   // instance variables
@private
   // instance variables
}

// property declarations
@property  (atomic , readwrite , assign ) id aProperty ;

// public instance and/or class method declarations
@end

Category declaration:

The @interface of a Objective-C category can not add instance variables. But it can optionally declare to conform to (additional) protocols. CategoryName can be omitted (leaving only the empty brackets) if the category is added to the implementation file of the class that it extends, in order to declare methods as “private”.

@interface ClassName  (CategoryName )  <aProtocol , anotherProtocol >

// property declarations
@property  (atomic , readwrite , assign ) id aProperty ;

// method declarations
@end

Dependent Directives:

  • @public – Declares the instance variables following the @public directive as publicly accessible. Public instance variables can be read and modified with pointer notation:
    someObject - >aPublicVariable  =  10 ;
  • @package – Declares the instance variables following the @package directive as public inside the framework that defined the class, but private outside the framework. This applies only to 64-bit systems, on 32-bit systems @package has the same meaning as @public.
  • @protected (default) – Declares the instance variables following the @protected directive as accessible only to the class and its derived classes.
  • @private – Declares the instance variables following the @private directive as private to the class. Not even derived classes can access private instance variables.
  • @property – Declares a property which can be accessed with dot notation. The @property can be followed by optional brackets within which special keywords (property modifiers) specify the exact behavior of the property. The property modifiers are:
    • readwrite (default), readonly – Generate both setter & getter methods (readwrite), or only the getter method (readonly).
    • assign (default), retaincopy – Only applicable for properties that can be safely cast to id. Assign simply assigns the passed value – retain sends release to the existing instance variable, sends retain to the new object, assigns the retained object to the instance variable – copy sends release to the existing instance variable, sends copy to the new object, assigns the copied object to the instance variable. In the latter two cases you are still responsible for sending release (or assigning nil) to the property on dealloc.
    • atomic (default), nonatomic – Atomic properties are thread-safe, nonatomic properties are prone to synchronization issues if accessed from multiple threads. Nonatomic property access is faster than atomic and often used in single-threaded apps, or in cases where you’re absolutely sure the property will only be accessed from one thread.
    • weak (default), strong – Available if automatic reference counting (ARC) is enabled. The keyword strong is synonymous to retain, while weak is synonymous to assign, except that a weak property is automatically set to nil should the instance be deallocated. Note thatweak is only available in iOS 5 or newer and Mac OS X 10.7 (Lion) or newer.
  • @end – Marks the end of the interface declaration.

@implementation @synthesize @dynamic @end

Marks the start of a class’ or category implementation.

Class implementation:

@implementation ClassName
@synthesize aProperty , bProperty ;
@synthesize cProperty =instanceVariableName ;
@dynamic anotherProperty ;

// method implementations
@end

Category implementation:

@implementation ClassName  (CategoryName )
@synthesize aProperty , bProperty ;
@synthesize cProperty =instanceVariableName ;
@dynamic anotherProperty , bnotherProperty ;

// method implementations
@end

Dependent Directives:

  • @synthesize – Instruct compiler to automatically generate property setter and getter methods for the given (comma seperated list of) properties. The setter and getter methods are generated according to the property modifiers. If the instance variable is not named exactly like the@property, you can specify the instance variable name following the equals sign.
  • @dynamic – Tells the compiler that the necessary setter and getter methods for the given (comma seperated list of) properties will be implemented manually, or dynamically at runtime. Accessing a dynamic property will not generate a compiler warning, even if the getter/setter is not (yet) implemented. You will want to use @dynamic in cases where property getter and setter methods need to perform custom code.
  • @end – Marks the end of the implementation of the class.

@throw @try @catch @finally

Used for handling and throwing exceptions.

Throwing and Handling exceptions:

@try
{
     // code that might throw an exception … like this one:
    NSException  *exception  = 
         [NSException exceptionWithName :@ "ExampleException"
                                reason :@ "In your face!"
                              userInfo :nil ] ;
    @throw exception ;
}
@catch  (CustomException  *ce )
{
     // CustomException-specific handling ...
}
@catch  (NSException  *ne ) 
{
     // generic NSException handling ...

     // to simply re-throw the caught exception in a catch block:
    @throw ;
}
@finally 
{
     // code that runs whether an exception occurred or not ...
}

@synchronized

Encapsulates code in a mutex lock. It ensures that the block of code and the locked object can only be accessed by one thread at a time. See mutual exclusion.

- ( void ) aMethodWithObject : (id )object
{
   @synchronized (object )
    {
       // code that works with locked object 
    }
}

@autoreleasepool

In an app that has ARC (automatic reference counting) enabled, you must use @autoreleasepool as a replacement for the NSAutoreleasePool class. The @autoreleasepool is about six times faster than using NSAutoreleasePool, therefore Apple recommends its use even for non-ARC projects.

You should not declare a variable inside the @autoreleasepool block and continue to use the variable after the @autoreleasepool block. Such code should be avoided or refactored.

- ( void ) aMethod
{
    @autoreleasepool
     {
         // code that creates a large number of temporary objects
     }
}

@selector

Returns the selector type SEL of the given Objective-C method. Generates compiler warning if the method isn’t declared or doesn’t exist.

- ( void ) aMethod
{
    SEL aMethodSelector  = @selector (aMethod ) ;
     [self performSelector :aMethodSelector ] ;
}

@encode

Returns the character string encoding of a type.

- ( void ) aMethod
{
     char  *enc1  = @encode ( int ) ;                  // enc1 = "i"
     char  *enc2  = @encode (id ) ;                   // enc2 = "@"
     char  *enc3  = @encode (@selector (aMethod ) ) ;   // enc3 = ":"

     // practical example:
    CGRect rect  = CGRectMake ( 0 ,  0 ,  100 ,  100 ) ;
    NSValue  *=  [NSValue value :&rect withObjCType :@encode (CGRect ) ] ;
}

@compatibility_alias

Allows you to define an alias name for an existing class. The first parameter is the alias for a class name, a class with this name must not exist. The second parameter is the name of an existing class that the alias refers to.

@compatibility_alias AliasClassName ExistingClassName

From then on you can use AliasClassName in place of ExistingClassName. This can be useful after refactoring a class’ name without modifying its behavior, you can use @compatibility_alias to allow existing code using the refactored class to continue to work without refactoring.

@”string”

Declares a constant NSString object. Such strings do not need to be retained or released.

- ( void ) aMethod
{
    NSString * str  = @ "This is a constant string." ;
    NSUInteger strLength  =  [@ "This is legal!" length ] ;
}
Summary

I hope you enjoyed this list and hopefully learned something from it. If you know there’s a directive missing from the list, please add a comment and I will update the post!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值