05 May 2013, 02:10
Generic-user-small

João Melo (1 post)

Hi there,

My name is João and i’m going crazy with a line of code in the book.

The line is “[[segue destinationViewController] setRecipeMO:newMO];”. It appears on Section A1.2 (pg 213 in pdf), is the last line of the method “prepareForAddRecipeSegue” in “PPRMasterViewController.m”.

I can’t understand how you can call that setRecipeMO message since destinationViewController returns a id. Wouldn’t that return a compile error?

Yes, i looked in the example project and it compiles perfectly. It is driving me crazy :)

Thanks.

06 Dec 2013, 20:24
Avatarsmall_pragsmall

Marcus S. Zarra (253 posts)

id is the generic, universal object pointer. You can send any message to it and it will compile. Naturally if the target doesn’t respond to that message then you will crash, at runtime.

Objective-C was originally designed to be very loose at compile time and then strict at run time. This allowed for the language to be far more dynamic and loosely typed than other languages. Sadly that has been going away in popularity in recent years but the underlying structure is still there.

You can send any message to id and as long as the compiler can find that message structure in a header file somewhere it will compile. Making sure it works is up to the developer :)

  You must be logged in to comment