Re: @property and @synthesize not working
On Aug 13, 2008, at 6:47 PM, Nathan Gilmore wrote: I am a newbie and I am having trouble getting my setter to work when I use @synthesize. Here is the code: @interface DayTaskController : NSArrayController { NSCalendarDate *searchDate; } @property(readwrite, assign) NSCalendarDate *searchDate; This should really be "copy" rather than "assign" as NSDate and NSCalendarDate conform to NSCopying. At the very least, it should be "retain" unless you're writing GC-only code. -- Chris ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: @property and @synthesize not working
On Wed, Aug 13, 2008 at 10:27 PM, Nathan Gilmore <[EMAIL PROTECTED]> 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? The problem is just that your code is running too early. IBOutlets are connected after your class is alloc/inited. If you think about it, this is how it must be: they cannot be connected *before* that, because nothing exists to connect them *to*. Happily, Apple (or rather, NeXT) anticipated this problem and provided a solution: you can put "initialization" code that depends on IBOutlets being properly connected in an -awakeFromNib method and that code will be called after outlets are connected but before control is returned to the nib loader. Mike ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: @property and @synthesize not working
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 smime.p7s Description: S/MIME cryptographic signature ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: @property and @synthesize not working
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 ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: @property and @synthesize not working
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 smime.p7s Description: S/MIME cryptographic signature ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
@property and @synthesize not working
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 ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]