gpt4 book ai didi

ios - cocoa touch 如何将标签设置为 NSDate

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:10 24 4
gpt4 key购买 nike

我有一个名为 datelabel 的标签,我想在我的 AppDelgate 中将它设置为一个名为 dateofbirth 的 NSDate。

我已经定义并导入了我的 AppDelgate 并将其设置为 appDelgate,但是当我尝试 self.datelabel.text = appDelgate.dateofbirth

但是当我把它放进去时,它给出了这个错误 Incompatible pointer types assigning to 'NSString *' from 'NSDate *'

我尝试了一些方法,但都没有用。

有什么方法可以将标签设置为 NSDate。

最佳答案

您需要将 NSDate 转换为 NSString。您可以为此使用 NSDateFormatter

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// you can use one of the builtin localizated formats
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
self.datelabel.text = [formatter stringFromDate:appDelgate.dateofbirth];
// or you can set your own format
[formatter setDateFormat:@"yyyy-MM-dd"];
self.datelabel.text = [formatter stringFromDate:appDelgate.dateofbirth];

关于ios - cocoa touch 如何将标签设置为 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19745006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com