These questions will show if you are really the Cocoa Crack you like to believe to be. To see the answer highlight the answer text with your mouse. No peeking! Respond in the comments how many you got correct.
Quiz: Warm Up. Why can you not add an NSInteger to an NSArray? With which extra step can you do it?
Answer: NSInteger is only a different name for a signed integer. It is not a class even though the name might suggest it is. Only instances of classes can be added to NSArrays. The extra step mentioned is to first create an NSNumber object from the integer which you can add to an NSArray.
Quiz: I have an outlet defined and my code compiles without error or warning. But when I try to connect in in Interface Builder it does not show up. Why?
@interface CocoaQuizViewController : UIViewController
{
UILabel IBOutlet *myLabel;
}
@property (nonatomic, retain) UILabel IBOutlet *myLabel;
@end |
Answer: The IBOutlet is at the wrong position. It needs to be before the type UILabel. IBOutlet itself is defined to be replaced with empty text for compiling, that’s why it does not cause a syntax error. But Interface Builder is looking for this keyword in header files to know what outlets there are.
Quiz: What does the following code do? Variable age is defined as NSInteger. Is the syntax correct or is there something missing? It compiles fine, but why does it crash?
myLabel.text = age?[NSString stringWithFormat:@"%@", age]:@""; |
Answer: If the variable age is not equal to zero then the result of stringWithFormat will be assigned to the label text. If it is zero then an empty string will be assigned. The format argument is wrong, %@ gets the description of an instance. Since age is probably a number the format argument needs to be %d. It crashes because objC tries to access the memory at position “age” which the app does not own and therefore causes an exception.
Quiz: The following three methods all allow you to connect the outlet in Interface Builder. What is the technical difference between them and which is the method recommended by Apple?
Variant 1:
@interface CocoaQuizViewController : UIViewController
{
IBOutlet UILabel *myLabel;
}
@end |
Variant 2:
@interface CocoaQuizViewController : UIViewController
{
IBOutlet UILabel *myLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end |
Variant 3:
@interface CocoaQuizViewController : UIViewController
{
UILabel *myLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end |
Answer: Method 3 is the correct one that should be preferred according to Apple. The instance that gets loaded from the XIB gets retained, just in case which prevents accidental unloading in case of low memory. Method 2 has a superfluous IBOutlet next to the instance variable because in this case the compiler will use the property anyway. Method 1 also can be connected, but it does not retain the loaded instance. This might cause problems if the instance gets deallocated for some reason.
Quiz: Complete this code so that it becomes syntactically correct using no more than one additional word and two square brackets.
NSInteger myNumber = @"1000"; |
Answer: @”1000″ is in itself a NSString instance. Therefore you can use it as receiver for the method integerValue. NSInteger myNumber = [@”1000″ integerValue];
Quiz: (Bonus Question) Why does the following code crash as soon as the property is set? What is the simple fix?
MeasureStripView.h
@interface MeasureLinealView : UIView
{
NSUInteger minValue;
}
@property (nonatomic, assign) NSUInteger minValue; |
MeasureStripView.m
#import "MeasureStripView.h"
@implementation MeasureStripView
@synthesize minValue;
#pragma mark Passthrough Properties
- (void) setMinValue:(NSUInteger)aVal
{
// this overriding is absolutely necessary
self.minValue = aVal;
} |
Some other file
MeasureStripView *myMS; // assume it is properly initialized and otherwise working
myMS.minValue = 100; // this line crashes. If commented out the app works fine |
Answer: Using self.minValue as an lvalue cases the setter method setMinValue to be called. This causes an endless loop running out of memory eventually. The fix is to remove the “self.” inside the setter because minValue is an instance variable and thus accessible just by it’s name throughout the class.
So how many did you catch? Be honest! If you also have a Cocoa riddle like these to contribute please mail them directly to me (oliver@drobnik.com) and I will publish them in this format.