返回stringfromdate方法

时间:2012-01-02 06:27:19

标签: iphone ios cocoa-touch ios5

所以这里有一些我遇到问题的代码:

//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];

给出错误:“无法静态分配接口类型”

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

欢呼声, 马特

2 个答案:

答案 0 :(得分:5)

而不是行

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

在您提供的第一个代码块中,尝试以下行

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

但最好还是接受第二个陈述,

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

答案 1 :(得分:0)

你正在以错误的方式做事

你应该以这种方式做事情

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];
相关问题