Static variable cannot be accessed from another class

I have a static variable that i want to access from another class in the same project in X-Code. I have declared it in the .h file AND the .m file, gave it a value, and then when i accessed the other class, i got an error message saying that:

"Property 'xx' is not found on object of type 'yy'"

i declared the variable as extern in the .h, and redeclared it as the variable type in the .m. I have tried to change it to static in the .h, but it still doesn't work. And yes, i have imported the file containing the variable, in case that is the problem.

Can anyone help me?

EDIT:

this is the code that i'm currently using:

source.h

> static int anObject;
> 
> @interface source : NSObject

source.m

> static int a = 2
> 
> @implementation source

destination.m

# include "source.h"

> @implementation destination
> 
> -(void) anObjectTestFunction 

> {     
> printf("%d", source.anObject); //the first version
> printf("%d", anObject); //second version  
> }

now after i went to the second version, the variable anObject in destination.h can be accessed, but its value is not 2, it's 0. I want it to follow the one i declared in source.h.

Answers


I am assuming the static variable declared in the .h file is outside the @interface. So something like:

static NSString *myObjectTest = @"Test";

@interface MyObject : NSObject 
@end

If that is the case then you will not be able to access it using something like:

MyObject *obj = [[MyObject alloc] init];
[obj myObject]

or

obj.myObject

That is what is giving you the "Property 'xx' is not found on object of type 'yy'". That static variable is not a property on the object of MyObject.

That static variable is accessible like so myObjectTest as long as you import the .h file

Update See Chuck's comment below why this is a bad idea to do this way.


Need Your Help

BZip2 file read in Hadoop

hadoop gzip mapper bzip2

I heard we can use multiple mappers to read different parts of one bzip2 file in parallel in Hadoop, to increase performance. But I cannot find related samples after search. Appreciate if anyone co...

restart script after reaching certain limit

php

Didn't see a question similar to this, so here goes:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.