Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ag2web

macrumors newbie
Original poster
Mar 9, 2011
12
0
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.
 
Last edited by a moderator:

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Code:
//Another.m
#import "AppDelegate.h"
@implementation Another
- (void) someMethod
{
    AppDelegate *appD = [[AppDelegate alloc] init];
    NSTextView *drawerText2 = [[NSTextView alloc] init];
    drawerText2.string = @"ABC";
    appD.drawerText = drawerText2;
    NSLog(@"out %@", appD.drawerText.string);
}
@end
So what happens to appD when the method "someMethod" stops running?

Try "Analyze" in the "Product" menu. That will give you some hint.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

ag2web

macrumors newbie
Original poster
Mar 9, 2011
12
0
Code:
In your Another class you will need a way to access the AppDelegate object that is in use, rather than creating a new one.
-Lee[/QUOTE]

Can you post sample code for this

Thanks[COLOR="#808080"]

----------

[/COLOR][quote="ramy1989, post: 16311777"]I'm missing something: what's the problem that you get trying to run this code?[/QUOTE]

- When I run someMethod nothing is displayed in drawerText

Thanks[COLOR="#808080"]

----------

[/COLOR][quote="gnasher729, post: 16311854"][quote="ag2web, post: 16303834"][CODE]//Another.m
#import "AppDelegate.h"
@implementation Another
- (void) someMethod
{
    AppDelegate *appD = [[AppDelegate alloc] init];
    NSTextView *drawerText2 = [[NSTextView alloc] init];
    drawerText2.string = @"ABC";
    appD.drawerText = drawerText2;
    NSLog(@"out %@", appD.drawerText.string);
}
@end
So what happens to appD when the method "someMethod" stops running?

- When I run someMethod nothing is displayed in drawerText

Thanks
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

ag2web

macrumors newbie
Original poster
Mar 9, 2011
12
0
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

Thank you, understood
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
I do not have any programming background, Objective C is my first language.

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.
 

ag2web

macrumors newbie
Original poster
Mar 9, 2011
12
0
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.

Thanks Lars, makes sense
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.