Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Obj-C Question (iPhone Specific)

Options
  • 29-08-2010 7:58pm
    #1
    Registered Users Posts: 8,004 ✭✭✭


    Hi Folks,

    I'm sure this can be done, I just have no idea how to do it!

    I'm looking to take todays date and place it into a string. From this string I want to make it an integer. For instance, the 1st of January 2010 would be:

    int 01012010

    For a start, is this possible?

    Next, I want to take out a specific number of a group of numbers, so for instance if I want the 20 in 2010, how would I extract it and save as an integer? Likewise, could I take the 1 out our 01? I remember vaguely how to do this but I have a feeling it was to do with arrays.

    Many thanks!


Comments

  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    Here is some simple code to get todays date
    	//Get todays date as DD-MM-YYYY
    	NSDateFormatter *date_formater=[[NSDateFormatter alloc]init];
    	[date_formater setDateFormat:@"YYYY"];//Get Year
    	int year=[[date_formater stringFromDate:[NSDate date]] intValue];
    	[date_formater setDateFormat:@"MM"];//Get Month
    	int month=[[date_formater stringFromDate:[NSDate date]] intValue];
    	[date_formater setDateFormat:@"dd"];//Get Day
    	int day=[[date_formater stringFromDate:[NSDate date]] intValue];
    
    	//If you call alloc on something YOU MUST call release.
    	[date_formater release];
    
    Form that you could build the date as you want it by doing something like this:
    int serialisedDate = day; // 1
    serialisedDate *= 100;   // 100
    serialisedDate += month; // 101
    serialisedDate *= 10000; // 1010000
    serialisedDate += year; // 1012010
    
    I'm afraid there is no way to keep the 01 from losing its 0.
    unless you did it in reverse order and put it as YYYYMMDD
    then you would get something like; 20100101.

    In fact this is a better way as no matter what the date the int serialisedDate will always come out with 2 digits for the day.

    Next you want to deseralise it? That's slightly more complex.
    //Start with the int 20100101
    int seralisedDate = 20100101;
    //Get the year
    int year = seralisedDate/10000;
    //Get Month
    int month = (seralisedDate/100)-year*100;
    //Get Day
    int day = seralisedDate - (year*10000+month*100);
    

    That should be it!

    If you really must have the 0 in the day/month, when outputting to an NSString have a check like this;
    NSString *dayString;
    if(day<10)
    {
    dayString = [NSString stringWithFormat:@"0%i",day];
    }
    else
    {
    dayString = [NSString stringWithFormat:@"%i",day];
    }
    


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Thanks Genghiz Cohen. I'll try step through this.
    //Get todays date as DD-MM-YYYY
    	NSDateFormatter *date_formater=[[NSDateFormatter alloc]init];
    	[date_formater setDateFormat:@"YYYY"];//Get Year
    
    	int year=[[date_formater stringFromDate:[NSDate date]] intValue];
    	[date_formater setDateFormat:@"MM"];//Get Month
    
    	int month=[[date_formater stringFromDate:[NSDate date]] intValue];
    	[date_formater setDateFormat:@"dd"];//Get Day
    	int day=[[date_formater stringFromDate:[NSDate date]] intValue];
    
    	//If you call alloc on something YOU MUST call release.
    	[date_formater release];
    

    This is creating a few integers. Are these the numbers representative of the days? So if it was the 10th, int day would be 10? If it was February, would int month be 2?

    As regards the above, could I create a int for the hour and minute as well?

    It actually doesn't matter if the 01 became a 1. I merely require it for mathematical purposes, so the zero wouldn't be an issue.

    Thank you for the rest of the code, I'll keep going through it. I just got a string to an int, so I'm running on a high :)


  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    ironclaw wrote: »
    	//Get todays date as DD-MM-YYYY
    	NSDateFormatter *date_formater=[[NSDateFormatter alloc]init]; //create dateFormatter instance
    
    	[date_formater setDateFormat:@"YYYY"];//Get Year
    	int year=[[date_formater stringFromDate:[NSDate date]] intValue];
    
    	[date_formater setDateFormat:@"MM"];//Get Month
    	int month=[[date_formater stringFromDate:[NSDate date]] intValue];
    
    	[date_formater setDateFormat:@"dd"];//Get Day
    	int day=[[date_formater stringFromDate:[NSDate date]] intValue];
    
    	//If you call alloc on something YOU MUST call release.
    	[date_formater release];
    
    This is creating a few integers. Are these the numbers representative of the days? So if it was the 10th, int day would be 10? If it was February, would int month be 2?
    I've slightly reformatted the code above so it reads better.
    Yes, that is correct.
    As regards the above, could I create a int for the hour and minute as well?
    Yes, you simply tell the NSDateFormatter to give you back the time like so:
    [date_formater setDateFormat:@"hh"];//hours
    [date_formater setDateFormat:@"mm"];//minutes
    [date_formater setDateFormat:@"ss"];//seconds
    
    It actually doesn't matter if the 01 became a 1. I merely require it for mathematical purposes, so the zero wouldn't be an issue.
    But for formatting purposes it would be best to keep the format YYYYMMDD

    Actually, since posting this (which was pretty much a copy and paste from my current project) a better way has occurred to me.

    Instead of pulling out the day, month and year separately you could do them all together into an NSString then get the intValue of that string and ignore all that seralisedDate code in my first post.
     
    	//Get todays date as DD-MM-YYYY
    	NSDateFormatter *date_formater=[[NSDateFormatter alloc]init]; //create dateFormatter instance
    
    	[date_formater setDateFormat:@"YYYYMMDD"];//Get Year
    	int yearMonthDay=[[date_formater stringFromDate:[NSDate date]] intValue];//returns 20100101 for Jan-1st-2010
    
    	//If you call alloc on something YOU MUST call release.
    	[date_formater release];
    

    Hopefully that helps.
    Ask any questions you like!:D


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Thanks again! Thats sped me up no end.

    On a different note, how do I make them global variables?


  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    Rumour has it that anything declared in the .h file outside the @interface and @end keywords will be global.
    //Declare here for global
    
    int globalInt;
    float globalFloat;
    
    @interface myClass:NSObject {
    
    }
    -(void)myMethod:(int)Param1;
    @end
    
    //Or here for global
    NSString *globalNSString;
    NSArray *globalArray;
    
    

    To get access to the globals, you must #import myClass.h into the file you want to access them from.

    I must advise against globals, unless it is a static const. In which case you could use a #define which is faster.

    You could, of course have a .h file specifically for globals/#defines


  • Advertisement
Advertisement