|
|
#1 |
|
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
- 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 OllyW; Nov 16, 2012 at 03:19 AM. Reason: Please use code tags |
|
|
|
0
|
|
|
#2 |
|
I'm missing something: what's the problem that you get trying to run this code?
|
|
|
|
0
|
|
|
#3 |
|
[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
So what happens to appD when the method "someMethod" stops running? Try "Analyze" in the "Product" menu. That will give you some hint. |
|
|
|
0
|
|
|
#4 |
|
Code:
AppDelegate *appD = [AppDelegate new]; 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 |
|
|
|
0
|
|
|
#5 | ||
|
Quote:
Thanks ---------- Quote:
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
So what happens to appD when the method "someMethod" stops running? - When I run someMethod nothing is displayed in drawerText Thanks |
|||
|
|
0
|
|
|
#6 |
|
Code:
AppDelegate *appD = [[NSApplication sharedApplication] delegate]; -Lee |
|
|
|
0
|
|
|
#7 | |
|
Quote:
|
||
|
|
0
|
|
|
#8 | |
|
Quote:
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 |
||
|
|
2
|
|
|
#9 | |
|
Quote:
|
||
|
|
0
|
|
|
#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!
__________________
I know more than yesterday. Lars |
|
|
|
0
|
![]() |
|
| Tags |
| accessors, getters, objective-c, setters |
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 05:01 AM.








Linear Mode
