Amazon.com Widgets Question #15.1 works well in main, but doesn't in category implementation
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 07:00:51 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 15 - Numbers, Strings, and Collections
| | | |-+  Question #15.1 works well in main, but doesn't in category implementation
Pages: [1]   Go Down
Print
Author Topic: Question #15.1 works well in main, but doesn't in category implementation  (Read 422 times)
clp274
Newbie
*
Posts: 8


Email




« on: December 07, 2011, 02:53:44 PM »

If I am in main and say I create 2 NSDate objects:
NSDate *currentDate = [NSDate date];
NSDate *anotherDate = [NSDate dateWithNaturalLanguageString:@"January 15, 2012"];

I then proceed to retrieve the day difference result and store it in a unsigned long variable:
NSDateComponents *comps = [cal components:NSDayCalendarUnit fromDate:currentDate toDate:anotherDate options:0];
long days = [coppa day];

I then run NSLog:
NSLog(@"%li", days);

The aforementioned code works exactly as it should.

However, if I put that code in -(unsigned long) elapsedDays: (NSDate *) theDate :

#import "ElapsedDays.h"

@implementation NSDate (ElapsedDays)
-(unsigned long) elapsedDays: (NSDate *) theDate
{
    // Initialize NSCalendar object
    NSCalendar *cal = [[NSCalendar alloc] init];
    // Create variable to store NSDateComponent result returned from components:... method.
    NSDateComponents *comps = [cal components:NSDayCalendarUnit fromDate:self toDate:theDate options:0];
    // Retrieve the long variable day from the datecomponent, which should be the difference between the two dates
    unsigned long elapsedDays = [comps day];
   
    return elapsedDays;
}

@end

When I try to use the that method and run NSLog I get 0. I'm not sure why that is. The only thing I did different I believe was use self for the startdate in the components method. Also, as a follow up question, what should I release to ensure their aren't any memry leaks. Since I allocated NSCalendar, I presume I would have to release that at the end. However, if I try to use dealloc, it doesn't recognize cal NSCalendar object.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: December 07, 2011, 08:32:55 PM »

Try initializing your calendar using the initWithCalendarIdentifier: method.  You're not supposed to do just an init on it.

The calendar object cal should be released in your elpasedDays: method after you're done using it.

Cheers,

Steve
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.