0

I am new to xcode and following this RestKit tutorial: https://github.com/RestKit/RKGist/blob/master/TUTORIAL.md I have gone through it once succesfully. I am going through it again and customizing it to fit my needs and for some reason, very early on (even before I've made any real deviations) the app crashes on the simulator. I set up the data model, configured RestKit and CoreData, and then failed the first simulation in the tut. This is the error I get when I try to run it:

    2013-06-09 18:37:10.121 marko1[16183:c07] I restkit:RKLog.m:34 RestKit logging initialized...
2013-06-09 18:37:10.148 marko1[16183:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** First throw call stack:
(0x1b24012 0x1949e7e 0x1b23deb 0x13609b1 0x136093b 0x26db 0x88b157 0x88b747 0x88c94b 0x89dcb5 0x89ebeb 0x890698 0x25c0df9 0x25c0ad0 0x1a99bf5 0x1a99962 0x1acabb6 0x1ac9f44 0x1ac9e1b 0x88c17a 0x88dffc 0x25dd 0x2505 0x1)
libc++abi.dylib: terminate called throwing an exception

And above, it displays this line of code from the main.m file:

 #import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

I can't figure it out because I cloned the code from a functional version I created and it still crashes. Any help would be greatly - even if its just explaining what this means abstractly and how to approach attacking this problem. Thanks in advance!

Here is the AppDelegate.m file where I call NSURL (and where the exception breakpoints direct me).

#import "AppDelegate.h"
#import <RestKit/RestKit.h>
#import "MasterViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSError *error = nil;
    NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Marko1" ofType:@"momd"]];

    NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

    //Initialize the Core Data Stack
    [managedObjectStore createPersistentStoreCoordinator];

    NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
    NSAssert(persistentStore, @"Failed to add persistent store: %@", error);

    [managedObjectStore createManagedObjectContexts];

    //Set the default store shared instance
    [RKManagedObjectStore setDefaultStore:managedObjectStore];

    //Override point for customization after application launch.
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
    controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
    return YES;
}
@end
Marco
  • 6,692
  • 2
  • 27
  • 38
  • First enable exception breakpoints. That will tell you the true location of the error. If you don't know what that is, then search stack overflow or google for it, there is tons of info about it. – borrrden Jun 10 '13 at 01:54
  • You need to show the code where you call [NSURL initFileURLWithPath:], and how you get the value that you pass in as the argument. – rdelmar Jun 10 '13 at 01:55
  • Thanks @borrrden - I enabled the breakpoints and it pointed me to the appdelegate.m file, which I've added above. Any ideas? – grantvansant Jun 10 '13 at 04:32
  • @rdelmar I have attached the code from the file where I call NSURL - what do you think? Thanks for the help. I really appreciate it. – grantvansant Jun 10 '13 at 04:34
  • `[[NSBundle mainBundle] pathForResource:@"Marko1" ofType:@"momd"]` is probably `nil` – borrrden Jun 10 '13 at 05:00

2 Answers2

0

I have seen this kind of crash and it was because the path returned nil in this line

NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Marko1" ofType:@"momd"]];

1.Log your path

 NSLog(@"%@",[[NSBundle mainBundle] pathForResource:@"Marko1" ofType:@"momd"]);

2.insted of momd try with mom

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • I tried this but I still got the same error and crash. The odd thing is, in the tutorial- the code works with that path returning nil- so I think the problem is elsewhere. Thanks for taking the time to help! – grantvansant Jun 10 '13 at 14:34
0

Have you tried to check if the file exists?

NSURL *test;
if ([[NSBundle mainBundle] pathForResource:@"Marko1" ofType:@"momd"]) {
    test = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Marko1" ofType:@"momd"]];
} else {
    NSLog(@"File could not be located");
}

or try using it this way:

NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"Makro1" ofType:@"momd"];
NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
zero3nna
  • 2,770
  • 30
  • 28
  • I think you are right - after running your test I got 'File could not be located'. Does that mean the 'pathForResource' is wrong? Or that I need to create a file - but what kind and where? – grantvansant Jun 10 '13 at 14:30
  • no it's not wrong but your file dosn't exists and the return value for a not fount file in -pathForResource:ofType: is "nil". Try the marked answer in this thread: http://stackoverflow.com/questions/3105891/iphone-pathforresource-vs-bundlepath. hope this helps. p.s. yes you have to create the file first. for a how to, look at the thread if posted. – zero3nna Jun 27 '13 at 13:34