Objective C Accessors - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 15, 2012, 02:48 PM   #1
ag2web
macrumors newbie
 
Join Date: Mar 2011
Objective C Accessors

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.
__________________
ag2web
http://www.youtube.com/user/ag2web

Last edited by OllyW; Nov 16, 2012 at 03:19 AM. Reason: Please use code tags
ag2web is offline   0 Reply With Quote
Old Nov 16, 2012, 06:28 PM   #2
ramy1989
macrumors newbie
 
Join Date: Nov 2012
I'm missing something: what's the problem that you get trying to run this code?
ramy1989 is offline   0 Reply With Quote
Old Nov 16, 2012, 06:47 PM   #3
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
[QUOTE=ag2web;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
[QUOTE]

So what happens to appD when the method "someMethod" stops running?

Try "Analyze" in the "Product" menu. That will give you some hint.
gnasher729 is offline   0 Reply With Quote
Old Nov 16, 2012, 06:55 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Nov 17, 2012, 12:15 PM   #5
ag2web
Thread Starter
macrumors newbie
 
Join Date: Mar 2011
Quote:
Originally Posted by lee1210 View Post
[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
Can you post sample code for this

Thanks

----------

Quote:
Originally Posted by ramy1989 View Post
I'm missing something: what's the problem that you get trying to run this code?
- When I run someMethod nothing is displayed in drawerText

Thanks

----------

[QUOTE=gnasher729;16311854][QUOTE=ag2web;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
[QUOTE]

So what happens to appD when the method "someMethod" stops running?

- When I run someMethod nothing is displayed in drawerText

Thanks
__________________
ag2web
http://www.youtube.com/user/ag2web
ag2web is offline   0 Reply With Quote
Old Nov 17, 2012, 01:05 PM   #6
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Nov 17, 2012, 01:30 PM   #7
ag2web
Thread Starter
macrumors newbie
 
Join Date: Mar 2011
Quote:
Originally Posted by lee1210 View Post
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
__________________
ag2web
http://www.youtube.com/user/ag2web
ag2web is offline   0 Reply With Quote
Old Nov 17, 2012, 03:20 PM   #8
larswik
macrumors 65816
 
Join Date: Sep 2006
Quote:
Originally Posted by ag2web View Post

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.
__________________
I know more than yesterday.
Lars
larswik is offline   2 Reply With Quote
Old Nov 18, 2012, 01:00 PM   #9
ag2web
Thread Starter
macrumors newbie
 
Join Date: Mar 2011
Quote:
Originally Posted by larswik View Post
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
__________________
ag2web
http://www.youtube.com/user/ag2web
ag2web is offline   0 Reply With Quote
Old Nov 18, 2012, 02:53 PM   #10
larswik
macrumors 65816
 
Join Date: Sep 2006
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!
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Tags
accessors, getters, objective-c, setters

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Objective C Accessors ag2web Mac Applications and Mac App Store 0 Nov 14, 2012 02:30 PM
Objective C question farmerdoug Mac Programming 4 Sep 16, 2012 02:44 PM
Problems with Objective-C++? frank100 Mac Programming 5 Aug 8, 2012 04:49 PM
Testing objective-c Evre iPhone/iPad Programming 21 Jul 12, 2012 07:22 AM
Objective C Syntax nashyo iPhone/iPad Programming 6 Jul 4, 2012 11:56 AM

Forum Jump

All times are GMT -5. The time now is 06:33 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC