Serialization array of custom objects iOS - ios

I know there is a lot of resources about that here but yet, I can't find what's going wrong with my code:
I have a class Level and two subclasses of Level: GameLevel and TransitionLevel. Each of this class implements the NSCoding protocol.
Then, I've got an array of Level (so it contains both classes of GameLevel and TransitionLevel). Saving the archive seems to work fine :
NSString *archivePath = [DOCUMENT_DIRECTORY stringByAppendingPathComponent:#"levels.archive"];
[NSKeyedArchiver archiveRootObject:levels toFile:archivePath];
Note that levels is the array I want to save. I can see the file, it's created and seems to contains what it's supposed to contain.
But when I want to retrieve this array :
NSString *archivePath = [DOCUMENT_DIRECTORY stringByAppendingPathComponent:#"levels.archive"];
levels = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
I've got this exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
I'm not sure what I'm missing here.

Either your implementation of initWithCoder: returns nil, either some part of your code tries to insert a nil value into an array.
You may go in the Breakpoint Navigator (⌘6) and add an Exception Breakpoint. Then, when the application raises an exception, the Debug Navigator will display the stack of the functions and methods currently executed. This would allow you to know precisely which method is trying to insert nil into an array.

Related

Realm throwing RLMException when checking equality on property

Whenever I try to check equality between a property on a Realm object and a NSInteger, it throws the following exception:
*** Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'
However I am not modifying the object, just accessing it. Do I have to start a write transaction to check equality?
If I put a breakpoint right at the start of the if statement, the next step in throws the exception.
//message is a RLMObject stored in a RLMResults array
if (message.status == 3 || message.status == 4) {
NSLog(#"Message status: %ld", (long)message.status);
}
The issue was that I was querying and modifying the RLMObject elsewhere in the setup of another view controller. This was causing the exception to be thrown. My fault for not looking closely at what my other code was doing.

Assertion failure in -[NSIndexPath row] with AppList dataSource

See update below.
I'm writing a tweak and am using AppList. I am hooking into spotlight and creating a table off all installed applications. I am trying to use the dataSource to get the information (display identifier etc). The problem is if dataSource is defined... I get the following error:
SpringBoard[1622] <Warning>: *** Assertion failure in -[NSIndexPath row], /SourceCache/UIKit/UIKit-2380.17/UITableViewSupport.m:2680
SpringBoard[1622] <Warning>: ***** AltKeyboard Uncaught Exception: Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible. *****
or
SpringBoard[1890] <Warning>: ***** AltKeyboard Uncaught Exception: *** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1] *****
SpringBoard[1890] <Error>: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
If dataSource is not defined and I use fake data (test name), the table is generated fine. When I print the dataSource there are only two items in it.
Ryan Petrich, on IRC, seemed to suggest that I was doing everything completely wrong but wasn't much help after that. Perhaps I am going about solving this problem incorrectly?
Code: http://pastebin.ca/2457626
Repository: https://github.com/twodayslate/ListLauncher
edit/update!
dataSource is giving me problems. It isn't fetching the same number of apps and thus I am getting index errors and the like. I have over 200 apps but dataSource is only fetching 2.
The following code gives no errors but only displays 2 items: http://pastebin.ca/2458961
edit!
I changed it so I just use ALApplicationList. I am able to get all the applications listed. However, when I attempt to scroll through the table, it crashes. Sorting and creating the list is really slow so I believe that might be causing the problem. I'm no pro at obj-c so I'm sure there is a better way than what I am doing. http://pastebin.ca/2459318
Your problem is twofold. First, UITableView expects to be handed index paths containing both a row and a section, where you've provided it with only a row. Second, you cannot expect unchecked array access to succeed 100% of the time, so bounds check whatever access calls you might be using. It also wouldn't hurt to use some retains in the iVar assignments you've got in your code unless you don't need to hold a reference to it (but it looks like you do).
I figured out an alternative solution to ALDataSource. I sorted ALApplicationList.applications. The problem was that the instance of the sorted NSArray would keep disappearing. So I did [values retain] and everything worked! Reinitializing the sorted values was too slow and would crash the device.
Here is the working code that successfully lists all the apps: http://pastebin.ca/2459778

Zero object in array crashes the program, also after certain count

I have a program that grabs data from Instagram, puts it into dictionary and then I parse it.
Problems begin when I try to use that data, magically item number 0 crashes my program.
Also, after certain number next item also crashes the program.
Here is the code for UITableViewCell, that grabs text from the array/dictionaries and puts it into cell. As it looks, it crashes, if I add another check to start with number 1, it works, but crashes later when it reaches some count.
I don't understand why this happens, since I have the method that tells the TableView how many rows are in table, and it returns [self.loader.dataArray count], so it can't possibly try to load the thing that is out of bounds.
if (self.loader.dataArray[indexPath.row] != nil )
{
cell.textLabel.text = [[[self.loader.dataArray[indexPath.row] objectForKey:#"caption"] objectForKey:#"from"] objectForKey:#"full_name"];
}
Here is the error I am getting:
instagramClient[8254:907] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKey:]: unrecognized selector sent to instance 0x3bb8f090'
The exception that you're getting does not indicate an out of bounds issue, it indicates that you have got an NSNull in your dataArray (or possibly in the "caption" object of one of the dictionaries in your dataArray or in the "from" object of one of those dictionaries, and so on). Try logging all of self.loader.dataArray when you get updates to that array and see if it contains any NSNull objects.

Crash with "Collection ... mutated while being enumerated"

I got this error after a crash:
malloc: *** error for object 0x75617b4: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
2013-02-05 19:15:44.950 BusinessIdea[3171:15e03] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSSetM: 0x72dc5c0> was mutated while being enumerated.'
I am not familiar with this error. Any idea what it could be?
The whole 'modified while being enumerated' error means you are probably trying to delete something from a set while iterating over it. For example
for(NSString *str in array){
if([str isequalToString:#"delete me"]){
[array removeObject:str]; //this will cause a problem,
}
}
If you want to use the fast enumeration loop, you need to build a list of items you want to remove, and then delete them after the iteration step. Alternatives are to use a traditional indexed for loop if you want to remove items in the loop body.

Saving another type to an attribute of an entity to core data?

cell.textLabel.text = operationEnt.operationNaam;
on this line of code I get the error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString operationNaam]: unrecognized selector sent to instance 0x6bce400'
But operationNaam is an attribute defined as a String. So I'm wondering, where is my mistake.
This is when I try to fill my cells of my TableView.
That exception means that the message operationNaam is being sent to a NSString object, and NSString does not recognize that message selector.
This means that your operationEnt object has somehow become a NSString during runtime.
Possibly, you did this...
operationEnt = someString;
when you meant this...
operationEnt.operationNaam = someString;
It seems that you either have memory management issues or misunderstand Objective-C's property accessor syntax.o operationEnt keeps becoming an NSString which doesn't respond to that particular property getter message, hence the crash. (Watch out for overreleases!)

Resources