Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
May 23, 2018, 09:16:42 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: adding a variables and instance variables to a category  (Read 4124 times)
samuel_j
Newbie
*
Posts: 13


« on: September 26, 2012, 12:59:49 AM »

Hi all,
while working on exercise 11.4 and creating the category 'Trig' I was trying to define a new variable, to be used in the methods (called result).
I put the variable at the beginning of the implementation file and tried to {} it as you would do to any instance variable and the complier returned a 'expected identifier or '(' ' error. (see below)

The only way I could make the program work was remove the {} around the variable.

I later understood that I didn't of course need the variable 'result' to be an instance variable (in fact I could do without it at all) but still, the issue of instance variables in a category is not discussed much in the chapter and I find it a bit confusing. How can you define an instance variable in a new category? can a new category access the instance variables of the other category or just its methods?

thanks

Code: (Objective-C)
 
#import "Calculator+Trig.h"

@implementation Calculator (Trig)
{
    double result;
}

- (double) sin
{
    result = sin (self.accumulator);
    return result;
}
- (double) cos
{
    result = cos (self.accumulator);
    return result;
}
- (double) tan
{
    result = tan (self.accumulator);
    return result;
}

@end

Logged
Bertus
Newbie
*
Posts: 20


'Audaces fortuna iuvat'


WWW Email
« Reply #1 on: April 30, 2014, 06:55:04 AM »

Categories don't contain instance variables:

Quote
'Also, you don’t tell it about the instance variables, as you’ve done in all the previous interface sections you’ve defined. In fact, if you try to list the parent class or the instance variables, you’ll get a syntax error from the compiler.'

Uittreksel van: Stephen G. Kochan. 'Programming in Objective-C, Third Edition (LIVE ONLINE TRAINING EDITION)'. iBooks.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.