<i id='P2aVx'><tr id='P2aVx'><dt id='P2aVx'><q id='P2aVx'><span id='P2aVx'><b id='P2aVx'><form id='P2aVx'><ins id='P2aVx'></ins><ul id='P2aVx'></ul><sub id='P2aVx'></sub></form><legend id='P2aVx'></legend><bdo id='P2aVx'><pre id='P2aVx'><center id='P2aVx'></center></pre></bdo></b><th id='P2aVx'></th></span></q></dt></tr></i><div id='P2aVx'><tfoot id='P2aVx'></tfoot><dl id='P2aVx'><fieldset id='P2aVx'></fieldset></dl></div>

        <tfoot id='P2aVx'></tfoot><legend id='P2aVx'><style id='P2aVx'><dir id='P2aVx'><q id='P2aVx'></q></dir></style></legend>

          <bdo id='P2aVx'></bdo><ul id='P2aVx'></ul>

        <small id='P2aVx'></small><noframes id='P2aVx'>

      1. 编译器错误:“初始化器元素不是编译时常量";

        Compiler error: quot;initializer element is not a compile-time constantquot;(编译器错误:“初始化器元素不是编译时常量;)

        1. <tfoot id='oeKia'></tfoot>
              <tbody id='oeKia'></tbody>

            • <i id='oeKia'><tr id='oeKia'><dt id='oeKia'><q id='oeKia'><span id='oeKia'><b id='oeKia'><form id='oeKia'><ins id='oeKia'></ins><ul id='oeKia'></ul><sub id='oeKia'></sub></form><legend id='oeKia'></legend><bdo id='oeKia'><pre id='oeKia'><center id='oeKia'></center></pre></bdo></b><th id='oeKia'></th></span></q></dt></tr></i><div id='oeKia'><tfoot id='oeKia'></tfoot><dl id='oeKia'><fieldset id='oeKia'></fieldset></dl></div>
              <legend id='oeKia'><style id='oeKia'><dir id='oeKia'><q id='oeKia'></q></dir></style></legend>

              <small id='oeKia'></small><noframes id='oeKia'>

                • <bdo id='oeKia'></bdo><ul id='oeKia'></ul>
                • 本文介绍了编译器错误:“初始化器元素不是编译时常量";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  编译此代码时,我收到错误初始化程序元素不是编译时常量".谁能解释一下为什么?

                  When compiling this code, I get the error "initializer element is not a compile-time constant". Can anyone explain why?

                  #import "PreferencesController.h"
                  
                  @implementation PreferencesController
                  
                  - (id)init
                  {
                      self = [super init];
                      if (self) {
                          // Initialization code here.
                      }
                  
                      return self;
                  }
                  
                  
                  NSImage* imageSegment = [[NSImage alloc] initWithContentsOfFile:@"/User/asd.jpg"];//error here
                  

                  推荐答案

                  当您在函数范围之外定义变量时,该变量的值实际上会写入您的可执行文件.这意味着您只能使用常量值.由于您在编译时不了解运行时环境的所有信息(哪些类可用,它们的结构是什么等),所以在运行时之前您无法创建目标 c 对象,但常量字符串除外,它被赋予特定的结构并保证保持这种状态.你应该做的是将变量初始化为 nil 并使用 +initialize 来创建你的图像.initialize 是一个类方法,它将在你的类上调用任何其他方法之前被调用.

                  When you define a variable outside the scope of a function, that variable's value is actually written into your executable file. This means you can only use a constant value. Since you don't know everything about the runtime environment at compile time (which classes are available, what is their structure, etc.), you cannot create objective c objects until runtime, with the exception of constant strings, which are given a specific structure and guaranteed to stay that way. What you should do is initialize the variable to nil and use +initialize to create your image. initialize is a class method which will be called before any other method is called on your class.

                  例子:

                  NSImage *imageSegment = nil;
                  + (void)initialize {
                      if(!imageSegment)
                          imageSegment = [[NSImage alloc] initWithContentsOfFile:@"/User/asd.jpg"];
                  }
                  - (id)init {
                    self = [super init];
                    if (self) {
                      // Initialization code here.
                    }
                  
                    return self;
                  }
                  

                  这篇关于编译器错误:“初始化器元素不是编译时常量";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员
                  quot;Auto Layout still required after executing -layoutSubviewsquot; with UITableViewCell subclass(“执行 -layoutSubviews 后仍需要自动布局带有 UITableViewCell 子类)
                  How do I animate constraint changes?(如何为约束更改设置动画?)
                  How do I size a UITextView to its content?(如何根据内容调整 UITextView 的大小?)
                  Can#39;t get to work CocoaPods and Yosemite(无法开始工作 CocoaPods 和 Yosemite)
                  unable to use cocoapods after updating(更新后无法使用 cocoapods)
                  <i id='kExAn'><tr id='kExAn'><dt id='kExAn'><q id='kExAn'><span id='kExAn'><b id='kExAn'><form id='kExAn'><ins id='kExAn'></ins><ul id='kExAn'></ul><sub id='kExAn'></sub></form><legend id='kExAn'></legend><bdo id='kExAn'><pre id='kExAn'><center id='kExAn'></center></pre></bdo></b><th id='kExAn'></th></span></q></dt></tr></i><div id='kExAn'><tfoot id='kExAn'></tfoot><dl id='kExAn'><fieldset id='kExAn'></fieldset></dl></div>
                      <tbody id='kExAn'></tbody>
                  • <tfoot id='kExAn'></tfoot>

                      <small id='kExAn'></small><noframes id='kExAn'>

                        <legend id='kExAn'><style id='kExAn'><dir id='kExAn'><q id='kExAn'></q></dir></style></legend>

                          • <bdo id='kExAn'></bdo><ul id='kExAn'></ul>