0

I have static variable:

static PaginationTableRestaurants *pagination;

After in implementation I do:

pagination.next = 3;
NSLog(@"%@", pagination.next);// nil

It gives me nil, why?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Abamazi
  • 39
  • 5

1 Answers1

1

You never initialized the pagination variable, judging by your code. If you try to set a property on a nil instance, your method call cannot work.

If it is actually important to you that that variable is static, you can achieve that behavior by getting it through a class method like this.

+ (NSArray *)restaurants
{
   static NSArray *_restaurants;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
      _restaurants = @[
                  @"Restaurant 1",
                  @"Restaurant 2",
                  @"Restaurant 3",
                  @"Restaurant 4",
                  @"Options"
                  ];
   });
   return _restaurants;
}

The reality is that you almost certainly just want to declare a property and initialize it later.

@interface ViewController ()
@property (strong, nonatomic) NSArray *restaurants;
@end
Dare
  • 2,497
  • 1
  • 12
  • 20