Objective-C Flashcards
Create a UIViewController manually
UIViewController *vc = [[UIViewcontroller alloc]init];
Present a UIViewController (from within another VC)
[self presentViewController:vc animated:YES completion:nil];
What is the compiler flag that indicates ARC is not in use?
-fno-objc-arc compiler flag
Stack vs. Heap?
Stack: compiler managed, variables from functions stored, LIFO, faster access.
Heap: user managed area, no size limit, slower access, fragmentation
Name some basic types
Basic: int, float, char, unsigned char, unsigned int, short, long, unsigned long, unsigned short
Void
Derived: pointer, array, structure, union, function types.
Create a constant
#define LENGTH 10 OR const int LENGTH 10;
How much space do the following take:
float
double
long double
Float: 4 bytes (32 bits)
Double: 8 bytes (64 bits)
Long Double: 10 bytes (80 bits)
How much space do the following take:
char unsigned char signed char int unsigned int short long
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
Does NSInteger require memory management?
No, datatypes like NSInteger, NSUinteger, and BOOL aren’t objects and don’t need memory management.
initialize an array of size 5 of ints
int arr[5] = {1,2,3,4,5};
Pass an array of ints to a function (give the signature)
-(void) myFunc: (int *)arr{
for (int i=0;i
Create an array that can be modified
NSMutableArray *myArr = [[NSMutableArray alloc] init];
OR
NSmutableArray *myArr = [NSMutableArray array];
OR
[[NSMutableArray alloc] initWithCapacity:10];
Create an array that can be modified of an exact size
myArr = [[NSMutableArray alloc] initWithCapacity:10];
Return an array from a function
- (int *) myFuncThatReturnsArray(){…}
Iterate an array with a pointer
// set pointer to start of array double balance[10]; double *p = balance;
for (int i=0;i
How do you add an NSInteger or an int to an NSMutableArray?
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.
Assign a pointer to an integer and print to output.
int a = 1;
int *p = &a;
NSLog(@”%d”, *p);
Check if a pointer is null.
Check if it’s not null.
if (pointer){…} //if pointer is not null it succeeds
if (!pointer){…} // if pointer is null it succeeds
What’s the difference between declaring an object id or NSObject?
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.
If a pointer is to an integer at space ‘1000’ and you increment its value by 1, what memory space does it point to?
It will point to memory space ‘1004’ because integers are 4 bytes.
If a character pointer is incremented, what memory space does it point to?
It will point to ‘1001’ because chars are 1 byte.
Set a pointer to the end of an array of integers and then decrement it to iterate backwards through the array..
int myArr[5] = {1,2,3,4,5};
int *endPtr = &myArr[4];
for (int i = 5; i>0;i–){
NSLog(@”%d”, *endPtr);
endPtr–;
}
Can pointers be compared with , ==, etc?
Yes, the actual address in the heap is being compared.
How would you loop on an array using only pointers?
int *ftPtr;
int *bkPtr;
int myArr[3] = {1,2,3};
ftPtr = &myArr; bkPtr = &myArr[2]
while(ftPtr <= bkPtr){
NSLog(@”%d”,*ftPtr);
ftPtr++
}
How would you declare an array of pointers to strings?`
char *myStr = “Hello”;
char *stringPtrs[100];
stringPtrs[0] = myStr;
Compare strings in C
char *str1 = “Hello”;
char *str2 = “Hello”;
strcmp(str1,str2); // returns negative if str1 < str2, 0 if equal, and > 0 if str1 greater than str2
Get string length in C
strlen(str1);
Print the 4th character in a string in C
char *myStr = “Hello”;
NSLog(@”%c”, myStr[3]);
OR
NSLog(@”%c”, *(myStr + 3));
Attach two strings together
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.
Declare a string of size 20 and initialize it
char myStr[20] = “Hello”;
Pass an array of characters to a function
-(void) myFunction:(char []) myStr{
}