Re: @property and @synthesize not working
Re: @property and @synthesize not working
- Subject: Re: @property and @synthesize not working
- From: Andrew Merenbach <email@hidden>
- Date: Wed, 13 Aug 2008 20:40:27 -0700
Hi, Nathan,
Ah! I have a suggestion, but there's one thing that I forgot: as a
*general* rule of thumb for *most* init methods, do not do anything
between "self = [super init];" and "return self;" that isn't in a
conditional to test the validity of "self" -- that is to say:
- (id)init {
self = [super init];
if (self) {
// do stuff here
}
return self;
}
This helps with avoiding various problems, such was wasted code
execution and potential crashes, if super's -init method returns nil.
Now, on to your problem:
If you have a connection set in Interface Builder, the outlet won't
generally be available until -awakeFromNib gets called in the
controller -- rather than -init. Thus you may wish to do the
following (warning, typed in Mail):
- (id)init {
self = [super init];
if (self) {
[self setDayOneDate:[NSCalendarDate calendarDate]]; // you may wish
to use a property for this accessor, too, if you don't already
}
return self;
}
- (void)awakeFromNib {
dayOneTasks.searchDate = [self dayOneDate];
}
-- that should do the trick, assuming that everything's wired up
properly in Interface Builder.
One other thing, though: it is my understanding that NSCalendarDate
will likely be deprecated in a future release of Mac OS X. This, from
the docs:
Important: Use of NSCalendarDate strongly discouraged. It is not
deprecated yet, however it may be in the next major OS release after
Mac OS X v10.5. For calendrical calculations, you should use
suitable combinations of NSCalendar, NSDate, and NSDateComponents,
as described in Calendars in Dates and Times Programming Topics for
Cocoa.
Instead, one may wish to use a combination of NSDate and NSCalendar.
The archives have had periodic posts on the topic.
Cheers,
Andrew
On Aug 13, 2008, at 7:27 PM, Nathan Gilmore wrote:
Hi Andrew,
Thanks so much for your quick response and all of the great tips!
You were right. dayOneTasks was nil.
I am a little confused about how this works with Interface Builder.
In MainMenu.nib, I have a DayOneTasks Controller. It's class is set
to DayTaskController.
I also have the outlet for AppController.dayOneTasks set to the
DayOneTasks Controller Object in the nib file. So, I guess by doing
that, AppController.dayOneTasks still does not get initialized
unless I call the alloc and init methods?
Thank you,
Nathan
On Aug 13, 2008, at 9:55 PM, Andrew Merenbach wrote:
On Aug 13, 2008, at 6:47 PM, Nathan Gilmore wrote:
Hello everyone,
I am a newbie and I am having trouble getting my setter to work
when I use @synthesize. Here is the code:
**Header File**
@interface DayTaskController : NSArrayController {
NSCalendarDate *searchDate;
}
- (void)search:(id)sender;
@property(readwrite, assign) NSCalendarDate *searchDate;
@end
**Implementation File**
@implementation DayTaskController
@synthesize searchDate;
@synthesize appController;
.
.
.
**AppController**
I try and just set the searchdate field and then output it:
- (id) init
{
[super init];
[self setDayOneDate:[NSCalendarDate calendarDate]];
NSLog(@"self dayOneDate = %@",dayOneDate);
[dayOneTasks setSearchDate:dayOneDate];
NSLog(@"dayOneTasks search date = %@", [dayOneTasks searchDate]);
return self;
}
The above code gives this output:
2008-08-13 21:30:23.081 LifeTask2[20085:10b] self dayOneDate =
2008-08-13 21:30:23 -0400
2008-08-13 21:30:23.082 LifeTask2[20085:10b] dayOneTasks search
date = (null)
Any suggestions as to what I am doing wrong?
Thank you!
Nathan
Hi, Nathan!
Have you checked to ensure that dayOneTasks itself is not nil?
Also, are you using Garbage Collection? If *not*, then try
changing from "assign" to "retain" in your property declaration.
Also, a couple of suggestions: be sure to write your first line as
"self = [super init];" (instead of "[super init];" by itself).
Additionally, you may wish to consider using the standard property
syntax, such as:
dayOneTasks.searchDate = dayOneDate;
NSLog(@"dayOneTasks search date = %@", dayOneTasks.searchDate);
-- instead of using the bracketed accessors. That's one reason, in
my opinion, that properties are a good idea -- they can simplify
syntax and/or improve readability for certain cases.
Cheers,
Andrew
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden