- 浏览: 624444 次
文章分类
最新评论
-
q781253433:
qin ~.求demo看看~
Android之Widget -
zhongyuanceshi:
博主,没看到你的例子的Demo,共享下
Android之Widget
原文:http://www.learn-cocos2d.com/2011/10/complete-list-objectivec-20-compiler-directives/
- @class
- @defs
- @protocol @required @optional @end
- @interface @public @package @protected @private @property @end
- @implementation @synthesize @dynamic @end
- @throw @try @catch @finally
- @synchronized @autoreleasepool
- @selector @encode
- @compatibility_alias
- @”string”
@class
Used for class forward declarations. Declares class as known without having to import the class’ header file.
Note, unlike with @protocol and @selector you can not write the following to get the Class object by name:
Class c = @class(ClassName);
Instead use:
@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.
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.
@required
// method declarations
@optional
// method declarations
@end
Like with @selector you can use @protocol to get a protocol object by name:
{
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.
{
@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”.
// 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), retain, copy – 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:
@synthesize aProperty, bProperty;
@synthesize cProperty=instanceVariableName;
@dynamic anotherProperty;
// method implementations
@end
Category implementation:
@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:
{
// 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.
{
@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.
{
@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.
{
SEL aMethodSelector = @selector(aMethod);
[self performSelector:aMethodSelector];
}
@encode
Returns the character string encoding of a type.
{
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 *v = [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.
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.
{
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!
发表评论
-
c语言中的关键字总结
2014-02-08 14:03 746switch构成switch选择结构 case swi ... -
代码块 block
2013-02-03 14:17 995代码块block讲解: http://blog.csdn ... -
Objective-C/C++混编编译器设置
2012-12-25 19:20 1785XCode编译器介绍 : http://www.cnblog ... -
c语言中的extern关键字
2012-12-02 13:14 14961. 用extern声明外部变量 定义:外部变量是指在函数或 ... -
小技巧_ARC新机制与旧代码混合使用
2012-09-17 16:58 921-fno-objc-arc 在xcode中可以设置某一个m文 ... -
NSString,char*,NSData,NSUrl转换
2012-08-30 19:03 72411. NSString转化为UNICODE String: ... -
ios5中的一些新变化
2012-08-30 16:45 122ARC属性的规则 strong:等同于"ret ... -
KVC&KVO小结
2012-08-21 10:05 2765使用@property,属性名与实例变量名有时可能不相 ... -
深浅拷贝
2012-08-15 10:57 852参考文章:http://mobile.51cto.com/ip ... -
id (void *)区别
2012-08-15 09:56 2498参考文章:http://stackoverflow.com/q ... -
objective-c if判断条件
2012-08-15 09:40 1394在if条件中如果是false,FALSE(为0), n ... -
@interface和category和extension小结
2012-08-10 16:58 1994注意:category是一种机 ... -
单例模式
2012-08-10 16:25 862官方参考: http://www.apple.com.cn/ ... -
@property后的关键字小结
2012-07-23 13:16 953参考内容:http://blog.csdn.net/l ... -
objective c 学习网站
2012-06-26 10:20 1683最基础的oc学习: http://blog.csdn.net ...
相关推荐
在 C++ 中,用 `static` 关键字声明的成员函数类似于 Objective-C 的类方法,因为它们都与类本身关联,而不是特定的实例。 7. **运行时系统** Objective-C 使用动态类型和运行时系统,允许在程序运行时决定消息的...
Objective-C(简称Obj-C或Objective-C)是苹果公司基于C语言构建的一种面向对象的编程语言,广泛应用于iOS和Mac OS X操作系统。本教程将深入探讨Object-C的基础概念、语法特性以及实际开发中的应用,旨在帮助初学者...
* C程序的执行步骤:编译执行程序代码的录入、生成源程序*.c、语法分析、查错、翻译生成目标程序*.obj与其它目标程序或库链接、装配、生成可执行程序*.exe * C程序的结构:C程序由一个或多个函数组成、必须有且只能...
- 用户标识符是程序员自定义的变量名,不能与C语言的关键字相同,也不能以数字开头,选项A和D中包含了一些非法的标识符。 5. **函数和程序结构** - 函数是C程序的基本模块,由函数首部(声明参数和返回类型)和...
在C语言的学习中,掌握其基础知识至关重要。以下是对C语言关键知识点的详细解析: 1. **C程序的结构**: - C程序由函数组成,其中必须包含一个`main()`函数作为程序执行的起点。 - 函数定义是平行的,不支持嵌套...
问题:在C++环境下使用C函数的时候,常常会出现编译器无法找到obj模块中的C函数定义,从而导致链接失败的情况,应该如何解决这种情况呢? 答案:C++语言在编译的时候为了解决函数的多态问题,会将函数名和参数联合...
1. **简洁明了**:C语言的关键字数量有限,只有32个,这使得语言结构清晰,易于理解和学习。 2. **强大的表达能力**:尽管简洁,C语言却能有效地表达复杂的计算和逻辑操作,通过9种控制语句和丰富的运算符实现。 3. ...
这里,`stdcall` 是调用约定,`external` 关键字用于指定外部函数所在的 DLL 文件和函数名(如果 C 函数名在 DLL 中被导出为 mangled name,则需要提供正确的名称)。 **步骤四:编写 Delphi 代码** 现在,你可以...
根据 Obj-C 的关键字,去除一些重复行 删除 注释行 和 空格 删除赋值语句之前的变量 只 Print 重复次数 > 3 的代码行 具体设置注释中有,可以自行设置 实际测了下,虽然目前弱爆了,但至不用在工程里人肉慢慢找了 ##...
Objective-C,简称Obj-C,是苹果公司为iOS和macOS平台开发应用程序的主要编程语言。它是一种动态的、面向对象的C语言扩展,结合了Smalltalk的特性,使得它在处理面向对象编程时更加灵活。这份PDF文档,针对iOS开发...
- C语言中标识符的分类,包括关键字、用户定义标识符和预定义标识符。 6. 运算符和表达式: - 运算符的种类,如算术运算符、关系运算符和逻辑运算符。 - 表达式中赋值运算和算术运算的结合规则。 - 指定赋值和...
- 常见的C语言关键字包括int, float, double, struct, union, void等。 - 基本数据类型包括char, short, int, long, float, double等。 - 控制结构如if, for, while, do-while等。 - 函数定义和调用的语法。 3....
- C语言源代码不能直接运行,需先编译成`.obj`文件,再链接生成`.exe`可执行文件。 - 标识符命名规则中,不允许使用连接符。 - 将`int`转换为`float`使用强制类型转换`(float)n`。 - C语言的基本单位是函数。 -...
- C语言源文件的扩展名通常是`.c`,编译后目标文件扩展名为`.obj`,可执行文件扩展名为`.exe`。 - 标识符由字母、数字和下划线组成。 5. **数据类型、运算符和表达式**: - C语言提供多种数据类型,如整型(int)...
- **nil和Nil**:nil是Objective-C中表示空对象引用的关键字,而Nil是一种特殊的空对象实例。 ##### 4.2 类声明 - **属性和方法**:在类声明中定义,使用`@property`声明属性,使用`-(返回类型)(方法名)`声明方法...
const对象默认为文件的局部变量,与其他变量不同,除非特别说明,在全局作用域的const变量时定义该对象的文件局部变量。此变量只存在于那个文件中中,不能别其他文件...因为若此时如果不分配空间,则obj中根本就不会
它在C语言的基础上扩展了Smalltalk式的消息传递机制,成为Apple开发者工具Xcode中的主要编程语言。这篇“Objective-C 基础教程-中文版”是针对初学者和希望深入理解Objective-C的开发者设计的,旨在帮助他们掌握这一...
- 源程序(`.c`)经过编译生成目标程序(`.obj`),再通过连接生成可执行程序(`.exe`)。 9. **数据类型及其运算**: - C语言的数据类型包括基本类型(如`int`、`float`)、构造类型(如数组、结构体)、指针...