Objective C Accessors

Discussion in 'Mac Programming' started by ag2web, Nov 15, 2012.

  1. ag2web, Nov 15, 2012
    Last edited by a moderator: Nov 16, 2012

    macrumors newbie

    ag2web

    Joined:
    Mar 9, 2011
    #1
    I am struggling to understand some basic concepts in Objective C, would appreciate if someone can help. This is about accessors - setters and getters. I have Cocoa application with the following classes:

    Code:
    //AppDelegate.h
    #import <Cocoa/Cocoa.h>
    @interface AppDelegate : NSObject <NSApplicationDelegate> 
    @property (nonatomic, retain) IBOutlet NSTextView *drawerText;
    @end
    
    //AppDelegate.h
    @implementation AppDelegate
    //some methods here
    @end
    
    //Another.h
    #import <Cocoa/Cocoa.h>
    @interface Another : NSViewController
    //some properties here
    @end
    
    
    //Another.m
    #import "AppDelegate.h"
    @implementation Another
    - (void) someMethod {
    AppDelegate *appD = [AppDelegate new];
    NSTextView *drawerText2 = [[NSTextView alloc] init];
    drawerText2.string = @"ABC";
    appD.drawerText = drawerText2;
    NSLog(@"out %@", appD.drawerText.string);
    }
    @end
    
    I guess the issue I have should be understandable from what I am trying to do in Another.m someMethod. In summary:

    - Theres NSTextView *drawerText defined in AppDelegate
    - drawerText is an IBOutlet which is properly linked in IB
    - I need to display some content in drawerText and the content should come from Another.m class
    - When I run someMethod nothing is displayed in drawerText
    - when I NSLog drawerText from Another.m it has ABC value
    - when I NSLog drawerText from AppDelegate.m it is empty, though not (null)
    - when I run the same someMethod from within AppDelegate.m, everything work, i.e. ABC is displayed in drawerText

    As I said, I believe I am missing some basic understanding here, as the issue is not only with this particular drawerText but with any control or instance variable defined in one class and set / get from another class.

    I read a lot of Objective C docs by Apple, various books (Stephen G. Kochan, SCOTT KNASTER, MARK DALRYMPLE) but I just can't grasp how to use setters and getters across different classes.

    I do not have any programming background, Objective C is my first language. Usually, if there is enough time, I can figure out things, but not this time. By reading these books, I even managed to developed couple of simple Cocoa Apps, but had to keep everything in one class, which obviously is not correct, especially when an app becomes more or less complex.

    Any help will be appreciated.
     
  2. macrumors newbie

    Joined:
    Nov 7, 2012
    #2
    I'm missing something: what's the problem that you get trying to run this code?
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Code:
    AppDelegate *appD = [AppDelegate new];
    
    This will get you every time. I'm not sure you're making the object vs. class differentiation. There is an AppDelegate object created that has sone state. It is an instance of the AppDelegate class. You create another instance by passing "new", manipulate it, and hope that the other AppDelegate object reflects these changes. "new" is outmodded and you can forget about it. In your Another class you will need a way to access the AppDelegate object that is in use, rather than creating a new one. Look at sharedApplication on UI/NSApplication, then pass delegate to the resulting object.

    See if there are programming courses at a community college nearby or watch some iTunes U courses. I'd go for the prior, because it would benefit you to be able to bounce things off of classmates and instructors.

    -Lee
     
  5. thread starter macrumors newbie

    ag2web

    Joined:
    Mar 9, 2011
    #5
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Code:
    AppDelegate *appD = [[NSApplication sharedApplication] delegate];
    
    This will get you *the* delegate for *the* NSApplication, instead of creating a new one. Gnasher was implying that you would leak the object you created, but you shouldn't create one in the first place.

    -Lee
     
  7. thread starter macrumors newbie

    ag2web

    Joined:
    Mar 9, 2011
    #7
    Thank you, understood
     
  8. macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    This was me almost 3 years ago. I struggled and found what I could in books and online to make things work without grasping the basics. The people helping you now are the same folks that helped me understand the basics years ago.

    I finally stopped struggling and went back to the basics, learned C first. When I understood the basics of programming and made some small programs stepping in to Objective C was much easier and I understood it the second time around.

    If you want to be a successful at programming start in the right area. Even if you get the answer to your question and it works, you won't understand why it works which will just lead to bigger problems down the road for you. At that point you will just give up trying to program thinking it is to hard. When really you just started in the wrong place.

    My 2 cents.
     
  9. thread starter macrumors newbie

    ag2web

    Joined:
    Mar 9, 2011
    #9
    Thanks Lars, makes sense
     
  10. macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    and might I recommend the book "Learn C on the Mac". Other people have their favorite books too they recommend. Forget about pretty interfaces, buttons, textfields and so on. Read it till it sinks in and create your own projects from every chapter till it sinks in.

    I spent about 3 to 4 months with that book and moved on when I could write my own command line blackjack program from scratch. It was ugly looking code wise but it worked!
     

Share This Page