Note that there are some explanatory texts on larger screens.

plurals
  1. POid type instance variable sending messages to instance methods
    text
    copied!<p>my first question on stackoverflow so please be gentle. I've tried searching for answers but I really need help with this.</p> <p>The issue is with learning about delegates from Neal Goldstein's Objective-C for Dummies</p> <p>He has the following in <strong>Transaction.h</strong></p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @class Budget; @interface Transaction : NSObject { Budget *budget; double amount; NSString *name; id delegate; } //some init method @end @protocol TransactionDelegate @required - (void) spend: (Transaction *) aTransaction; //additional optional method @end -- </code></pre> <p>//and then in <strong>Transaction.m</strong> he has this</p> <pre><code>#import "Transaction.h" #import "Budget.h" @implementation Transaction @synthesize budget, delegate , amount; - (void) spend { if ([delegate respondsToSelector:@selector(spend:)]) [delegate spend:self]; } - (id) initWithAmount: (double) theAmount forBudget: (Budget*) aBudget { if (self = [super init]) { budget = aBudget; [budget retain]; amount = theAmount; } return self; } - (void) dealloc { [budget release]; [super dealloc]; } @end </code></pre> <p>I have problem understanding the spend method in the Transaction.m file</p> <p>Can the id type instance variable call ANY method in the class that contains it? I understand that respondsToSelector is a NSObject method that tells the compiler if a method has been implemented. But how can delegate which is of id-type call that method? the compiler does not even know what object it is yet...</p> <p>Please help!</p> <p>P.S. if anybody has any recommendations on good Objective-C books, I'd highly appreciate it. I wanna get into iPhone development but I figured I need to get a good grasp on the basics of Objective-C first.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload