Category: iOS
-
Coding question print binary tree by level in Objective C
This question was previously asked on the Facebook interview. Given input as a binary tree print to console each level followed by new line character. Input: Output: “A\n BC\n DEF” Solution: Do a breadth first search algorithm on a graph which will go over an entire tree and will build a resulted string. First of…
-
Difference between formal and informal protocols
An informal protocol is a Category on NSObject. Implementation of the methods is optional. @interface NSObject(NSApplicationNotifications) – (void)applicationWillFinishLaunching:(NSNotification *)notification; … @interface NSObject(NSApplicationDelegate) – (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender; … Formal protocols are an Extension to the Objective-C language. Methods can be required or optional. @protocol YourElementsViewDataSource – (NSUInteger)numberOfElements; – (CGFloat)sizeOfElementAtIndex:(NSUInteger)segmentIndex; @optional – (NSString *)titleForElementAtIndex:(NSUInteger)segmentIndex; – (BOOL)shouldExplodeElementAtIndex:(NSUInteger)segmentIndex; @required -…
-
Difference between shallow copy and deep copy
Shallow copy only copies a memory address. Changing one object will also change another. NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects:@1, @2, @3, nil]; NSMutableArray *secondArray = [[NSMutableArray alloc] initWithObjects:@4, @5, @6, nil]; secondArray = firstArray; [secondArray setObject:@9 atIndexedSubscript:0]; NSLog(@”%@”, [firstArray objectAtIndex:0]); //9 the value in a first array has also changed Deep copy copies the…
-
Difference between frame and bound
Frame is a CGRect that is relative to it’s superview and bound is a CGRect that is relative to it’s own coordinate system. Practically (x,y) origin of bound would always be (0,0).