objective-c a beginner's dive
Post on 21-Oct-2014
1.162 views
DESCRIPTION
Objective-C is a Smalltalk-like Object-Oriented layer on top of the C language. It is the official language of OS X and iOS. Assuming you have a background in Object-Oriented Programming and a basic understanding of the C language or syntax, this talk will cover everything you need to know about Objective-C. By the end of the talk, you will understand how to make and use your own objects, the Foundation Framework and the data structures it provides, and the Objective-C specific language constructs and syntactic-sugar.TRANSCRIPT
![Page 1: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/1.jpg)
Objective-CA Beginner’s Dive
Saturday, March 9, 13
![Page 2: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/2.jpg)
I Assume
• A Background in Java or C++
• An understanding of OOP
• A basic understanding of C
Saturday, March 9, 13
![Page 3: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/3.jpg)
What is Objective-C?
• Superset of C
• Smalltalk style Object-Oriented
• The Official Language for iOS and OS X
Saturday, March 9, 13
![Page 4: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/4.jpg)
Cool!But before we dive in...
Saturday, March 9, 13
![Page 5: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/5.jpg)
Hello World!//// main.m
#include <stdio.h>#include <Foundation/Foundation.h>
int main(int argc, const char **argv){ NSString *message = @"Hello World!"; printf("%s\n", [message cString]); return 0;}
Saturday, March 9, 13
![Page 6: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/6.jpg)
Hello World!• Objective-C String Literal
• @”Hello World!”
• C Function
• printf()
• main()
• Objective-C message
• [message cString]
//// main.m
#include <stdio.h>#include <Foundation/Foundation.h>
int main(int argc, const char **argv){ NSString *message = @"Hello World!"; printf("%s\n", [message cString]); return 0;}
Saturday, March 9, 13
![Page 7: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/7.jpg)
Ok.Now, on to the teaching!
Saturday, March 9, 13
![Page 8: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/8.jpg)
Objective-C Features
• C Functions
• C Structs
• C Unions
• C Pointers
• C Arrays
• C Everything Else
Saturday, March 9, 13
![Page 9: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/9.jpg)
Objective-C Features
• Objects
• Methods
• Inheritance
• Properties
• Protocols
• Categories
• Data and Collections
• Object Literals
• Object Subscripts
• Forin loops
• Blocks and Closures
• ARC
Saturday, March 9, 13
![Page 10: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/10.jpg)
You Will Learn About
• Making and Using Objects
• Using Foundation Framework Data Types
• Automatic Reference Counting (ARC)
Saturday, March 9, 13
![Page 11: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/11.jpg)
Writing ObjectsA Quick Run-Through
Saturday, March 9, 13
![Page 12: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/12.jpg)
Objects
• Interface
• like Java’s Interface, but every object has one
• placed in the header file
• Implementation
• must fill minimum requirements of Interface
• placed in the implementation file
Saturday, March 9, 13
![Page 13: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/13.jpg)
Example Object//// MyObject.h// Interface
#import <Foundation/Foundation.h>
@interface MyObject : NSObject { int _myInt;}
// method declarations go here
@end
//// MyObject.m// Implementation
#import “MyObject.h”
@implementation MyObject
// method implementations go here
@end
Saturday, March 9, 13
![Page 14: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/14.jpg)
Objects (More Info)
• Objects have Instance Variables (ivars)
• No Class variables, use C static globals
• No enforced “public” and “private”
• Object Instances can be of type:
•id
•Class *
Saturday, March 9, 13
![Page 15: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/15.jpg)
Object Methods
• Class Method or Instance Method
• Variables are “named” in the method’s signature (fancy word for method name)
• Default return and variable type is id
Saturday, March 9, 13
![Page 16: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/16.jpg)
Format of a Method
+/- (return type)methodName;
+/- (return type)methodWithVar:(var type)var;
+/- (return type)methodWithVar1:(var type)var1 ! ! ! ! ! ! ! ! var2:(var type)var2;
Saturday, March 9, 13
![Page 17: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/17.jpg)
Method Examples//// MyObject.m// Implementation
#import “MyObject.h”
@implementation MyObject
// setter- (void)setMyInt:(int)myInt { _myInt = myInt;}
// getter- (int)myInt { return _myInt;}
@end
//// MyObject.h// Interface
#import <Foundation/Foundation.h>
@interface MyObject : NSObject { int _myInt;}
// setter- (void)setMyInt:(int)myInt;
// getter- (int)myInt;
@end
Saturday, March 9, 13
![Page 18: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/18.jpg)
Object Method Calling
• Not like C, C++, or Java
• Based on Smalltalk message passing
• The Square Brackets [] are your friend!
[object method];[object methodWithVar:value];[object methodWithVar1:val1 ! ! ! ! ! ! ! ! var2:val2];
Saturday, March 9, 13
![Page 19: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/19.jpg)
Testing Responsiveness to a Selector
• The name of a method is its SelectorSEL mySelector = selector(myMethodWithParameter:)
• Every object inherits respondsToSelector:
• Takes the selector to test for
• Returns YES when the object can respond to that method
• Returns NO when the object cannot respond to that method
Saturday, March 9, 13
![Page 20: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/20.jpg)
Object Constructor
• Not a special method (unlike Java)
• Just an instance method to set up the Object’s Instance Variables
• Generally named init
Saturday, March 9, 13
![Page 21: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/21.jpg)
Generic Constructor
- (id)init { if (self = [super init]) { // initialize variables } return self;}
Saturday, March 9, 13
![Page 22: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/22.jpg)
Constructor Example
// default constructor- (id)init { // calls my custom constructor return [self initWithInt:0];}
// custom constructor- (id)initWithInt:(int)myInt { if (self = [super init]) { _myInt = myInt; } return self;}
Saturday, March 9, 13
![Page 23: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/23.jpg)
Inheritance
• Single Inheritance from Objects
• Method Overloading Supported
• Superclass defined in the Interface
• Super class referenced with super
Saturday, March 9, 13
![Page 24: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/24.jpg)
Inheritance Example//// MyObject.m// Implementation
#import “MyObject.h”
@implementation MyObject
- (id)init { if (self = [super init]) { _myInt = 5; } return self;}
@end
//// MyObject.h// Interface
#import <Foundation/Foundation.h>
@interface MyObject : NSObject { int _myInt;}
- (id)init;
@end
Saturday, March 9, 13
![Page 25: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/25.jpg)
Properties
• Syntactic sugar for variable, accessor, and mutator declarations all-in-one
• Properties are declared in the Interface and synthesized in the Implementation
• Let you use “dot syntax” with getters and setters! myObjectInstance.myInt = 5;! int x = myObjectInstance.myInt;
Saturday, March 9, 13
![Page 26: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/26.jpg)
Property Example//// NSString+Reverse.m// Category
#import "NSString.h"
@implementation MyObject
// creates variable _myInt// creates getter myInt// creates setter setMyInt:@synthesize myInt = _myInt;
@end
//// NSString+Reverse.h// Category
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
@property (assign, nonatomic) int myInt;
@end
Saturday, March 9, 13
![Page 27: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/27.jpg)
More on Properties
• Attributesstrong weak copy assign readonlyatomic nonatomic
• @synthesize vs @dynamic!@synthesize variable = nameOfIvar;
• Default ivar name for variable is _variable
• Custom getters and settersgetter = myGetter, setter = mySetter:
Saturday, March 9, 13
![Page 28: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/28.jpg)
Protocols
• Like an Objective-C Interface
• Similar to Java Interfaces
• Can implement multiple Protocols
• Protocols can ‘inherit’ other Protocols
Saturday, March 9, 13
![Page 29: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/29.jpg)
Example Protocol//// MyProtocol.h// Protocol
#import <Foundation/Foundation.h>
@protocol MyProtocol <NSObject>
@property (assign, nonatomic) int myInt;
- (NSString *)stringMyInt;
@end
//// MyObject.h// Interface
#import <Foundation/Foundation.h>#import "MyProtocol.h"
@interface MyObject : NSObject <MyProtocol>
@end
Saturday, March 9, 13
![Page 30: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/30.jpg)
Categories
• The ability to add new methods to an Object
• Changes apply to all instances of the object
• Overwrites any methods that already exist in the class
• Cannot add Properties and ivars
• Can be dangerous
Saturday, March 9, 13
![Page 31: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/31.jpg)
Example Category//// NSString+Reverse.m// Category
#import "NSString+Reverse.h"#import <stdlib.h>
@implementation NSString (Reverse)
- (NSString *)reverse { int length = [self length]; char *newString = ! ! calloc(length+1, sizeof(char)); int current = 0; const char *cstr = [self cString]; for (int i=length-1; i >= 0; i--) { newString[current] = cstr[i]; current++; } NSString *new = ! ! [NSString stringWithCString:newString]; free(newString); return new;}
@end
//// NSString+Reverse.h// Category
#import <Foundation/Foundation.h>
@interface NSString (Reverse)
- (NSString *)reverse;
@end
Saturday, March 9, 13
![Page 32: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/32.jpg)
Class Extension
• Like Categories, but with the ability to add ivars and Properties
• Implementations of methods required in the main Implementation block
• Can be used to declare “private” methods
Saturday, March 9, 13
![Page 33: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/33.jpg)
Class Extension Example
//// MyObject.m
// Class Extension@interface MyObject ()
@property (assign) int myPrivateInt;
@end
// Implementation@implementation MyObject
@synthesize myPrivateInt = _myPrivateInt;
// more implementation here
@end
Saturday, March 9, 13
![Page 34: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/34.jpg)
Ok.Now, lets take a look at MyObject
Saturday, March 9, 13
![Page 35: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/35.jpg)
MyObject//// MyObject.m
#import "MyObject.h"
@interface MyObject ()@property (assign, nonatomic) int myPrivateInt;@end
@implementation MyObject
@synthesize myInt = _myInt;@synthesize myPrivateInt = _myPrivateInt;
- (id)init { return [self initWithInt:0];}
- (id)initWithInt:(int)myInt { if (self = [super init]) { _myInt = myInt;! ! _myPrivateInt = 5; } return self;}
- (NSString *)stringMyInt { return [NSString stringWithFormat:@"%d", _myInt];}
@end
//// MyObject.h
#import <Foundation/Foundation.h>#import "MyProtocol.h"
@interface MyObject : NSObject <MyProtocol>
@property (assign, nonatomic) int myInt;
// default constructor- (id)init;
// custom constructor- (id)initWithInt:(int)myInt;
@end
Saturday, March 9, 13
![Page 36: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/36.jpg)
GreatBut how do I use it?
Saturday, March 9, 13
![Page 37: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/37.jpg)
Using MyObject
MyObject *obj = [[MyObject alloc] init];obj.myInt = 5;NSLog(@"%i\n", obj.myInt);
MyObject *other = [MyObject alloc];other = [other initWithInt:5];[other setMyInt:10];NSLog(@"%i\n", [obj myInt]);
Saturday, March 9, 13
![Page 38: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/38.jpg)
Using MyObject• alloc class method
• use of init and initWithInt: methods
• splitting up the alloc and init method calls
• use of dot syntax and generated methods
• NSLog() for printing messages to the console
MyObject *obj = [[MyObject alloc] init];obj.myInt = 5;NSLog(@"%i\n", obj.myInt);
MyObject *other = [MyObject alloc];other = [other initWithInt:5];[other setMyInt:10];NSLog(@"%i\n", [obj myInt]);
Saturday, March 9, 13
![Page 39: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/39.jpg)
One More ThingImports and Forward Declarations
Saturday, March 9, 13
![Page 40: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/40.jpg)
Imports
• Objective-C introduces the #import Preprocessor Directive#import "MyHeaderFile.h"
#import <SystemHeader.h>
• Guarantees the file is only included once
• Better Practice than C’s #include
Saturday, March 9, 13
![Page 41: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/41.jpg)
Forward Declarations
• A Promise to the Compiler that a Class or Protocol will exist at Compile Time
@class PromisedObject;
@protocol PromisedProtocol;
• Minimizes the amount of #includes
Saturday, March 9, 13
![Page 42: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/42.jpg)
Objective-C DataWhat you get with the Foundation Framework
Saturday, March 9, 13
![Page 43: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/43.jpg)
nil
• Represents the absence of an object
• Messages passed to nil return nil
• nil is for objects, NULL is for C pointers
Saturday, March 9, 13
![Page 44: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/44.jpg)
YES and NO
• Boolean type BOOL for “true” and “false”
• Use BOOL with YES and NO
• Dont use _Bool or bool with true and false from stdbool.h
Saturday, March 9, 13
![Page 45: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/45.jpg)
NSNumber
• Object Representation for Integers, Booleans, Floats, Doubles, Characters, etc.
• Object Literal Syntax@1 @1.0 @YES @NO @(1+2) @'a'
Saturday, March 9, 13
![Page 46: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/46.jpg)
NSString
• Immutable string
• NSMutableString for mutable strings
• Object Literal Syntax@"Hello World!" @("Hello World!")
Saturday, March 9, 13
![Page 47: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/47.jpg)
NSArray
• Immutable object array
• NSMutableArray for mutable arrays
• Object Literal Syntax@[object1, object2, ..., objectN];
• Object Subscripting Syntaxarray[0] = object;id object = array[0];
Saturday, March 9, 13
![Page 48: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/48.jpg)
NSDictionary
• Immutable object dictionary
• NSMutableDictionary for mutable
• Object Literal Syntax@{key1:value1, key2:value2, ...};
• Object Subscripting Syntaxdictionary[key] = object;id object = dictionary[key];
Saturday, March 9, 13
![Page 49: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/49.jpg)
Implementing Array Subscripting
• Accessing ObjectsobjectAtIndexedSubscript:
• Setting ObjectssetObject:atIndexedSubscript:
• The index subscript must be an integral
Saturday, March 9, 13
![Page 50: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/50.jpg)
Implementing Dictionary Subscripting
• Accessing Object values with key ObjectsobjectForKeyedSubscript:
• Setting Object values with key ObjectssetObject:forKeyedSubscript:
Saturday, March 9, 13
![Page 51: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/51.jpg)
Forin Loops
• Loop over the contents of a collection
• Foundation Framework collectionsNSArray NSDictionary
• Any implementation of the NSFastEnumeration Protocol
• Any Subclass of NSEnumerator
Saturday, March 9, 13
![Page 52: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/52.jpg)
Forin Loops Example
NSArray *array = @[@1, @2, @3, @4, @5];
for (NSNumber *num in array) { NSLog(@"%@", num);}
for (id num in [array reverseObjectEnumerator]) { NSLog(@"%@", num);}
Saturday, March 9, 13
![Page 53: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/53.jpg)
Objective-C Blocks
• Functions you can declare within functions
• Can close-over variables for later use
• Can pass block functions around like data
Saturday, March 9, 13
![Page 54: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/54.jpg)
Block Syntax
• Similar to C Function Pointer Syntax
return_type (^name)(parameter types) =! ^(parameter list) {! ! // do stuff! };
return_type (^name)() = ^{! ! // do stuff! ! // takes no parameters! };
Saturday, March 9, 13
![Page 55: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/55.jpg)
Block Exampleint multiplier = 5;int (^mult)(int) = ^(int num){ return num * multiplier;};
int num = mult(5); // num = 25
__block int number = 0;void (^increment)() = ^{ number++;};
increment(); // number = 1increment(); // number = 2
The block itself has read access to variables defined in the lexical scope at the creation of the block.
The block can close over outside variables and modify their values by declaring the variable with the __block modifier.
Saturday, March 9, 13
![Page 56: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/56.jpg)
Type-Safe Enums• Enums that you can declare the type they enumerate over
• int, char, unsigned char, NSUInteger, ...
• Syntax similar to Object Subclassingtypedef enum MyEnum : NSUInteger { A, B, C} MyEnum;
enum MyUnsignedCharEnum : unsigned char { FIRST, SECOND, THIRD};
typedef enum MyUnsignedCharEnum MyUnsignedCharEnum;
typedef enum : NSUInteger { ONE, TWO, THREE} AnotherEnum;
Saturday, March 9, 13
![Page 57: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/57.jpg)
Automatic Reference Counting
What it is, and what it means to you
Saturday, March 9, 13
![Page 58: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/58.jpg)
Referencing Counting
• Nearly-Manual Memory Management
• Objects have a counter showing how many references are using them
• Retain Objects when you receive them[object retain];
• Release Objects when you’re done using them[object release];
• Objects deallocate themselves when their retain count reaches 0
Saturday, March 9, 13
![Page 59: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/59.jpg)
Automatic Reference Counting (ARC)
• Almost Compile Time Garbage Collection
• Retain and Release messages are added at compile time by the compiler
• ARC manages when to do this for you
• strong variables are Retained when assigned
• weak variables are not Retained on assignment and are zeroed out when deallocated
Saturday, March 9, 13
![Page 60: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/60.jpg)
Retain Cycle
• Two Objects Reference Each Other
• Their retain counts can never reach 0
• Set one of the references to weak to prevent one of the objects from retaining the other and causing a cycle
Saturday, March 9, 13
![Page 61: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/61.jpg)
So there you have itObjective-C thrown at you
Saturday, March 9, 13
![Page 62: Objective-C A Beginner's Dive](https://reader034.vdocuments.mx/reader034/viewer/2022042623/544592eeb1af9fd7068b45bb/html5/thumbnails/62.jpg)
Useful References• Programming with Objective-C
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html
http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ProgrammingWithObjectiveC.pdf
• Objective-C Cheat Sheet and Quick Referencehttp://cdn5.raywenderlich.com/downloads/RW-Objective-C-Cheatsheet-v1_2.pdf
• Coding Guidelines for Cocoahttp://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.pdf
• Ry’s Objective-C Tutorialhttp://rypress.com/tutorials/objective-c/index.html
Saturday, March 9, 13