Array count is 0 & UITableView _endCellAnimationsWithContext - objective-c

I am trying to add an object to my mutable array but when when I do it crashes. I am sure that my array has been alloc and init.
Here is my code:
I have tried to figure out what is wrong, it seems my code works perfectly fine if I out comment the line [self.tableView insertRowsAtIndexPaths...]
The only problem is that my tableView is not updating then :'( (if I out comment the line).
Here is my error msg:
And here is my consol when I have out commented inserRowsAtIndexPaths... line:
As you can see everything seems to work fine.
Extra info:
I don't know if you need it but here is my UITableViewDataSource :)
Thank you
Anders

I believe your problem is that you never alloc-init'd your array. If that's the case, you're trying to stuff items into something that just isn't there. You say you're sure that it has been already, but I would do some debugging to make double sure. That was what caused me to get this same error message; I had accidentally deleted my init method earlier.

When I take look at your logs, I can see this 'Indexpath = 0' which means that you have nothing in your Array (self.budgets.items), try to debug your dataSource array(Actual data you want to display on tableView) first.

Related

Change value of multidimensional array in objective c?

I am having the strangest problem and looking for help. I have an array of arrays of integers. I am trying to change these values and I cannot find out how. Here is what I tried:
[[multidim-array objectAtIndex:0] replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:1]];
what this does is get the first array from the main array and change the integer at the first index to one. Or at least, that's what I think it should be doing. and its what the other threads here said it should do. But I am getting strange behavior.
The above code, for some reason, changes the first value of EVERY array to 1. It's as if I run the code 10 times on every array in the multidim-array array.
Anyone know why this could be happening? If my phrasing was unclear, what I am trying to do is the objective-c equivelant of multidim-array[0][0] = 1;
EDIT: Someone told me this should work so here is info about how I am handling the array. The array starts out filled with 0s. There is a lot of code affecting the array but it shouldn't matter because I have breakpoints trailing through so I can see what each line of code does to the array. Before I run that line, the numbers are all 0s. I try and change one value, and they a whole bunch of them become changed. I am still looking into it if no one knows what's happening :/
I'm guessing that you've assigned the same inner array to every element of the outer array.
BTW, you can actually write this:
multidim_array[0][0] = #1;

Update content of NSOutlineView

I have a NSOutlineView that show the content controlled by a NSTreeController which I bind to an NSMutableArray (arrayOfFiles). The array contains NSTreeNode objects where the representedObject (Fileobject class) holds a number of ivars. I would like to edit and update the ivar named "direction" for specific objects. I manage to get my object of interest using a NSIndexPath which I have stored for each object.
[self.myOutlineViewController.myTreeController setSelectionIndexPath:myIndexPath];
Fileobject *myObject =[[[self.myOutlineViewController.myTreeController.arrangedObjects descendantNodeAtIndexPath:myIndexPath] representedObject] representedObject];
[myObject setDirection:0];
This works fine, but I run into problems when I want to update the object I just extracted at NSIndexPath. The following crashes:[self.myOutlineViewController.myTreeController removeObjectAtArrangedObjectIndexPath:myIndexPath]; with the error message
An uncaught exception was raised
2012-11-08 17:23:25.557 S3access[20379:12b03] *** -[NSKeyValueSlowMutableArray removeObjectsAtIndexes:]: value for key myArrayOfFiles of object 0x40012e460 is nil
I understand i am doing something wrong with Key-Value coding here but I am unable to see what it is. I have tried to seek solutions in all of the examples from Apple but I can only find examples that do not use NSTreeController and NSTreeNode. My thought was to 1) extract the object at indexpath 2) edit the extracted object 3) remove current object at indexpath 4) insert my new edited object into indexpath using command [self.myOutlineViewController.myTreeController insertObject:myNode atArrangedObjectIndexPath:myIndexPath];. I dont see how I can replace my object using Key-Value coding when I dont replace only an ivar but the whole object?
Any suggestions for what I am doing wrong and suggestions for how I may solve this is highly appreciated.
Cheers, and thanks! Trond
I finally managed to get my update of NSOutlineView to work properly, and since my question was an extreme tumbleweed I thought I would at least update with an answer. It turns out my problem was more a lack of proper understanding of KVC rather than a programming problem. After having read answers to questions on Stackoverflow and the Apple documentation I finally figured out how to enable a dynamic update of my NSOutlineView and its NSTreeController contents (arrangedObjects) which are represented using NSTreeNode. The following code worked for me assuming you know the NSIndexPath (myIndexPath) of your object:
[self.myOutlineViewController.myOutlineView willChangeValueForKey:#"direction"];
[[[[self.myOutlineViewController.myTreeController.arrangedObjects descendantNodeAtIndexPath:myIndexPath] representedObject] representedObject] setDirection:0];
[self.myOutlineViewController.myOutlineView didChangeValueForKey:#"direction"];
Hopefully this may help some others. Cheers, Trond

When is a NSManagedObject really accessible?

I have a problem.
I have an NSObjectController called "mapController" and I want to put some defaults when the object is created. I do this inside the windowControllerDidLoadNib method of my document, as suggested by the docs. But…
if (![mapController content]){ // No map defined yet.
[mapController add: self]; // This should create the instance.
NSLog(#"%#",[mapController content]); // Gives NULL.
I tried:
BOOL ok = [mapController fetchWithRequest:nil merge:NO error:nil];
NSLog(#"%#",[mapController content]); // Gives NULL.
The content of mapController is in the Core Data "scratch pad" but I can't access it. I have to set one of its attributes like this:
[[mapController content] setValue:[matrix colorReference] forKey:#"mapData"];
This gives no error, the file is marked as changed, but it I test the value:
NSLog(#"%#",[mapController content]); // Gives NULL.
When the heck it the controller's content really HERE? Something appears on the screen but… what actually? Reading the docs doesn't help me…
OK, I found the answer in the docs:
add: Creates a new object and sets it as the receiver’s content object.
Discussion
Creates a new object of the appropriate entity (specified by
entityName) or class (specified by objectClass)—see newObject—and sets
it as the receiver’s content object using addObject:.
Special Considerations
Beginning with Mac OS X v10.4 the result of this method is deferred
until the next iteration of the runloop so that the error presentation
mechanism can provide feedback as a sheet.
That's why
[[mapController content] setValue:[matrix colorReference] forKey:#"mapData"];
worked fine when called elsewhere in the app. It was a few iterations later…
Well… maybe this post will save you a couple of hours you could use to sleep longer.
Regards,
Bernard
I don't think its your mapController, I think it is your NSLog. Try this:
NSLog(#"%#", mapController);
also try getting simple data from the content, like the float value of the CGColorRef so you can use other formatters like %f.
I would have tested this but I cannot seem to create an instance of NSObjectController because it is an undeclared identifier. What framework is it defined in? Did you have to #import anything?

iPhone application error. NSString with whitespace

I am having a major problem with my iPhone app. My problem seems basic but I cannot find out the reason for the error. I have a NSString saveDescription that is set by user's input.
savede = civilsJobDescTb.text;
I then call in a NSLog to check the string has been set, and it is. So I have no problem there.
Later in the program I create a PDF file with several details. Everything works ok except the NSString that I have set earlier. It seem as if the string no longer exists. I get an error from it. EXC_BAD_ACCESS or something like that.
Now for the weird thing. This only happens when I have a whitespace in the text box. Eg: I enter "ok" and it will work without problem. But if I enter "Everything is ok" then I get errors.
I can't see where the problem is. Any ideas?
You didn't specify if your "savede" is an ivar (instance variable) but assuming that it is,
and if you are using ARC, set a strong property to it:
#property (nonatomic, strong) NSString * savede;
self.savede = civilsJobDescTb.text;
If you are not using ARC, then make sure to retain your "savede" string after setting it. Something like:
savede = civilsJobDescTb.text;
[savede retain];
(making sure to release it when you are truly finished with it).
You can also make a copy of the string (in case the label that it's coming from disappears, or is cleared, or somehow disappears).
[savede setString: civilsJobDescTb.text];
[savde retain];

Cocos2d update:(ccTime)dt giving screwed up values

Inside of a class that subclasses CCNode, I have scheduled an -update:(ccTime)dt method. I also have a bunch of behavior objects that don't subclass CCNode, but also have an -update:(ccTime)dt method. Here's the inside of my CCNode's update method:
-(void)update:(ccTime)dt{
for(Behavior *currentBehavior in behaviors){
[currentBehavior update:dt];
}
}
When I NSLog the dt value passed into my CCNode's update, it prints out normal values (0.116699, 0.162726). However, when I NSLog the dt value from inside the behaviors' update methods, the printed numbers are all of a sudden really screwed up (0.000, 36893488147419103232, -2.000). It's the strangest thing. When I debug it, I'll see that the first dt value is normal, and then I'll step inside the behavior's update, and the value will suddenly change to something crazy. What's going on?
I figured it out. I didn't have -update:(ccTime)dt in my behavior class's .m file, and my theory is that it took the complier extra time to look for the method selector, which therefore screwed up the ccTime.