我是xcode的新手并且遵循这个RestKit教程: https://github.com/RestKit/RKGist/blob/master/TUTORIAL.md 我成功地完成了它。我正在重新审视并定制它以满足我的需求,并且由于某种原因,在很早的时候(甚至在我做出任何真正的偏差之前)应用程序在模拟器上崩溃了。 我设置了数据模型,配置了RestKit和CoreData,然后在tut中进行了第一次模拟失败。 这是我尝试运行时遇到的错误:
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
以上,它显示main.m文件中的这行代码:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
我无法理解,因为我从我创建的功能版本克隆了代码,但它仍然崩溃。 任何帮助都会很大 - 即使它只是解释这意味着什么是抽象的以及如何处理攻击这个问题。 提前谢谢!
这是AppDelegate.m文件,我调用NSURL(以及异常断点指向我的地方)。
#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
答案 0 :(得分:0)
我看到过这种崩溃,原因是路径返回nil
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Marko1" ofType:@"momd"]];
1.记录你的路径
NSLog(@"%@",[[NSBundle mainBundle] pathForResource:@"Marko1" ofType:@"momd"]);
2. momd
尝试使用mom
答案 1 :(得分:0)
您是否尝试检查该文件是否存在?
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");
}
或尝试以这种方式使用它:
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"Makro1" ofType:@"momd"];
NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];