【问题标题】:Why xcode and RestKit crash from NSURL nil string parameter为什么 xcode 和 RestKit 从 NSURL nil 字符串参数崩溃
【发布时间】:2013-06-05 15:21:32
【问题描述】:

我是 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]));
    }
}

我无法弄清楚,因为我从我创建的功能版本中克隆了代码,但它仍然崩溃。 任何帮助都会很大——即使它只是抽象地解释这意味着什么以及如何解决这个问题。 提前致谢!

这是我调用 NSURL 的 AppDelegate.m 文件(以及异常断点指向我的位置)。

#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

【问题讨论】:

  • 首先启用异常断点。这将告诉您错误的真实位置。如果你不知道那是什么,那么搜索堆栈溢出或谷歌,有很多关于它的信息。
  • 您需要显示调用 [NSURL initFileURLWithPath:] 的代码,以及如何获取作为参数传入的值。
  • 感谢@borrrden - 我启用了断点,它指向了我在上面添加的 appdelegate.m 文件。有什么想法吗?
  • @rdelmar 我已经附上了我调用 NSURL 的文件中的代码 - 你怎么看?谢谢您的帮助。我真的很感激。
  • [[NSBundle mainBundle] pathForResource:@"Marko1" ofType:@"momd"] 可能是nil

标签: iphone ios xcode restkit nsurl


【解决方案1】:

我见过这种崩溃,是因为在这一行中路径返回 nil

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

1.记录你的路径

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

2.insted of momd 试试mom

【讨论】:

  • 我试过这个,但我仍然遇到同样的错误和崩溃。奇怪的是,在教程中 - 代码适用于返回 nil 的路径 - 所以我认为问题出在其他地方。感谢您抽出宝贵时间提供帮助!
【解决方案2】:

您是否尝试检查该文件是否存在?

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]];

【讨论】:

  • 我认为你是对的 - 运行测试后我得到“找不到文件”。这是否意味着“pathForResource”是错误的?或者我需要创建一个文件 - 但是是什么类型的,在哪里?
  • 不,这没有错,但您的文件不存在,并且 -pathForResource:ofType: 中的非源文件的返回值为“nil”。尝试在此线程中标记的答案:stackoverflow.com/questions/3105891/…。希望这可以帮助。 p.s.是的,您必须先创建文件。有关如何操作,请查看帖子(如果已发布)。
猜你喜欢
  • 2011-03-14
  • 2014-04-28
  • 1970-01-01
  • 2016-09-20
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 2015-09-08
相关资源
最近更新 更多