Objective C Flashcards
How is Objective C described?
- C plus objects
- A strict superset of C
- fabricated in C to build larger-scale assemblies
What are the two object-oriented frameworks in objective C?
- Foundation: classes for basic data structures and for interacting with OS
- AppKit: classes for developing applications, windows, buttons, etc
What did Objective C 2.0 add?
- Garbage collection
- Syntax enhancements
- Runtime performance improvements
- 64-bit support
- Features such as properties and fast enumerators
What is the difference between #import and #include?
In a C header link file, the #include guard is required for any # statements. In an Objective C header link file, #import guard is an implicit #include guard
What are the functions, methods and messages calls in all C types?
C function call:
myFunction(parameter1,parameter2);
C++ method call:
MyObject *obj = new MyObject;
obj->myMethod(parameter1,parameter2);
Objective-C messaging:
MyObject *obj = [MyObject new];
[obj callWith:parameter1 and:parameter2];
What wrapper classes does the foundation framework provide?
- NSNumber (int, float, double, boolean)
- NSString and NSMutableString (Unicode strings (usually UTF-16))
- NSArray and NSMutableArray (dynamic arrays)
Where is memory for objects allocated?
It is always allocated in the heap and never in the stack
What is literal syntax?
It makes creating new objects easier
Without literal syntax:
BOOL b = NO;
NSNumber *num1 = [[NSNumber alloc] initWithInteger:43];
NSNumber *num2 = [[NSNumber alloc] initWithChar:’A’];
NSNumber *num3 = [[NSNumber alloc] initWithFloat:91.6];
NSNumber *num4 = [[NSNumber alloc] initWithBool:b];
With literal syntax:
BOOL b = YES;
NSNumber *num1 = @43;
NSNumber *num2 = @’A’;
NSNumber *num3 = @91.6;
NSNumber *num4 = @b;
This also works with expressions
How are header files and implementation files structured?
header file: MyClass.h
<import>
@interface <classname> : <superclass>
{
<attribute>
}
<method>
@end
Method definitions are like function prototypes
implementation file: MyClass.m
<import>
@implementation <classname>
<method>
@end
</method></classname></import></method></attribute></superclass></classname></import>
How do you access NSObject?
import <Foundation/Foundation.h>
What should all classes extend and why?
NSObject as the class inherits alloc, init and new
What makes a class method different to an instance method?
A class method cannot access attributes
How are Objective C constructors called?
Method name always starts with init and is called after alloc
NSNumber *anum = [[NSNumber alloc] initWithInteger:@43];
NSNumber *pi = [[NSNumber alloc] initWithFloat:@3.14159];
What is id?
A datatype that is a pointer to any type of object. It supports dynamic referencing which means that the object type is defined at runtime.
How are destructors called?
- Always called dealloc
- Always called automatically by the runtime
- Never explicitly called
What is MMR?
Manual Retain-Release:
- You explicitly manage memory by keeping track of how many owners an object has
- Implemented using a model called reference counting which is provided by NSObject and the Objective-C runtime
What is reference counting?
- Create a new object and reference counter is set to 1
- You claim ownership of an object and reference count is incremented by 1
- You relinquish ownership of an object and reference count is decremented by 1
An object exists while reference count is greater than zero. An object is destroyed when the reference count becomes zero.
What are the reference counting methods?
- alloc: Create an object and claim ownership of it. Set reference count to 1.
- retain: Claim ownership of an existing object. Increase reference count by 1.
- copy:Copy an object and claim ownership of it. Set reference count to 1.
- release: Relinquish ownership of an object. Decrement its reference count by 1.
- autorelease: Relinquish ownership of an object but defer its destruction.
Each alloc/retain/copy must have an associated release/autorelease to
avoid memory leaks
When is the best time to use autorelease?
You use autorelease when you need to send a deferred release message, typically when returning an object from a method
What is ARC?
Automatic reference counting:
- needs to be switched on in Xcode
- works the same way as manual reference counting, except it automatically inserts appropriate retain and release calls
- means you must not explicitly call retain, release or autorelease
- means you usually do not need a custom dealloc method