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.
Example:
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; }
Related Posts:
- What does \0 stand for?
- Xcode build failure “Undefined symbols for architecture x86_64”
- Difference between nil, NIL and, null in Objective-C
- printf(“%%%s”,”hello”)
- What is causing this: Cannot jump from switch statement to this case label
- Decode String with Emoji characters such as \ud83d
- What is the meaning of id?
- Include of non-modular header inside framework module
- Difference between nil, NIL and, null in Objective-C
- Using global variables in a function
- What is the easiest way to initialize a std::vector with hardcoded elements?
- How to initialize an array in Java?
- Initialize part of an array in java
- What does “collect2: error: ld returned 1 exit status” mean?
- What does “collect2: error: ld returned 1 exit status” mean?
- Initialization of an ArrayList in one line
- Compiler Error “void value not ignored as it ought to be” in C programming [duplicate]
- dereferencing pointer to incomplete type
- How to declare a global variable in php?
- Java “Error occurred during initialization of VM” fix?
- Could not load file or assembly or one of its dependencies
- error: expected unqualified-id error: Meaning and fix? [duplicate]
- Could not load file or assembly or one of its dependencies
- c++ compile error: ISO C++ forbids comparison between pointer and integer
- error: redefinition of class
- C++ Error: No match for ‘operator=’
- Python3: ImportError: No module named ‘_ctypes’ when using Value from module multiprocessing
- filedialog, tkinter and opening files
- Java compile error: “reached end of file while parsing }”
- What does “collect2: error: ld returned 1 exit status” mean?
- “error: assignment to expression with array type error” when I assign a struct field (C)
- Non-static variable cannot be referenced from a static context
- Identifier not found error on function call
- No acceptable C compiler found in $PATH when installing python
- Non-static variable cannot be referenced from a static context
- Error “initializer element is not constant” when trying to initialize variable with const
- Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3
- Non-static variable cannot be referenced from a static context
- What is an ‘undeclared identifier’ error and how do I fix it?
- Identifier not found error on function call
- Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3
- What is an “internal error” and how do I fix it?
- What is an ‘undeclared identifier’ error and how do I fix it?
- How do I use extern to share variables between source files?
- C ++ error: a expected initializer before [function name]
- The located assembly’s manifest definition does not match the assembly reference
- invalid new-expression of abstract class type
- C programming, error: called object is not a function or function pointer
- “unrecognized selector sent to instance” error in Objective-C
- Python function global variables?
- Double array initialization in Java
- The located assembly’s manifest definition does not match the assembly reference
- what does “error : a nonstatic member reference must be relative to a specific object” mean?
- Error: C++ requires a type specifier for all declarations
- “Symbol(s) not found for architecture x86_64” on QtCreator project
- error: redefinition of class
- libpthread.so.0: error adding symbols: DSO missing from command line
- C++ error: “Array must be initialized with a brace enclosed initializer”
- Extension methods must be defined in a non-generic static class
- Xcode – How to fix ‘NSUnknownKeyException’, reason: … this class is not key value coding-compliant for the key X” error?
- Why do I get a “referenced before assignment” error when assigning to a global variable in a function?
- Else without if
- C compile error: “Variable-sized object may not be initialized”
- Error: Could not find or load main class in intelliJ IDE
- error: expected primary-expression before ‘)’ token (C)
- How to initialize a struct in accordance with C programming language standards
- Use of “global” keyword in Python
- What’s the purpose of ini_set() in php? (especially for error reporting)
- what does “error : a nonstatic member reference must be relative to a specific object” mean?
- Thread 1: EXC_BAD_ACCESS (code=1, address=0xf00000c)
- How to use iMonkey in an iOS app
- Extension methods must be defined in a non-generic static class
- Don’t understand why UnboundLocalError occurs (closure) [duplicate]
- Java – String cannot be converted to int
- How to fix “namespace x already contains a definition for x” error? Happened after converting to VS2010
- How can I use an array of function pointers?
- Why do I get a “referenced before assignment” error when assigning to a global variable in a function?
- C compile error: Id returned 1 exit status
- Resolve build errors due to circular dependency amongst classes
- How do I fix “Undefined variable” error in PHP?
- How to initialize array to 0 in C?
- C++ error: definition of implicitly-declared
- Defining lists as global variables in Python
- (.text+0x20): undefined reference to `main’ and undefined reference to function
- cc1plus: error: unrecognized command line option “-std=c++11” with g++
- Variable might not have been initialized error
- extra qualification error in C++
- How to modify a global variable within a function in bash?
- How to modify a global variable within a function in bash?
- global variable for all controller and views
- malloc: *** error: incorrect checksum for freed object – object was probably modified after being freed
- how to initialize an empty integer array in c++
- Swift protocols: method does not override any method from its superclass
- What is the default initialization of an array in Java?
- Cannot open output file, permission denied
- C++ – Too Many Initializers for Arrays
- What does “error: ‘.class’ expected” mean and how do I fix it
- Duplicate symbols for architecture x86_64 under Xcode
- Initializing multiple variables to the same value in Java
- Call to non-static member function without an object argument compiler error