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