In GLViewController.m file
At the very top of Implementation
NSArray* imageArray ;
Then in GLViewController.m inside GLViewController
- (id)initWithCoder:(NSCoder*)coder
{
imageArray = @[ @"baban.jpg",@"cete.jpg",@"cipan.jpg",@"kuc.jpg"];
}
Inside drawView in GLViewController.m
NSLog(imageArray[0]); //Fails
I've got it declared in the GLViewController.h file too:
NSArray* imageArray;
I am including GLViewController.h in the GLViewController.m
Your question is not clear. E.g. have you declared imageArray
as an instance variable, a global variable, or possibly both – "At the very top of Implementation" could be read as "top of file", "top of implementation just below @implementation
in braces" etc.
So we're guessing, let's hope it helps.
First, from what you report; that the init runs but in and instance method accessing the instance variables "fails" in some non-specified way; you may have two different variables declared in different scopes. Without a minimal reproducible example we can't be more specific. What you can try is to insert:
NSLog(@"Loc A imageArray @%p = %p", &imageArray, imageArray);
in various locations, where you change the "A" (to "B", "C"...) for each one, where you are accessing imageArray
. This will print the address of the variable itself, from which you can determine whether you are always referring the same variable, and the variable's contents as an address, from which you can determine if the value is changing i.e. whether it's nil
or the array it references has changed.
Second, you write "I've got it declared in the GLViewController.h file too". You normally do not declared instance variables in the .h
file, though you can, and you do not re-declare global variables in a .h
and doing so should produce a compiler error – you don't report such an error which leads to the possibility covered above that you may have two variables in different scopes.
If you wish to declare a global variable in file.m
and have it accessible to other code in a different file then in file.h
you include an extern
declaration for it:
extern NSArray* imageArray;
The extern
states that this line is not declaring the variable itself but that the variable is declared elsewhere and accessible. (You might wonder why you don't add extern
to functions/methods, in those cases the extern
is implicit.)
Hope that helps in some way. If this doesn't help at all I suggest you ask a completely new question providing more detail, the minimum reproducible example, etc. (If you edit this question now people may miss the edits thinking they've seen the question.) You can always delete this question to reduce clutter.