Objective C Flashcards

1
Q

How is Objective C described?

A
  • C plus objects
  • A strict superset of C
  • fabricated in C to build larger-scale assemblies
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two object-oriented frameworks in objective C?

A
  • Foundation: classes for basic data structures and for interacting with OS
  • AppKit: classes for developing applications, windows, buttons, etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What did Objective C 2.0 add?

A
  • Garbage collection
  • Syntax enhancements
  • Runtime performance improvements
  • 64-bit support
  • Features such as properties and fast enumerators
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between #import and #include?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the functions, methods and messages calls in all C types?

A

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];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What wrapper classes does the foundation framework provide?

A
  • NSNumber (int, float, double, boolean)
  • NSString and NSMutableString (Unicode strings (usually UTF-16))
  • NSArray and NSMutableArray (dynamic arrays)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Where is memory for objects allocated?

A

It is always allocated in the heap and never in the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is literal syntax?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How are header files and implementation files structured?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you access NSObject?

A

import <Foundation/Foundation.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What should all classes extend and why?

A

NSObject as the class inherits alloc, init and new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What makes a class method different to an instance method?

A

A class method cannot access attributes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How are Objective C constructors called?

A

Method name always starts with init and is called after alloc
NSNumber *anum = [[NSNumber alloc] initWithInteger:@43];
NSNumber *pi = [[NSNumber alloc] initWithFloat:@3.14159];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is id?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How are destructors called?

A
  • Always called dealloc
  • Always called automatically by the runtime
  • Never explicitly called
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is MMR?

A

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

17
Q

What is reference counting?

A
  • 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.
18
Q

What are the reference counting methods?

A
  • 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
19
Q

When is the best time to use autorelease?

A

You use autorelease when you need to send a deferred release message, typically when returning an object from a method

20
Q

What is ARC?

A

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