-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathObjective-C_@Compiler_Directives.rb
More file actions
288 lines (255 loc) · 9.66 KB
/
Copy pathObjective-C_@Compiler_Directives.rb
File metadata and controls
288 lines (255 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
cheatsheet do
title 'Objective-C @Compiler Directives'
docset_file_name 'Objective-C_@Compiler_Directives'
keyword 'objc'
source_url 'http://cheat.kapeli.com'
style 'td.description .name {font-weight:bold}'
category do
id 'class'
entry do
name '@class'
notes <<-'END'
Declares class as known without having to import the class’ header file.
```objc
@class ClassName;
Getting class object by name:
// ERROR: this doesn't work!
Class c = @class(ClassName);
Instead use:
Class c = [ClassName class];
```
END
end
entry do
name '@end'
notes <<-'END'
Marks end of the class, protocol or interface declaration.
END
end
end
category do
id 'protocol'
entry do
name '@protocol'
notes <<-'END'
Marks the start of a protocol declaration.
```objc
@protocol ProtocolName <aProtocol, anotherProtocol>
```
Get a protocol object by name:
```objc
Protocol *aProtocol = @protocol(ProtocolName);
```
END
end
entry do
name '@required'
notes <<-'END'
Methods following are required (default).
END
end
entry do
name '@optional'
notes <<-'END'
Methods following are optional. Classes making use of the protocol must test Optional protocol methods for existence:
```objc
[object respondsToSelector:@selector(optionalProtocolMeth od)];
```
END
end
end
category do
id 'interface'
entry do
name '@interface'
notes <<-'END'
Marks the start of a class or category declaration. Objective-C classes should derive from NSObject directly or indirectly. Use @interface to declare that the class conforms to protocols.
```objc
@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** - Objective-C category cannot add instance variables. Can to conform to (additional) protocols. CategoryName can be omitted if in implementation file making methods “private”.
```objc
@interface ClassName (CategoryName) <aProtocol, anotherProtocol>
```
END
end
entry do
name '@public'
notes <<-'END'
Declares instance variables after @public directive as publicly accessible. Read and modified with pointer notation:
```objc
someObject->aPublicVariable = 10;
```
END
end
entry do
name '@package'
notes <<-'END'
Declares instance variables after @package directive as public inside the framework that defined the class, but private outside the framework. Applies only to 64-bit systems, on 32- bit systems @package has the same meaning as @public.
END
end
entry do
name '@protected'
notes <<-'END'
Default. Declares instance variables after @protected directive as accessible only to the class and derived classes.
END
end
entry do
name '@private'
notes 'Declares the instance variables following the @private directive as private to the class. Not even derived classes can access private instance variables.'
end
entry do
name '@property'
notes <<-'END'
Declares a property which accessible with dot notation. @property can be followed by optional brackets within which property modifiers specify the exact behavior of the property. Property modifiers: readwrite (default), readonly – Generate both setter & getter methods (readwrite), or only the getter method (readonly).
**assign (default), retain, copy** – For properties that can safely cast to id. Assign assigns passed value – retain sends release to an 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 latter two cases you are responsible for sending release (or assigning nil) to the property on dealloc.
**atomic (default), nonatomic** – Atomic properties are thread-safe. Nonatomic properties are not thread-safe. Nonatomic property access is faster than atomic and often used in single- threaded apps, or cases where you’re absolutely sure the property will only be accessed from one thread.
**strong (default), weak** – Available if automatic reference counting (ARC) is enabled. The keyword strong is synonymous to retain, while weak is synonymous to assign, except a weak property is set to nil if instance is deallocated.
END
end
entry do
name '@selector'
notes <<-'END'
Returns the selector type SEL of the given Objective-C method. Generates compiler warning if the method isn’t declared or doesn’t exist.
```objc
- (void)aMethod {
SEL aMethodSelector = @selector(aMethod);
[self performSelector:aMethodSelector];
}
```
END
end
end
category do
id 'implementation'
entry do
name '@implementation'
notes <<-'END'
Marks start of a class’ or category implementation.
**Class implementation:**
```objc
@implementation ClassName
@synthesize aProperty, bProperty;
@synthesize cProperty=instanceVariableName;
@dynamic anotherProperty;
// method implementations
@end
```
**Category implementation:**
```objc
@implementation ClassName (CategoryName)
@synthesize aProperty, bProperty;
@synthesize cProperty=instanceVariableName;
@dynamic anotherProperty, banotherProperty;
// method implementations
@end
```
END
end
entry do
name '@synthesize'
notes <<-'END'
Generate setter and getter methods for a comma separated property list according to property modifiers. If instance variable is not named exactly like @property, you can specify instance variable name after the equals sign.
END
end
entry do
name '@dynamic'
notes <<-'END'
Tells the compiler the setter and getter methods for the given (comma separated) properties are implemented manually, or dynamically at runtime. Accessing a dynamic property will not generate a compiler warning, even if the getter/setter is not implemented. You will want to use @dynamic in cases where property getter and setter methods perform custom code. @end – Marks end of class implementation.
END
end
entry do
name '@synchronized'
notes <<-'END'
Encapsulates code in mutex lock ensuring that the block of code and locked object are only accessed by one thread at a time.
```objc
-(void) aMethodWithObject:(id)object {
@synchronized(object) {
// code that works with locked object
}
}
```
END
end
entry do
name '@"string"'
notes <<-'END'
Declares a constant NSString object. Does not need to be retained or released.
```objc
NSString* str = @"This is a constant string.";
NSUInteger strLength = [@"This is legal!" length];
```
END
end
entry do
name '@throw @try @catch @finally'
notes <<-'END'
Used for handling and throwing exceptions. Throwing and Handling exceptions:
```objc
@try {
// code that might throw an exception
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 ...
// re-throw the caught exception in a catch block:
@throw;
} @finally {
// runs whether an exception occurred or not
}
```
END
end
entry do
name '@autoreleasepool'
notes <<-'END'
In an ARC (automatic reference counting) enabled about 6x faster than NSAutoreleasePool and used as a replacement. Avoid using a variable created in an @autoreleasepool after the autoreleasepool block.
```objc
@autoreleasepool {
// code that creates temporary objects
}
```
END
end
entry do
name '@encode'
notes <<-'END'
Returns the character string encoding of a type.
```objc
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)];
```
END
end
entry do
name '@compatibility_alias'
notes <<-'END'
Sets alias name for an existing class. First parameter is the alias, second the actual class name. @compatibility_alias AliasClassName ExistingClassName After this you can use AliasClassName in place of ExistingClassName.
END
end
end
notes <<-'END'
* Based on a [cheat sheet](https://maniacdev.com/cheatsheetobjccd.pdf) by [Chaosky](http://chaosky.me)
END
end