【问题标题】:returning a stringfromdate method返回一个 stringfromdate 方法
【发布时间】:2012-01-02 06:27:18
【问题描述】:

下面是一些我遇到问题的代码:

//format the date to a string for echoing it
    NSDateFormatter* formattedDate = [[NSDateFormatter alloc] init];
    [formattedDate setDateStyle:NSDateFormatterLongStyle]; //now myFormatted is set to a long style
    NSString* dateForOutput = [formattedDate stringFromDate:self.datePickerOutlet.date];
    //also now need to set the "you began on" text to the newly chosen date
    [self.startDate setText:@"You started on: %@", dateForOutput];

给出的错误是:“方法调用的参数太多,预期为 1,有 2”

我不明白为什么它说我正在尝试传递两种方法。 如果我很愚蠢,我尝试执行以下操作,但它仍然给了我一个错误:

//format the date to a string for echoing it
NSDateFormatter* formattedDate = [[NSDateFormatter alloc] init];
[formattedDate setDateStyle:NSDateFormatterLongStyle]; //now myFormatted is set to a long style
NSString* dateForOutput = [formattedDate stringFromDate:self.datePickerOutlet.date];
//also now need to set the "you began on" text to the newly chosen date
NSString *foobar = @"You started on: %@", dateForOutput;
[self.startDate setText:foobar];

给出的错误:“接口类型不能静态分配”

坦率地说,我不知道为什么它给了我这个错误...一些帮助将不胜感激。 这可能只是我出于某种原因看不到的小东西=/

干杯, 马特

【问题讨论】:

  • 您是否为日期选择器和 UIlabel 创建了属性..?

标签: iphone ios cocoa-touch ios5


【解决方案1】:

代替线

[self.startDate setText:@"You started on: %@", dateForOutput];

在您给出的第一段代码中,尝试以下行

[self.startDate setText:[NSString stringWithFormat:@"You started on: %@", dateForOutput]];

但最好还是用第二个语句,

NSString *foobar = [NSString stringWithFormat:@"You started on: %@", dateForOutput];

【讨论】:

  • 就是这样!非常感谢!
【解决方案2】:

你做错事了

你应该这样做

NSDate* myDate=[NSDate new];

NSDateFormatter* formattedDate = [[NSDateFormatter alloc] init];

[formattedDate setDateStyle:NSDateFormatterLongStyle];

//现在 myFormatted 设置为长样式

//这里我刚刚过了当前日期

NSString* dateForOutput = [formattedDate stringFromDate:myDate];

//also now need to set the "you began on" text to the newly chosen date

NSString *foobar = @"You started on:";

//现在可以追加字符串了

//这是附加字符串的方式..

foobar= [foobar  stringByAppendingFormat:@"%@",dateForOutput];

[self.startDate setText:foobar];

【讨论】:

    猜你喜欢
    • 2019-03-27
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多