-1

I have an NSArray that contains a couple of items. The NSArray is loaded into a uitableview from another class. when I back out of the detail view and re-enter (for the 3rd time), the NSArray is empty and the tableview is empty as well. What is happening? (I am using garbage collector so I don't think it is a leak)

myMatch = [[Match alloc] initWithId:1 OpponentId:13];
rooftop
  • 3,031
  • 1
  • 22
  • 33
michaela
  • 173
  • 1
  • 2
  • 13
  • 3
    1) There is no garbage collector for the iPhone, just ARC. 2) There is nothing anyone can do to help you with just that 1 line of code. – Joe May 01 '12 at 18:55
  • OK well I just grab an array from a method within that class and add it to the table view. Why would it all of the sudden disapear ? – michaela May 01 '12 at 18:57
  • 2
    You need to provide more code so we can better see what is going on. :) – Luke May 01 '12 at 18:58
  • Can you post the code (including the class name) where you set the value of that NSArray and the code (including the class name) where you use it for the datasource for the TV? – LJ Wilson May 01 '12 at 18:59
  • you don't "add it to the table view", you just use it in it's delegate methods... please add more code... – meronix May 01 '12 at 19:02
  • can u tell me what exactly are u doing while allocating Match. u can email me at waleed.khan@cubixlabs.com – WaaleedKhan May 01 '12 at 19:03
  • @BrainStorms That information should be included in the post for historical reason. Also posting your full email is a good way to spammed by bots that scan sites. – Joe May 01 '12 at 19:44

2 Answers2

1

The best answer that I can think of is to properly retain the NSArray. In my experience (I've encountered similar problems to this before) I hadn't set up the NSArray properly. Have you, in the header file, used @property? @property(nonatomic, retain)NSArray *myArray; In the implementation part, you can use @synthesize for the array. Without going through these steps, the array may be released after the method you used to initialize it, exits. Thus, you must retain it!

waylonion
  • 6,866
  • 8
  • 51
  • 92
1

I've seen this before in the following context.

There's a good chance that you've defined the NSArray as:

@property (weak, nonatomic) NSArray *myMatch;

Change this to:

@property (strong, nonatomic) NSArray *myMatch;

Assumptions:

1) there is no garbage collector in iOS, so I'm guessing you mean you are using Automatic Reference counting.

2) you aren't changing from one view to another (I.e. you aren't leaving the xib or scene that has the UITableView on it)

3) the array has been defined as an @property

When should you use weak or strong references?

A good 'rule of thumb' for beginners is that a property should only be weak if it refers to a control on the view, e.g. @property (weak, nonatomic) IBOutlet UILabel *myLabel; In most other cases the property should be strong.

A strong pointer means 'hold a strong reference to this data in memory'. A weak pointer means 'if no one else is interested in this data, I'm not interested in it either.' Controls on a view normally only require weak pointers because they belong to the currently loaded view, which has a strong pointer to it.

bndouglas
  • 135
  • 1
  • 3
  • Thank you for your advice, but I had it set to (nonatomic, retain). upon changing it to (nonatomic, strong) it still does the same thing. I really appreciate your answer, and I now know the difference between weak and strong. Could you possibly think of another solution? here is more code: http://stackoverflow.com/questions/10408584/nsarray-clears-after-3-times-view-did-load – michaela May 02 '12 at 07:37
  • I'll have a look at the other post, but I'm not convinced you fully grasp strong and weak. If you are using ARC then you shouldn't have "retain" anywhere in your code. This is how things were done before ARC. – bndouglas May 03 '12 at 11:39