-1

I have a NSMutablearray property in my class and i need to access it from another class what should i do ?? i tried to access it using a class method called breakfast , but it didnt see the property !!

This is my code

@interface FirstViewController : UIViewController { 
    NSMutableArray *food;
}

+ (NSArray*)breakfast;

@property (nonatomic , strong) NSMutableArray * food;

// in .m file

+ (NSMutableArray*)breakfast {
    return self.food ; // here class method dont see the property called food
}
  • 1
    You need to clarify your goal. Why do you need a class method? A class method does not work on any one instance. – rmaddy Nov 22 '13 at 16:18
  • Please clarify WHY are you trying to do - what's a big point for what you're doing - also read my answer for suggestion in case of I maybe guessed what are trying to do here. – Grzegorz Krukowski Nov 22 '13 at 16:24
  • i need to khnow why i cant see any of class property inside the class method ? – user2987058 Nov 22 '13 at 16:28
  • You can't access 'self' from a class method, as it doesn't exist. – Tim Nov 22 '13 at 16:28
  • ok thank u :) ,, i will find another way to access data from another controller – user2987058 Nov 22 '13 at 16:30
  • You can access the `food` property from other classes. You just need to have a reference to an actual instance of the `FirstViewController` to do so. There is no need for a class method in such a case. – rmaddy Nov 22 '13 at 16:36
  • but then it will be emty , i need to define astatic property – user2987058 Nov 22 '13 at 16:41
  • There is no such thing as a static property. And how would it be empty? If you have an instance of your view controller you can both set and get the `food` property value. – rmaddy Nov 22 '13 at 17:09
  • but i will put the property values in my controller , and read it from another one , when i create an instance every thing will be nil at begin , and i need to set it value in its contoller – user2987058 Nov 22 '13 at 17:16

2 Answers2

0

You can do something like this:

// FirstViewController.h
@interface FirstViewController : UIViewController {
}

+(NSMutableArray*) breakfast;

// FirstViewController.m
+(NSMutableArray*) breakfast
{
  static NSMutableArray* _breakfast = nil;

  if (_breakfast == nil)
  {
    // _breakfast alloc init
  }

  return _breakfast;
}
Greg
  • 25,317
  • 6
  • 53
  • 62
  • the breakfast array implemented outside the class method , i connect to server and get data from it and put these data in the food property , i need to access that data from another class so im trying to build a class method , here you want me to alloc and init the breakfast array inside the class method – user2987058 Nov 22 '13 at 16:18
  • This variable (_breakfast) will be allocated just once, first time you will be accessing this method. – Greg Nov 22 '13 at 16:34
  • I don't know what you trying to do. You can access it from another class like this: [[FirstViewController breakfast] addObject:@"object"]; NSString *str = [FirstViewController breakfast][0]; – Greg Nov 22 '13 at 16:44
-1

Biggest issue is that you cannot use "self.propertyname" inside static (+) methods. You need to have an instance of class to call self.

Other problem is that you have "Food" and "food" - different case in @property and getter you are trying to use - they are case-sensitive. Also you have NSArray and NSMutableArray in .m and .h file. On top of that - you don't really need this local variable declaration.

I think you described your problem in wrong way. If you want to pass data between your view controllers, check: Passing Data between View Controllers

If you want to have data available for applicataion in general - create singleton.

Community
  • 1
  • 1
Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71