Last active
January 3, 2017 21:55
-
-
Save peterflynn/c8948a9c3076c1bcf229 to your computer and use it in GitHub Desktop.
Objective-C syntax cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Can omit the whole @interface section if no private fields/properties to declare | |
@interface MyClass () // parens are almost always empty - used only to define a "Category" when adding mixins to existing class definition | |
{ | |
int foo; // private field (not common) - access as 'foo' | |
} // can omit these {}s if no private fields | |
@property (nonatomic) int foo2; // private property - access as 'self.foo2' | |
// Any property (private or public) can also be accessed within the class as "_foo2" (without "self"). This bypasses the getter/setter | |
// methods and accesses it directly like an ivar. Since the getter/setter are usually auto-generated stubs, there's usually no behavior | |
// difference with this syntax. | |
@end | |
@implementation MyClass | |
{ | |
int foo3; // private field - access as 'foo3' | |
} // can omit these {}s if no private fields | |
int foo4; // file-global variable - NOT a class member at all | |
-(int)methodWithNoArgs | |
{ | |
return 0; | |
} | |
[instance methodWithNoArgs]; // method call for the above | |
-(int)methodWithOneArg:(NSString*)arg1 | |
{ | |
return arg1.length; | |
} | |
[instance methodWithOneArg:someString]; // method call for the above | |
-(int)methodWithTwoArgs:(NSString*)arg1 arg2:(int)arg2 | |
{ | |
// Note: the two occurrences of "arg2" can actually be different identifiers. The first one is the "public" arg name | |
// that callers use to pass the arg; the second one is the "internal" arg name you use to address the arg here, inside | |
// the method body. | |
// The first arg has no such split (effectively, the method name serves as the "public" arg name for arg1). | |
return arg11.length + arg2; | |
} | |
[instance methodWithTwoArgs:someString arg2:someNumber]; // method call for the above | |
+(int)staticMethod // note "+" instead of "-" | |
{ | |
return 0; | |
} | |
[MyClass staticMethod]; // method call for the above | |
// (static methods can have args too, via same syntax as above) | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: interfaces in Objective-C are called "protocols" | |
@interface MyClass : SuperClass <SuperInterface1, SuperInterface2> | |
{ | |
int foo5; // public field, aka "ivar" - access as "foo5" within the class, "instance->foo5" from outside the class | |
@protected int foo6; // protected field, aka "ivar" - access as "foo6" (within the class or subclasses only) | |
} // can omit these {}s if no ivar fields | |
@property (nonatomic) int foo7; // public property - access as "self.foo7" within the class, "instance.foo7" from outside | |
@property (nonatomic, readonly) int foo8; // read-only to code outside the class, but writable by code within the class | |
// Public methods | |
// (private methods do not need to be declared in the .h at all - a method body definition in the .m file is all you need) | |
-(int)methodWithNoArgs; | |
-(int)methodWithOneArg:(NSString*)arg1; | |
-(int)methodWithTwoArgs:(NSString*)arg1 arg2:(int)arg2; | |
// (This declaration includes the "internal" arg2 name too even though callers are never exposed to it. It doesn't even have to match | |
// the actual "internal" arg2 name used in the method body definition - the 2nd "arg2" here can be anything, and is basically ignored). | |
+(int)staticMethod; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Objective-C calls methods "messages" - calling method on object foo is "sending a message to foo" | |
Because @property fields are really getter/setter methods ("messages") that are automatically invoked, all the rules that apply to | |
calling functions ("sending messages") also apply to getting/setting properties. | |
"id" is similar to "void*" but can only point to Objective-C types. Compiler lets you call any method on it. | |
Contrast to "NSObject*", which only lets you call base NSOjbect methods unless you cast it. | |
Calling any method or accessing any property on a null pointer ("NIL") will silently succeed. If the invocation had a return type, you'll | |
get a zeroed-out value like NIL or 0. | |
"First responder" = has keyboard focus | |
NSButton: "action" = method name, "target" = object to call that method on | |
@property -------------- | |
- If thread-safe access is needed, use atomic instead of nonatomic | |
- Pointer-type properties default to a "strong" reference (aka "retain"). Specify "weak" or "copy" to override this | |
("copy" is a no-op for immutable types like NSString*) | |
- Primitive-type properties default to "assign" instead | |
To specify a getter/setter for an @property declaration: | |
@property (nonatomic) int foo; | |
-(int)foo { // getter | |
return _foo; | |
} | |
-(void)setFoo:(int)newValue { | |
_foo = newValue; | |
} | |
If you specify both of these, add @synthesize to the @property declaration - otherwise the backing "_foo" ivar will not | |
get auto-generated anymore. (Or if you want a different backing storage, e.g. a more complex type than the getter/setter expose, you | |
can manually declare your own backing storage instead). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Man, GitHub sure makes it a pain to put Gist files in the order you want 👎