仅当我使用中间变量时才有效

发布于 2024-09-19 12:40:20 字数 775 浏览 12 评论 0原文

我试图将 UILabel 的文本设置为等于 NSDateComponents 中表示的星期几的名称。我使用以下代码:

NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
dateComponents.weekday = 1; //Sunday
NSString * const weekdayNames[8] = {@"Array starts at 1", @"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday"};

UILabel *myLabel = [[[UILabel alloc] init] autorelease];
myLabel.text = weekdayNames[dateComponents.weekday]; //compiler error: Assignment of read-only variable 'prop.49'

我可以通过以下三种方式中的任何一种使代码工作:

  1. 使 weekdayNames 不是 const
  2. 将 dateComponents.weekday 分配给中间 int 变量,然后将其用作数组索引
  3. 将 weekday[dateComponents.weekday] 分配给中间变量NSString * 调用 setText 之前的变量:

但我想知道为什么我的代码(如最初编写的那样)失败了。

I am trying to set the text of a UILabel to be equal to the name of the day of the week represented in an NSDateComponents. I am using the following code:

NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
dateComponents.weekday = 1; //Sunday
NSString * const weekdayNames[8] = {@"Array starts at 1", @"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday"};

UILabel *myLabel = [[[UILabel alloc] init] autorelease];
myLabel.text = weekdayNames[dateComponents.weekday]; //compiler error: Assignment of read-only variable 'prop.49'

I can make the code work in any of three ways:

  1. Make weekdayNames not be const
  2. Assign dateComponents.weekday to an intermediate int variable before using it as an array index
  3. Assign weekday[dateComponents.weekday] to an intermediate NSString * variable before calling setText:

But I want to know why my code, as originally written, fails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2024-09-26 12:40:20

您没有正确初始化 NSDateComponents,因此工作日不会返回您期望的值。请参阅文档。

现在您将使用当前日期对其进行初始化:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [weekdayComponents weekday];

稍后编辑:初始化问题的一部分,以修复编译器错误更改:

NSString * const weekdayNames[8] =

NSString const * weekdayNames[8] =

(指向常量 NSString 的指针)。

但是,当您这样做时,您将收到警告“传递‘setText:’的参数 1 会丢弃指针目标类型中的限定符”,因为您正在将常量指针传递给需要指针的函数。要修复该警告,您可以将 setText 的参数转换为 (NSString *)。

完全删除 const 限定符可能是有意义的。
NSString 在 Objective-C 中已经是不可变的,因此它们已经是常量。

You are not initializing NSDateComponents properly, thus weekday does not return the value you expect. See the documentation.

This is now you would initialize it using the current date:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [weekdayComponents weekday];

Later edit: Initialization is part of the problem, to fix the compiler error change:

NSString * const weekdayNames[8] =

to

NSString const * weekdayNames[8] =

(pointer to a constant NSString).

However, when you do that you will hit a warning "passing argument 1 of 'setText:' discards qualifiers from pointer target type" because you're passing a constant pointer to a function that expects a pointer. To fix the warning you can cast the argument to setText to (NSString *).

It probably makes sense to remove the const qualifier altogether.
NSStrings are already immutable in Objective-C, so they're already constants.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文