[NSString lastPathComponent];崩溃的应用程序

时间:2015-05-13 03:16:37

标签: objective-c macos cocoa nsstring runtime-error

我的应用程序从NSOpenPanel获取路径并从路径获取nsdata对象,并从路径获取nsstring对象。然后我去调用[filePath lastPathComponent]; (filePath是路径中的NSString对象)。它崩溃我的应用程序并给我错误:

2015-05-12 20:11:46.022 myApp[409:303] -[__NSCFData lastPathComponent]: unrecognized selector sent to instance 0x100528920
2015-05-12 20:11:46.022 myApp[409:303] An uncaught exception was raised
2015-05-12 20:11:46.022 myApp[409:303] -[__NSCFData lastPathComponent]: unrecognized selector sent to instance 0x100528920
2015-05-12 20:11:46.028 myApp[409:303] (
    0   CoreFoundation                      0x00007fff8d35ff56 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff93119d5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8d3ec1be -[NSObject doesNotRecognizeSelector:] + 190
    3   CoreFoundation                      0x00007fff8d34ce23 ___forwarding___ + 371
    4   CoreFoundation                      0x00007fff8d34cc38 _CF_forwarding_prep_0 + 232
    5   myApp                            0x00000001000030cc -[MPOMainWindowController tableView:objectValueForTableColumn:row:] + 140
    6   AppKit                              0x00007fff8aa7d6e7 -[NSTableView _dataSourceValueForColumn:row:] + 73
    7   AppKit                              0x00007fff8aa7d167 -[NSTableView preparedCellAtColumn:row:] + 437
    8   AppKit                              0x00007fff8aa8e171 -[NSTableView _drawContentsAtRow:column:withCellFrame:] + 42
    9   AppKit                              0x00007fff8aa8c5d8 -[NSTableView drawRow:clipRect:] + 1647
    10  AppKit                              0x00007fff8aa8bdb5 -[NSTableView drawRowIndexes:clipRect:] + 565
    11  AppKit                              0x00007fff8aa89ed7 -[NSTableView drawRect:] + 1390
    12  AppKit                              0x00007fff8a9f22ce -[NSView _drawRect:clip:] + 3758
    13  AppKit                              0x00007fff8aa1f49b -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1583
    14  AppKit                              0x00007fff8aa1f8c7 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2651
    15  AppKit                              0x00007fff8aa1f8c7 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2651
    16  AppKit                              0x00007fff8aa1f8c7 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2651
    17  AppKit                              0x00007fff8aa1f8c7 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2651
    18  AppKit                              0x00007fff8a9ef8a9 -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 1032
    19  AppKit                              0x00007fff8a9eeda3 -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 270
    20  AppKit                              0x00007fff8a9ea1bb -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 4755
    21  AppKit                              0x00007fff8a9e2c35 -[NSView displayIfNeeded] + 1676
    22  AppKit                              0x00007fff8aaa02fb -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1306
    23  AppKit                              0x00007fff8aa9fcf8 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 807
    24  AppKit                              0x00007fff8aa9f97c -[NSWindow orderWindow:relativeTo:] + 154
    25  AppKit                              0x00007fff8a9b847e -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1556
    26  AppKit                              0x00007fff8a9ae8bb loadNib + 322
    27  AppKit                              0x00007fff8a9addb8 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
    28  AppKit                              0x00007fff8a9adcd3 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
    29  AppKit                              0x00007fff8a9adc16 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
    30  AppKit                              0x00007fff8ac1ecd7 NSApplicationMain + 398
    31  myApp                            0x0000000100001412 main + 34
    32  myApp                            0x00000001000013e4 start + 52
    33  ???                                 0x0000000000000003 0x0 + 3
)
2015-05-12 20:11:46.029 myApp[409:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData lastPathComponent]: unrecognized selector sent to instance 0x100528920'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff8d35ff56 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff93119d5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8d3ec1be -[NSObject doesNotRecognizeSelector:] + 190
    3   CoreFoundation                      0x00007fff8d34ce23 ___forwarding___ + 371
    4   CoreFoundation                      0x00007fff8d34cc38 _CF_forwarding_prep_0 + 232
    5   myApp                            0x00000001000030cc -[MPOMainWindowController tableView:objectValueForTableColumn:row:] + 140
    6   AppKit                              0x00007fff8aa7d6e7 -[NSTableView _dataSourceValueForColumn:row:] + 73
    7   AppKit                              0x00007fff8aa7d167 -[NSTableView preparedCellAtColumn:row:] + 437
    8   AppKit                              0x00007fff8aa8e171 -[NSTableView _drawContentsAtRow:column:withCellFrame:] + 42
    9   AppKit                              0x00007fff8aa8c5d8 -[NSTableView drawRow:clipRect:] + 1647
    10  AppKit                              0x00007fff8aa8bdb5 -[NSTableView drawRowIndexes:clipRect:] + 565
    11  AppKit                              0x00007fff8aa89ed7 -[NSTableView drawRect:] + 1390
    12  AppKit                              0x00007fff8a9f22ce -[NSView _drawRect:clip:] + 3758
    13  AppKit                              0x00007fff8aa1f49b -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1583
    14  AppKit                              0x00007fff8aa1f8c7 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2651
    15  AppKit                              0x00007fff8aa1f8c7 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2651
    16  AppKit                              0x00007fff8aa1f8c7 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2651
    17  AppKit                              0x00007fff8aa1f8c7 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2651
    18  AppKit                              0x00007fff8a9ef8a9 -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 1032
    19  AppKit                              0x00007fff8a9eeda3 -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 270
    20  AppKit                              0x00007fff8a9ea1bb -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 4755
    21  AppKit                              0x00007fff8a9e2c35 -[NSView displayIfNeeded] + 1676
    22  AppKit                              0x00007fff8aaa02fb -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1306
    23  AppKit                              0x00007fff8aa9fcf8 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 807
    24  AppKit                              0x00007fff8aa9f97c -[NSWindow orderWindow:relativeTo:] + 154
    25  AppKit                              0x00007fff8a9b847e -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1556
    26  AppKit                              0x00007fff8a9ae8bb loadNib + 322
    27  AppKit                              0x00007fff8a9addb8 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217
    28  AppKit                              0x00007fff8a9adcd3 +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141
    29  AppKit                              0x00007fff8a9adc16 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
    30  AppKit                              0x00007fff8ac1ecd7 NSApplicationMain + 398
    31  myApp                            0x0000000100001412 main + 34
    32  myApp                            0x00000001000013e4 start + 52
    33  ???                                 0x0000000000000003 0x0 + 3
)
terminate called throwing an exception(lldb) 

我不明白为什么这不起作用,因为它在我工作的时候大约工作了2个小时。有人有什么建议吗?

编辑:这是我的代码:

NSOpenPanel *panel = [NSOpenPanel openPanel];

    // display the panel
    [panel beginWithCompletionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {
            [[webView mainFrame] loadRequest:nil];
            // grab a reference to what has been selected
            NSURL *fileURL = [[panel URLs]objectAtIndex:0];

            // write our file name to a label
            NSString *filePath = [NSString stringWithFormat:@"%@", fileURL];

NSString *fileName = [filePath lastPathComponent]; // <-- Error is here I think

1 个答案:

答案 0 :(得分:1)

更改此行:

NSString *filePath = [NSString stringWithFormat:@"%@", fileURL];

到这一行:

NSString *filePath = [fileURL path];