Objective-C Flashcards

1
Q

Create a UIViewController manually

A

UIViewController *vc = [[UIViewcontroller alloc]init];

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

Present a UIViewController (from within another VC)

A

[self presentViewController:vc animated:YES completion:nil];

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

What is the compiler flag that indicates ARC is not in use?

A

-fno-objc-arc compiler flag

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

Stack vs. Heap?

A

Stack: compiler managed, variables from functions stored, LIFO, faster access.

Heap: user managed area, no size limit, slower access, fragmentation

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

Name some basic types

A

Basic: int, float, char, unsigned char, unsigned int, short, long, unsigned long, unsigned short

Void

Derived: pointer, array, structure, union, function types.

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

Create a constant

A
#define LENGTH 10
OR
const int LENGTH 10;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How much space do the following take:

float
double
long double

A

Float: 4 bytes (32 bits)
Double: 8 bytes (64 bits)
Long Double: 10 bytes (80 bits)

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

How much space do the following take:

char
unsigned char
signed char
int
unsigned int
short
long
A
char: 1 byte
unsigned char: 1 byte
signed char: 1 byte
int: 2 or 4 bytes
unsigned int: 2 or 4 bytes
short: 2 bytes
long: 4 bytes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Does NSInteger require memory management?

A

No, datatypes like NSInteger, NSUinteger, and BOOL aren’t objects and don’t need memory management.

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

initialize an array of size 5 of ints

A

int arr[5] = {1,2,3,4,5};

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

Pass an array of ints to a function (give the signature)

A

-(void) myFunc: (int *)arr{

for (int i=0;i

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

Create an array that can be modified

A

NSMutableArray *myArr = [[NSMutableArray alloc] init];

OR

NSmutableArray *myArr = [NSMutableArray array];

OR

[[NSMutableArray alloc] initWithCapacity:10];

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

Create an array that can be modified of an exact size

A

myArr = [[NSMutableArray alloc] initWithCapacity:10];

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

Return an array from a function

A
  • (int *) myFuncThatReturnsArray(){…}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Iterate an array with a pointer

A
// set pointer to start of array
double balance[10];
double *p = balance;

for (int i=0;i

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

How do you add an NSInteger or an int to an NSMutableArray?

A

You can’t, the NSInteger isn’t an object and can’t be added. You CAN convert the NSInteger to an NSNumber object type then add.

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

Assign a pointer to an integer and print to output.

A

int a = 1;
int *p = &a;

NSLog(@”%d”, *p);

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

Check if a pointer is null.

Check if it’s not null.

A

if (pointer){…} //if pointer is not null it succeeds

if (!pointer){…} // if pointer is null it succeeds

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

What’s the difference between declaring an object id or NSObject?

A

With a variable typed id, you can send it any known message and the compiler will not complain.

With a variable typed NSObject *, you can only send it messages declared by NSObject (not methods of any subclass) or else it will generate a warning.

Id is the most generic declaration of an object type.

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

If a pointer is to an integer at space ‘1000’ and you increment its value by 1, what memory space does it point to?

A

It will point to memory space ‘1004’ because integers are 4 bytes.

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

If a character pointer is incremented, what memory space does it point to?

A

It will point to ‘1001’ because chars are 1 byte.

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

Set a pointer to the end of an array of integers and then decrement it to iterate backwards through the array..

A

int myArr[5] = {1,2,3,4,5};
int *endPtr = &myArr[4];

for (int i = 5; i>0;i–){
NSLog(@”%d”, *endPtr);
endPtr–;

}

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

Can pointers be compared with , ==, etc?

A

Yes, the actual address in the heap is being compared.

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

How would you loop on an array using only pointers?

A

int *ftPtr;
int *bkPtr;

int myArr[3] = {1,2,3};

ftPtr = &myArr;
bkPtr = &myArr[2]

while(ftPtr <= bkPtr){
NSLog(@”%d”,*ftPtr);
ftPtr++
}

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

How would you declare an array of pointers to strings?`

A

char *myStr = “Hello”;

char *stringPtrs[100];
stringPtrs[0] = myStr;

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

Compare strings in C

A

char *str1 = “Hello”;
char *str2 = “Hello”;

strcmp(str1,str2); // returns negative if str1 < str2, 0 if equal, and > 0 if str1 greater than str2

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

Get string length in C

A

strlen(str1);

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

Print the 4th character in a string in C

A

char *myStr = “Hello”;
NSLog(@”%c”, myStr[3]);

OR

NSLog(@”%c”, *(myStr + 3));

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

Attach two strings together

A

char *str1 = “hello”;
char *str2 = “goodbye”;

strcat(str1,str2); //str1 now contains “hello goodbye”

The destination array must be large enough to hold combined strings…thus, the above example would crash.

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

Declare a string of size 20 and initialize it

A

char myStr[20] = “Hello”;

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

Pass an array of characters to a function

A

-(void) myFunction:(char []) myStr{

}

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

What is a pointer to a pointer?

A

References the address of the second pointer which, in turn, points to the location that contains the actual values.

33
Q

What’s the use of pointer to pointers?

A

Modify strings by Reference in a function:

void myFunc(char **str){
   strcpy(str, "Second");
}

Traverse array of pointers to strings (char arrays):

-(int) wordsInSenetnce:(char **)testwords{
while (*testwords){ //pointer not nil
testWords++;

}
}

34
Q

How are strings represented in Objective-C?

A

NSString and NSMutableString

35
Q

Name some important NSString methods?

A

NSString *myStr = @”Hello”;

[myStr capitalizedString];
[myStr lowercasedString];
[myStr hasPrefix:@"Pre"];
[myStr hasSuffix:@"Suffix"];
[myStr isEqualToString:@"Equal"];

[myStr length];

36
Q

Declare a structure named Books with a title, author, subject, and book ID then create an instance of it.

A
struct Book{
  NSString *title;
  NSString *author;
  NSString *subject;
  int bookID;
}

struct Book book1;

37
Q

Declare a function that can take a Book structure in as a parameter

A
  • (void) structChanger:(struct Book*)book{book->author = @”John”;
    book->name = @”Right!”;

}

Declare a typedef for the structure if you want to not use the keyword struct in the method signature.

typedef struct Book{
NSString *title;
} Book;

38
Q

If you have a struct pointer how do you access an element?

A

with the arrow -> operator.

typedef struct Book{
NSString *author;
} Book;

Book book;
Book *bookPtr = &book;
bookPtr->author = @”John”;

39
Q

Objective-C uses call-by-reference or call-by-value when passing arguments to method calls?

A

Call-by-value

40
Q

What’s the signature of an Objective-C method?

A
  • (return_type) method_name:( argumentType1 )argumentName1
    joiningArgument2:( argumentType2 )argumentName2 …
    joiningArgumentn:( argumentTypen )argumentNamen {
    body of the function
    }
41
Q

What does a method definition that starts w/+ mean?

A

class method: can be referenced anytime by referencing the class.

42
Q

What does a method definition that starts w/- mean?

A

instance method: can be accessed only by an instance of the class.

43
Q

Difference between “methods” and “functions”?

A

Methods pass messages and have bracket syntax. Functions are like standard C w/normal signature.

void SomeFunction(SomeType value);

vs.

  • (void)someMethodWithValue:(SomeType)value;
44
Q

Whats the difference between closures in Swift and Blocks in Objective-C

A

Same except variables are copied and not mutable unless specified in Obj-C with the keyword __block.

Closures and Obj-C blocks are compatible and can be passed back and forth.

45
Q

What is the keyword to add to a variable in a block’s scope so its value can be modified within the block’s scope?

A

__block int anInteger = 42;

46
Q

Declare a block variable named “multiplyTwoValues” that does what the name says and returns a Double

A

double (^multiplyTwoValues)(double, double);

47
Q

Declare block block variable named “multiplyTwoValues” that returns a double and assign a block to it. Then call it.

A

double (^multiplyTwoValues)(double, double) = ^(double firstVal, double secondVal){
return firstVal * secondVal;
};

multiplyTwoValues(1.0,2.0);

48
Q

What’s the advantage of a callback block over a delegate to communicate information back?

A

The callback block is defined where the task is defined so the logic is in a single spot.

49
Q

Declare a method that takes a callback block with no parameters or return values?

A

-(void) beginTaskWithCallbackBlock:(void (^)(void))callbackBlock;

__________
Implemented:
- (void)beginTaskWithCallbackBlock:(void (^)(void))callbackBlock {

...

callbackBlock();

}

50
Q

Declare block with 2 double parameters and no return?

A

-(void)doSomethingWithBlock:(void (^) (double, double))myBlock;

__________
Implemented:

  • (void)doSomethingWithBlock:(void (^)(double, double))myBlock {…myBlock(2.0,5.0);

}

51
Q

How do you ensure a block doesn’t have a retain cycle?

A

Don’t get a strong reference to self. Do not reference self inside the block. Instead, use the __weak keyword:

XYZBlockKeeper * __weak weakSelf = self;

self.block = ^{
[weakSelf doSomething];
}

52
Q

Convert this to a block:

[self testTheBlock:
 void helloBlockWorld() {
 NSLog(@"Hello Block World!");
 }
];
A

1) remove the return (void)
2) remove the name of the function and replace with ^
()
[self testTheBlock:
^(){
NSLog(@”Hello Block World!”);}
];

53
Q

Convert this to a block:

NSString* helloBlockWorld2(NSString *greeting) {
return [NSString stringWithFormat:@”%@ Block World!”, greeting];
}

A

^(NSString *greeting){
return [NSString stringWithFormat:@”%@ Block World!”, greeting];

}

54
Q

Compare the syntax of a block to a method call

A

( , … )
i.e., BOOL isAdmin(User aUser)

^) (, …
i.e., BOOL(^)(User)

55
Q

What are two ways to declare a block?

A

“Anonymous”
^(int fVal){

	 return 1;
};

“Named”
int (^tester)(int) = ^(int fVal){

	return 1
};
56
Q

(NON-ARC) In the following example is the instance of myObject released?

MyObject *myObject = [[MyObject alloc]init];

NSMutableArray *myArray = [NSMutableArray alloc]initWithObjects:myObject];

[myObject release];

A

No, the array still holds a reference to the object and keeps it in memory.

57
Q

What is a memory leak?

A

Allocating memory and forgetting to deallocate it. (setting a pointer to memory using alloc but never sending a release message)

Enough leaked memory the application will run out and the program will crash.

58
Q

(NON-ARC) What does autorelease do?

A

an object, when receiving an autorelease message, will be deallocated with the next destruction of the autorelease pool (later in the run loop).

59
Q

When is memory management handled with ARC?

A

At compile time instead of runtime like garbage collection.

60
Q

What does a protocol do?

A

Sets a list of behaviors that an object is expected to conform too.

61
Q

What is CLANG?

A

the compiler frontend for C, C++, Objective-C, etc.

62
Q

What is the @dynamic keyword?

A

Some accessors are created dynamically at runtime, such as certain ones used in CoreData’s NSManagedObject class. If you want to declare and use properties for these cases, but want to avoid warnings about methods missing at compile time, you can use the @dynamic directive instead of @synthesize.

Using the @dynamic directive essentially tells the compiler “don’t worry about it, a method is on the way.”

63
Q

Where do you enable ARC?

A

Project->Target->Build Settings->Automatic Reference Counting->Apple Clang Language-Objective-C->Automatic Reference Counting

64
Q

What is the difference between using new and alloc-init?

i.e.,

MyObject *obj = [[NSObject alloc]init];
vs.
MyObject *obj = [NSObject new];

A

New calls the init method and alloc->alloc.

+(id)alloc{
NSLog(@”Allocating…”);
return [super alloc];
}

-(id)init{
NSLog(@”Initializing…”);
return [super init];
}

65
Q

Import UIKit

A

import

66
Q

What is the id keyword?

A

It’s a generic type that can be set to anything. No need for using a pointer. Can be used polymorphically:

Ex.

id datavalue;

Fraction *f1 = [[Fraction alloc]init];
Complex *c1 = [[Complex alloc]init];

// Calls the corresponding print method
// polymorphic
datavalue = f1;
[datavalue print];
data value = c1;
[datavalue print];
67
Q

When are the type tests run on type id?

A

Runtime

68
Q

Why wouldn’t you declare most objects of type id?

A

Compiler doesn’t do type checking and thus can’t tell if method calls are incorrect, etc. Static typing also makes the program more readable.

69
Q

What is an Objective-C class broken into?

A

Class implementation and class Interface.

70
Q

Name some collections

A

NSSet
NSDictionary
NSArray

NSMutableArray
NSMutableDictionary
NSMutableSet

71
Q

Initialize an NSSet with some strings then iterate it

A

NSSet *mySet = [NSSet setWithObjects:@”String 1”, @”String 2”, @”Whatever”, nil];

for (id value in mySet){
NSLog(@”%@”,value);
}

72
Q

What is a way to initialize an NSArray or NSMutable array with initial values?

A

NSArray *arr = @[@1,@2,@3,@4,@5];

OR

NSArray *oldStyle = [NSArray arrayWithObjects: @”Zero”, @”One”,
@”Two”, nil];

73
Q

Initialize an NSSet with an NSArray

A

NSSet *mySet = [NSSet setWitArray:@[@1,@2,@3]];

74
Q

Create a mutable set and add then remove an object

A

NSMutableSet *mySet = [[NSMutableSet alloc]init];
[mySet addObject: testString];
[mySet removeObject:testString];

75
Q

Create a mutable array then add and remove an object

A

NSMutableArray *myArr = [[NSMutableArray alloc] init];
[myArr addObject: testString];
[myArr insertObject:@”Hello”, atIndex:1];
[myArr removeObjectAtIndex:1];

76
Q

How to empty a NSMutableArray?

A

removeAllObjects

NSMutableArray *myArr = [[NSMutableArray alloc]init];
[myArr addObject: @”HELLO”];
[myArr addObject: @”GOODBYE”];
[myArr removeAllObjects];

77
Q

Remove the element at the end of a Mutable array

A

removeLastObject

[myArr removeLastObject];

78
Q

What’s the method to add an object and key to a NSMutableXitionary? (Create a NSMutableDictionary and add an Object to it.)

A

setObject:forKey

NSMutableDictionary *testDictionary = [[NSMutableDictionary alloc] init];

[testDictionary setObject:[NSNumber numberWithFloat:1.23f]
forKey:[NSNumber numberWithInt:1]];

NSLog(@”Test dictionary: %@”, testDictionary);