NSDate as argument in customMethod

不想你离开。 提交于 2019-12-25 01:59:45

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/8209160/nsdate-as-argument-in-custommethod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!