objc_msg发送崩溃Objective-C

时间:2019-06-10 14:10:44

标签: objective-c macos crash appkit

我收到了实时用户的崩溃报告 我有一个具有nsstring的NSObject

@interface MountedVolume : NSObject
@property (nonatomic, strong) NSString *name;
@end

视图控制器具有文本字段,通知时其值会更改 收到。

@interface ViewController : NSViewController {

      MountedVolume *selectedVolume;

    __weak IBOutlet NSTextField *txtVolumeName;

}
@end

@implementation ViewController

- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserverForName:DCNotificationNameVolumesUpdated object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        dispatch_async(dispatch_get_main_queue(), ^{

//crash report says in this line it got crashed

            txtVolumeName.stringValue = selectedVolume.name;

        });
    }];


}

@end

以下是崩溃报告中有关如何处理此问题的任何建议?

崩溃报告

Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x7fff6ac2639d objc_msgSend + 29
1  AppKit                         0x7fff3db3d868 -[NSCell _objectValue:forString:errorDescription:] + 157
2  AppKit                         0x7fff3db3d71d -[NSCell setStringValue:] + 40
3  AppKit                         0x7fff3db95044 -[NSControl setStringValue:] + 135
4  MyApp               0x104de865b __40-[ViewController viewDidLoad]_block_invoke_3 (ViewController.m:172)
5  libdispatch.dylib              0x7fff6c3ab5f8 _dispatch_call_block_and_release + 12
6  libdispatch.dylib              0x7fff6c3ac63d _dispatch_client_callout + 8
7  libdispatch.dylib              0x7fff6c3b768d _dispatch_main_queue_callback_4CF + 1135
8  CoreFoundation                 0x7fff40435f56 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
9  CoreFoundation                 0x7fff40435683 __CFRunLoopRun + 2300
10 CoreFoundation                 0x7fff40434b35 CFRunLoopRunSpecific + 459
11 HIToolbox                      0x7fff3f71396b RunCurrentEventLoopInMode + 292
12 HIToolbox                      0x7fff3f7136a5 ReceiveNextEventCommon + 603
13 HIToolbox                      0x7fff3f713436 _BlockUntilNextEventMatchingListInModeWithFilter + 64
14 AppKit                         0x7fff3daad987 _DPSNextEvent + 965
15 AppKit                         0x7fff3daac71f -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1361
16 AppKit                         0x7fff3daa683c -[NSApplication run] + 699
17 AppKit                         0x7fff3da95d7c NSApplicationMain + 777
18 libdyld.dylib                  0x7fff6c3f93d5 start + 1

1 个答案:

答案 0 :(得分:0)

enter image description here查看文档,stringValue似乎不是可选的。您没有在viewDidLoad之前初始化selectedVolume(据我所知),这意味着如果在通知selectedVolume被赋值之前运行该通知块,您可能会尝试将此值设置为nil。我将从首先尝试将txtVolumeName.stringValue设置为硬编码值开始,然后查看它是否完全停止崩溃。如果是这样,则在调用setter之前,可能需要防止nil值。

例如

NSString *string;
NSTextField *textField = [[NSTextField alloc]init];
if (string) {
    textField.stringValue = string;
}