0

I basically have have a custon subclass of an UIViewController, which has a NSMutableArray called waypoints. I initialise it in the -(void)viewDidLoad method of the controller with

waypoints = [[NSMutableArray alloc] init];

Later, in a method which gets called via a presentedViewController with some parameters including a NSMutableDictionary as waypointData, I call

[waypoints insertObject:waypointData atIndex:0];

and I also tried

[waypoints addObject:waypointData];

But neither seems to work!

I logged some stuff there to make it more clear. The parameters get transmitted correctly and the NSMutableDictionary saved under waypointData is the correct content it should be. Logging the waypoints array before the insertion shows it empty (which is correct; app got launched; no data added yet) and after the insertion it's still empty. The log:

2014-02-19 14:40:11.050 xxx[xxx] waypoints before insertion: (null:)
2014-02-19 14:40:11.051 xxx[xxx] INSERT WAYPOINT
2014-02-19 14:40:11.052 xxx[xxx] waypoints after insertion: (null:)

INSERT WAYPOINT gets logged directly before the insertion, so the program routine is really executing the insertObject:atIndex: method.

TL;DR: Even though insertObject:atIndex: (and -addObject:) for an NSMutableArray get called the object won't get inserted in the array.

EDIT:
This method gets called in -viewDidLoad too:

- (void)loadWaypoints {
    id unarchivedObject = [NSKeyedUnarchiver unarchiveObjectWithFile:[[self applicationDocumentsDirectory] stringByAppendingString: kAppDataFilePlistName]];
    waypoints = (NSMutableArray *)unarchivedObject;
}

unarchivedObject of course is NULL if there hasn't been anything saved yet. Thanks to 0x7fffffff.

8
  • What are you logging exactly ? Could you show us the NSLog line ? Commented Feb 19, 2014 at 13:58
  • set et breakpoint in viewDidLoad and tell me if it breaks Commented Feb 19, 2014 at 13:58
  • It sounds to me like the array is nil. Are you assigning any value directly to waypoints in-between the insertion and the alloc/init? Please include more code from within this method. Commented Feb 19, 2014 at 13:58
  • 1
    It looks like waypoints at the point you are trying to add waypointData is nil, that is it is not instantiated at that point. Not that waypointData is null. How is waypointData declared? Add more of the code including the declaration of waypoints . Commented Feb 19, 2014 at 14:00
  • Please show more of the code. It's possible that waypoints is nil at the time of those calls to add and insert objects. Commented Feb 19, 2014 at 14:01

4 Answers 4

4

Check your waypoints NSArray it shouldn't be nil when you call at [waypoints addObject:waypointData];

Sign up to request clarification or add additional context in comments.

2 Comments

@0x7fffffff Perhaps it is just a languish issue. "should" -> "should not".
Yeah, sorry, I'm not native in english, sometimes I do mistakes ;)
1

easiest way to make sure your 'waypoints' array isn't nil is doing this:

if (!waypoints)
{ 
    waypoints = [NSMutableArray new];
}
[waypoints insertObject:waypointData atIndex:0];

or

if (!waypoints)
{ 
    waypoints = [NSMutableArray new];
}
[waypoints addObject:waypointData];

4 Comments

And as written in many many places: don't use new, use alloc init or just array class method for initialising.
I don't see the point, new just do alloc init it works the same, just a subjective opinion in the end.
Not that long ago new was not in favor but recently even Apple is suggesting to use new. Personally I find new concise and short.
But the main issue here is poor control over when and how waypoints is declared. This bandaid solution is a terrible solution. Lazy initialization is not usually a good design pattern and when used there should be a through understanding of why and clear thought about the consequences. In this case it is clear (no compiler warnings) that waypoints is declared somewhere, it is instantiated and yet when used is nil, this points to a greater programming error.
0

It sounds like your waypoints array is nil. Post your header file, including the declaration of waypoints. Post your whole viewDidLoad method where you create the empty array.

The 2 most likely causes I can think of are that you declared waypoints as weak, or that you have a local variable waypoints in your viewDidLoad and you're creating an empty array in the local variable but not saving it to the instance variable.

Set a breakpoint in viewDidLoad at the line that creates the empty array. Step over it and make sure your array is being created. Then right-click on the variable down in the variables section of the debugger window and add a watchpoint to that variable. If it is getting cleared out then the breakpoint will cause your program to break at the offending line.

(My post assumes that you are using ARC. Are you?)

Comments

-4

Please use following code:

if(waypoints == nil){

waypoints = [[NSMutableArray alloc] init];

}

[waypoints addObject:waypointData];

1 Comment

this will remove all existing objects in the array each time he adds a new object and only leaving him with the last object he added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.