el.severo
el.severo

Reputation: 2277

NSDate as argument in customMethod

I've defined my custom method ( -(void)loadXML {} ) into my appDelegate. Now I'd like to use it in severals viewControllers; Right now I'm using local NSDate objects.

    NSDate *todayDate = [NSDate date];
    NSString *XMLUrl = @"http://localhost/MyApp/GetXML?&aDate=";
    NSString *urlString = [NSString stringWithFormat:@"%@%@", XMLUrl, todayDate];
    tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:urlString]];

instead of 'todayDate' I'd like to have 'selectedDate'; also how I add a bool to my method, need to have some conditions into my method?

Upvotes: 0

Views: 79

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

Here's what you can do:

NSDate *selectedDate = ???????; // set this to whatever you want selectedDate to be
BOOL myBoolean = YES;
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat: @"yyyy-MM-dd"]; // or whatever format you wish
NSString *urlString = 
    [NSString stringWithFormat:@"http://localhost/MyApp/GetXML?BOOL=%@&aDate=%@", 
    (myBoolean ? @"YES" : @"NO"), 
    [dateFormatter stringFromDate: selectedDate]];

tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:urlString]];
[dateFormatter release]; // don't forget to release, if not using ARC

And for your benefit, I'm showing you how to use a NSDateFormatter and a generic C ternary conditional.

I hope this helps you out!

Upvotes: 1

Related Questions