Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Xcode reporting a "defined but not used" warning for my class variable?

I am getting a warning on this line in my header, but I am using the class variable in my implementation (in both class methods and instance methods):

#import <UIKit/UIKit.h>

static NSMutableArray *classVar; // Xcode warning: 'classVar' defined but not used

@interface MyViewController : UIViewController {
like image 437
gerry3 Avatar asked Oct 21 '09 20:10

gerry3


3 Answers

This variable is not a class/instance variable. Each time when the header file is included to .m file, the compiler creates a new static variable with scope limited to the file that includes this header. If you're trying to get a class level variable, move the declaration to the beginning of respective .m file.

like image 191
Oleksandr Tymoshenko Avatar answered Nov 12 '22 17:11

Oleksandr Tymoshenko


A static variable has file scope. Since Xcode can't find the variable being used in that file, it sees an unused variable. If you actually want the variable to be accessible from your whole program, make that an extern variable declaration and define it in your implementation. If it's only meant to be used by that class, just move the static variable into your implementation file.

like image 20
Chuck Avatar answered Nov 12 '22 18:11

Chuck


You have placed the classVar outside the interface definition. This will make the compiler think you are declaring a global variable, and as this looks like it is a header file (.h) it will also be created in all files including this header file. I'd guess the warning comes when compiling a file other than MyViewController.m that includes this header file.

EDIT My suggestion is that you move the classVar into the .m file for MyViewController (miss-interpreted what you where after first)

like image 3
epatel Avatar answered Nov 12 '22 16:11

epatel