-2

I have 2 arrays that are in a singleton class(globals), and i have this :

 [[Globals sharedGlobals].RecentLoadFromServer addObjectsFromArray:images];

NSLog(@"%ld",[[Globals sharedGlobals].RecentLoadFromServer count]);


 [[Globals sharedGlobals].CurrentLoad addObjectsFromArray:[Globals sharedGlobals].RecentLoadFromServer];

NSLog(@"%ld",[[Globals sharedGlobals].CurrentLoad count]);

first one have 500, second one have 0 . Whats wrong here ?

in the singleton i don't allocate them(should i?) and i have :

@property(nonatomic,strong) NSMutableArray *RecentLoadFromServer;
@property(nonatomic,strong) NSMutableArray *CurrentLoad;
Curnelious
  • 1
  • 16
  • 76
  • 150
  • 4
    You can't add objects to an array that isn't instantiated. Plus, I really don't like this pattern. There is always a better way than using Singletons like this. – Fogmeister Sep 08 '14 at 12:16
  • Dollars to donuts there is no array -- it's not been created yet. – Hot Licks Sep 08 '14 at 12:30
  • What is a good alternative to the Singleton pattern? Im using it to manage a class that handles all my image data manipulation (loading, enumerating, saving, etc). – scord Sep 08 '14 at 14:53
  • 1
    @scord have a look at these mate http://stackoverflow.com/questions/5912541/alternative-to-singleton-in-objective-c-for-better-application-design and http://blog.dadabeatnik.com/2013/07/28/objective-c-singletons-an-alternative-pattern/ Hope they are useful, alternatively ask your own question, I will advise though that just like the first link I supplied it could attract downvotes do it being opinion based answers. Good luck – Popeye Sep 08 '14 at 14:55

1 Answers1

0

So, my mistake was that when you do :

 [[Globals sharedGlobals].RecentLoadFromServer addObjectsFromArray:images];

You get a good result because the array has objects in it already , but when you do it on an empty array, you get nothing .(empty array).

Solved by mutable copy to the array in the second example .

Taryn
  • 242,637
  • 56
  • 362
  • 405
Curnelious
  • 1
  • 16
  • 76
  • 150
  • Any "call" using a null pointer silently "succeeds". That's the way Objective-C works (for some reason). – Hot Licks Sep 08 '14 at 16:17
  • More specifically, any message passed to `nil` is effectively a no-op and will return `0`, `NO`, or `nil`. – wjl Sep 08 '14 at 16:28