首页 > 解决方案 > 如何从 NSTextField 设置静态 int

问题描述

我无法从 UI NSTextField 设置静态 int。如果我在下面显示的代码片段中手动设置我的静态值:

int startVal = [startingTextField intValue];
static int theIndex = 1;

它当然不会产生错误。但是,如果我尝试从 textField 中获取的 int 设置静态(如我所愿),如下所示:

int startVal = [startingTextField intValue];
static int theIndex = startVal;

我在第二个(静态)行上得到一个令人惊讶的“初始化器元素不是编译时常量”错误。是什么原因造成的,正确的语法是什么?

标签: objective-c

解决方案


该消息解释了它。如果将其设置为 1,则编译器可以这样做,因为它在编译时是已知的。您将不得不以不同的方式进行操作,例如

static int theIndex = 1;

然后当你知道价值时,以某种方式

theIndex = [startingTextField intValue];

问题在于初始化,而不是分配。


推荐阅读