NSDate Error in iPhone SDK

Discussion in 'iOS Development' started by cocotutch, Jan 15, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Code:
    -(void)updateLabel {
    	NSDate* now = [NSDate date];
    	int hour = 14 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    	int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
    	int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
    	countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
    } 
    
    Every time that I compile this code (in my app) for the Simulator, it works flawlessly.

    But, whenever I go to compile for device, I get:

    Picture 1.png

    I am getting really confused. And angry for that matter. Two lots of my Code has ended up on here for correcting as I cannot find a way to do it, even though I have edited the code many times to try and get it to compile.

    Any help?

    cocotutch
  2. Spreche

    Spreche Member

    Joined:
    Dec 27, 2007
    Messages:
    164
    Likes Received:
    0
    Device:
    iPhone
    The function 'dateWithCalendarFormat:timeZone:' is, in fact, an NSDate function, but it is not part of the NSDate that is implemented in the iPhone. Try using NSDateFormatter for whatever your goal is for this. You might want to try NSDateComponents as a second option. NSDate, NSDateComponents, and NSDateFormatter are all different classes in the iPhone.
  3. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    It's still bitterly refusing to work. I changed my code to:

    Code:
    -(void)updateLabel {
    	NSDateComponents* now = [NSDate date];
    	int hour = 14 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    	int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
    	int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
    	countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
    } 
    And then:

    Code:
    -(void)updateLabel {
    	NSDateFormatter* now = [NSDate date];
    	int hour = 14 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    	int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
    	int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
    	countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
    } 
    Still at no avail.

    Do you know anything else?

    cocotutch
  4. Spreche

    Spreche Member

    Joined:
    Dec 27, 2007
    Messages:
    164
    Likes Received:
    0
    Device:
    iPhone
    The function 'dateWithCalendarFormat:timeZone:' is not implemented in the iPhone, so you are going to have to take that out. I don't exactly know what you are trying to get from those few lines, but one reason for the invalid operand might be because your 'hourOfDay', 'minuteOfHour', and 'secondOfMinute' functions(assuming you made them yourself) don't return an integer, which is needed to perform algebraic operands. Such as addition and subtraction, which is what you are doing. So you may want to change the functions if this is the case:
    Code:
    - (NSUInteger)hourOfDay {
    
    //Code Here.
    
    }
    
    I just seen that one of the errors is 'no -hourOfDay method found' and so on. You need these function in order to get rid of the 'red' errors. Make sure you create each function with NSUInteger in parenthesis.
  5. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I really don't get it. (Sorry).

    I am creating a countdown until 2:59 PM. This works fine on the Simulator. Which part of my code do I have to put in the "-(NSUInteger)hourOfDay {" and "secondOfDay"? methods? And more importantly, where do I put them?

    Should I just scrap this piece of code?

    cocotutch
  6. Spreche

    Spreche Member

    Joined:
    Dec 27, 2007
    Messages:
    164
    Likes Received:
    0
    Device:
    iPhone
    I'll write a little sample code for you.

    Code.h
    Code:
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    
    @interface ClassName : SuperClass {
    
    //instance variables
    
    }
    
    - (NSUInteger)hourOfDay;
    - (NSUInteger)minuteOfHour;
    - (NSUInteger)secondOfMinute;
    
    @end
    
    These 3 lines are needed in the '.h' file:
    - (NSUInteger)hourOfDay;
    - (NSUInteger)minuteOfHour;
    - (NSUInteger)secondOfMinute;


    Code.m
    Code:
    #import "Code.h"
    
    @implementation ClassName
    
    - (NSUInteger)hourOfDay {
    
    //Code Here
    
    }
    - (NSUInteger)minuteOfHour {
    
    //Code Here
    
    }
    - (NSUInteger)secondOfMinute {
    
    //Code Here
    
    }
    
    
    /*
    Rest
    of
    your
    code
    here
    */
    
    @end
    
    In the '.m' file, it really doesn't matter where you place the methods. As long as they are somewhere before you call them. In your case, they must be before the 'updateLabel' method.
  7. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I have done this:

    Picture 2.png

    I haven't compiled it yet, thats why the errors are still visible.

    Do I have to put:

    Code:
    int hour = 14 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    Inside:

    Code:
    - (NSUInteger)hourOfDay {
    	
    	//Code Here
    	
    }
    And the others in the respective methods?

    And then, keep the:

    Code:
    -(void)updateLabel {
    	NSDate* now = [NSDate date];
    	int hour = 14 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    	int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
    	int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
    	countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
    }
    
    Separate?


    cocotutch
  8. Spreche

    Spreche Member

    Joined:
    Dec 27, 2007
    Messages:
    164
    Likes Received:
    0
    Device:
    iPhone
    It all depends. If you are going to do this all dynamically, meaning you will see the seconds counting down and all, then you will need an NSTimer.
    Code:
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    
    You will then need to create a 'countDown' method because a '@selector' is basically telling the timer what to do. So 'countDown' will be telling what the 'timer' to do. You would have to do something like this.

    Code:
    - (void)countDown {
    
    //counts down 1 second
    NSUInteger second = second - 1.0;
    
    [self updateLabel];
    }
    
    - (void)updateLabel {
    
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
    
    }
    
    What this is saying is that you are setting up a timer that has an interval of 1 second. Every 1 second the selector 'countDown' is called and subtracts the current value of the second by 1. Hence, counting down by one. Then the updateLabel method is called, which updates the label's content to the new time. You can do that same thing with hours and minutes.

    Code:
    NSDate* now = [NSDate date];
    int hour = 14 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    
    The problem with this is that the [now dateWithCalendarFormat:nil timeZone:nil] is invalid. The method 'dateWithCalendarFormat:timeZone:' is not compatible with NSDate on the iPhone. It is compatible on a regular Mac application though. Your 'hourOfDay' function can ONLY be called by 'self' like this [self hourOfDay]. That would return and integer and would be subtracted from 14 just like you have it. Unfortunately,
    Code:
    [[[NSDate date] dateWithCalendarFormat:nil timeZone:nil] hourOfDay]
    is equal to
    [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    which is not equal to
    [self hourOfDay];
    and cannot be subtracted from
    14
    

    I'm getting tired so I'll probably help you write your code tomorrow. IM me rdevils09 on aim if you have any questions you need answered.
  9. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I have decided to simply drop this area of code from my application.

    I have now changed the function to viewing an older/newer timetable from Semester 1 or 2 of a given year.

    The function is working entirely.

    Thanks anyway,
  10. chobecom

    chobecom New Member

    Joined:
    Feb 13, 2009
    Messages:
    1
    Likes Received:
    0
    Hi,check this codes. It worked.

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit| NSMinuteCalendarUnit |NSSecondCalendarUnit;
    NSDate *date = [NSDate date];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];
    int h = [comps hour];
    int m = [comps minute];
    int s = [comps second];
    countdownLabel.text = [NSString stringWithFormat

    Please Register or Log in to view images

    "%02d:%02d:%02d",h, m, s];

Share This Page