Xcode 语义问题“Initializer element is not a compile-time constant" [英] Xcode semantic issue "Initializer element is not a compile-time constant "

查看:21
本文介绍了Xcode 语义问题“Initializer element is not a compile-time constant"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用 NSMakeRange 方法时遇到此错误.我做错了什么?

I have this Error with calling NSMakeRange method. What I'm doing wrong?

#import <Foundation/Foundation.h>
//
NSRange range1 = NSMakeRange(12, 5);

推荐答案

在函数或方法之外初始化变量时,只能使用常量值:不能执行任何代码.这里的问题是您正在尝试执行 NSMakeRange.(参见这个问题的答案,这是相似的).

When you initialize a variable outside of a function or method, you can only use constant values: you can't execute any code. Here the problem is that you're trying to execute NSMakeRange. (See the answers to this question, which is similar).

解决方法是声明range1但不给它赋值,然后实现一个+initialize方法来设置值.initialize 是在类中调用任何其他方法之前调用的类方法.

The solution is to declare range1 but not assign any value to it, and then implement an +initialize method that sets the value. initialize is a class method that is invoked before any other methods are called in your class.

+ (void)initialize {
    if (range1 == NULL) {
        range1 = NSMakeRange(12, 5);
    }
}

这篇关于Xcode 语义问题“Initializer element is not a compile-time constant"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆