【问题标题】:NSDate as argument in customMethodNSDate 作为 customMethod 中的参数
【发布时间】:2012-01-02 19:05:24
【问题描述】:

我已经在我的 appDelegate 中定义了我的自定义方法 (-(void)loadXML {})。 现在我想在几个视图控制器中使用它;现在我正在使用本地 NSDate 对象。

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

我想要'selectedDate'而不是'todayDate';还有我如何在我的方法中添加一个布尔值,需要在我的方法中添加一些条件吗?

【问题讨论】:

  • 你如何存储你的'selectedDate'?关于“引导”……你是什么意思? ..您想在应用程序启动时调用您的方法吗?
  • -(void)loadXML:(NSDate*)selectedDate{} ??
  • @makaron:对不起,我打错了,我的意思是 bool 不启动;现在我的应用程序调用了一个方法,该方法通过 todayDate 解析 xml,但我正在使用 (Si-Calendar)[github.com/voidparadox/Si-Calendar] 并希望通过从日历中选择的日期解析 xml
  • @el.severo 可能意味着BOOL;并且她/他应该学习如何使用 NSDateFormatter。按照上面的方式构造一个字符串看起来很容易被破坏。
  • @MichaelDautermann:你是对的;我已将我的 NSDate 对象转换为字符串; BOOL 呢?我的应用程序在午餐时崩溃了......

标签: iphone methods ios5 nsdate


【解决方案1】:

您可以这样做:

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

为了您的利益,我将向您展示如何使用 NSDateFormattergeneric C ternary conditional

希望对你有所帮助!

【讨论】:

  • 在 didFinishLunchingWithOptions here's the example 中调用我的 loadXML 方法会使我的应用崩溃
  • 在分配和初始化您的日期格式化程序后摆脱autorelease
猜你喜欢
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多