If you like my tutorials, you will love my book . It is chockful of advanced programming techniques and the only comprehensive barcode reference for serious iOS developers.
Our DNA is written in Objective-C

"Are You a Cocoa Crack?" Quiz (1)

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;

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;

Variant 2:

@interface CocoaQuizViewController : UIViewController
	IBOutlet UILabel *myLabel;
@property (nonatomic, retain) IBOutlet UILabel *myLabel;

Variant 3:

@interface CocoaQuizViewController : UIViewController
	UILabel *myLabel;
@property (nonatomic, retain) IBOutlet UILabel *myLabel;

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?


@interface MeasureLinealView : UIView
	NSUInteger minValue;
@property (nonatomic, assign) NSUInteger minValue;


#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 ( and I will publish them in this format.

Categories: Q&A


  1. 4/5 correct!
    For the last one I highlighted the answer, but it was really obvious… :-(

  2. Oliver great quiz. Keep them coming! (BTW I only got 2/5 correct – shame on me!)


  3. 4/5 correct, didn’t know 4th one (thanks for the info :-))

  4. 3/5 – Doh!

    Would love to see more of these!

  5. Hi, nice quiz, I’m just starting out, so am too embarrassed to quote my score, but would love to see more of these quizzes!

    Just a quick question. In the answer to the final one you write “minVar”, do you mean “minValue”?

  6. You are correct. I fixed that.

  7. I got all 5 at least mostly correct. For instance, I wasn’t sure if it was intValue or integerValue, etc. But I didn’t know all the details of all of them, so the answers were informative!

  8. 2 right, 1partially right. And one that baffled me even though the answer was so obvious once I saw it.

  9. I got 4/5 correct. I didn’t realize that Apple recommends variant 3. Most of the tutorials and books show variant 2.

  10. failed only 4th Quiz since I prefer to code all interface =)
    The rest questions are fairly simple.


  1. “Are You a Cocoa Crack?” Quiz (2) @ Dr. Touch
  2. iOS Developer Challenge | Cocoanetics

Leave a Comment

%d bloggers like this: