0

これが私のコードです:

.h

NSMutableString *buffer, *tmpBuffer;
int status; // 0=waiting for <DMX>, 1=recording

.m

 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    buffer = [[NSMutableString alloc] init];
    tmpBuffer = [[NSMutableString alloc] init];
    status = 0;
}

- (void) receivedData:(NSString *)data {

        // status=0 means we are still looking for start tag
        if(status == 0) {
            // add new data to last examined chunk (if any)
            [tmpBuffer appendString:data];

            // try to locate the open tag inside the tmpBuffer
            NSRange range = [tmpBuffer rangeOfString:@"<DMX>" options:NSCaseInsensitiveSearch];

            // if found, store the portion after the start tag into buffer
            if(range.location != NSNotFound) {
                range.length = [tmpBuffer length] - range.location + 5; // 5 is length of start tag...
                [buffer setString:[tmpBuffer substringWithRange:range]];
                status = 1; // set status to 1 so we know recording started
            } else {
                // store last examined chunk
                [tmpBuffer setString:data];
            }
        } else {
            [buffer appendString:data];
            NSRange range = [buffer rangeOfString:@"</DMX>" options:NSCaseInsensitiveSearch];
            if(range.location != NSNotFound) {
                range.length = [buffer length] - range.location;

                [self fullDMXReceived:buffer];

                [buffer deleteCharactersInRange:range];
                status = 0;
            }
        }
    }

    - (void) fullDMXReceived:(NSString*)finalData {
        NSRunAlertPanel(@"", finalData, @"", @"", @"");
    }

エラーは次のとおりです。

2012-12-02 14:39:00.356 Chatty Mac[1805:303] *** Assertion failure in -[NSTextFieldCell _objectValue:forString:errorDescription:], /SourceCache/AppKit/AppKit-1187.34/AppKit.subproj/NSCell.m:1532
2012-12-02 14:39:00.362 Chatty Mac[1805:303] An uncaught exception was raised
2012-12-02 14:39:00.367 Chatty Mac[1805:303] Invalid parameter not satisfying: aString != nil
2012-12-02 14:39:00.374 Chatty Mac[1805:303] (
    0   CoreFoundation                      0x00007fff93e350a6 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff8f52e3f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff93e34ee8 +[NSException raise:format:arguments:] + 104
    3   Foundation                          0x00007fff933fe6a2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189
    4   AppKit                              0x00007fff92660205 -[NSCell _objectValue:forString:errorDescription:] + 159
    5   AppKit                              0x00007fff9266015f -[NSCell _objectValue:forString:] + 20
    6   AppKit                              0x00007fff926600db -[NSCell setStringValue:] + 39
    7   AppKit                              0x00007fff926f43fc -[NSControl setStringValue:] + 138
    8   AppKit                              0x00007fff928c2ba8 -[NSAlert buildAlertStyle:title:formattedMsg:first:second:third:oldStyle:] + 7479
    9   AppKit                              0x00007fff928c478b _NXDoLocalRunAlertPanel + 343
    10  AppKit                              0x00007fff928c461c NSRunAlertPanel + 157
    11  Chatty Mac                          0x00000001000070bd -[AppDelegate fullDMXReceived:] + 61
    12  Chatty Mac                          0x0000000100007036 -[AppDelegate receivedData:] + 582
    13  Chatty Mac                          0x000000010000e751 -[ORSSerialPortDemoController serialPort:didReceiveData:] + 401
    14  Chatty Mac                          0x000000010000a6fa -[ORSSerialPort receiveData:] + 170
    15  Chatty Mac                          0x0000000100009676 __block_global_1 + 38
    16  libdispatch.dylib                   0x00007fff9637bf01 _dispatch_call_block_and_release + 15
    17  libdispatch.dylib                   0x00007fff963780b6 _dispatch_client_callout + 8
    18  libdispatch.dylib                   0x00007fff9637d0c8 _dispatch_main_queue_callback_4CF + 275
    19  CoreFoundation                      0x00007fff93dd70fe __CFRunLoopRun + 1614
    20  CoreFoundation                      0x00007fff93dd66b2 CFRunLoopRunSpecific + 290
    21  HIToolbox                           0x00007fff949130a4 RunCurrentEventLoopInMode + 209
    22  HIToolbox                           0x00007fff94912e42 ReceiveNextEventCommon + 356
    23  HIToolbox                           0x00007fff94912cd3 BlockUntilNextEventMatchingListInMode + 62
    24  AppKit                              0x00007fff92682613 _DPSNextEvent + 685
    25  AppKit                              0x00007fff92681ed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
    26  AppKit                              0x00007fff92679283 -[NSApplication run] + 517
    27  AppKit                              0x00007fff9261dcb6 NSApplicationMain + 869
    28  Chatty Mac                          0x00000001000056d2 main + 34
    29  Chatty Mac                          0x0000000100001da4 start + 52
    30  ???                                 0x0000000000000003 0x0 + 3
)
2012-12-02 14:39:00.475 Chatty Mac[1805:303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: aString != nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff93e350a6 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff8f52e3f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff93e34ee8 +[NSException raise:format:arguments:] + 104
    3   Foundation                          0x00007fff933fe6a2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189
    4   AppKit                              0x00007fff92660205 -[NSCell _objectValue:forString:errorDescription:] + 159
    5   AppKit                              0x00007fff9266015f -[NSCell _objectValue:forString:] + 20
    6   AppKit                              0x00007fff926600db -[NSCell setStringValue:] + 39
    7   AppKit                              0x00007fff926f43fc -[NSControl setStringValue:] + 138
    8   AppKit                              0x00007fff928c2ba8 -[NSAlert buildAlertStyle:title:formattedMsg:first:second:third:oldStyle:] + 7479
    9   AppKit                              0x00007fff928c478b _NXDoLocalRunAlertPanel + 343
    10  AppKit                              0x00007fff928c461c NSRunAlertPanel + 157
    11  Chatty Mac                          0x00000001000070bd -[AppDelegate fullDMXReceived:] + 61
    12  Chatty Mac                          0x0000000100007036 -[AppDelegate receivedData:] + 582
    13  Chatty Mac                          0x000000010000e751 -[ORSSerialPortDemoController serialPort:didReceiveData:] + 401
    14  Chatty Mac                          0x000000010000a6fa -[ORSSerialPort receiveData:] + 170
    15  Chatty Mac                          0x0000000100009676 __block_global_1 + 38
    16  libdispatch.dylib                   0x00007fff9637bf01 _dispatch_call_block_and_release + 15
    17  libdispatch.dylib                   0x00007fff963780b6 _dispatch_client_callout + 8
    18  libdispatch.dylib                   0x00007fff9637d0c8 _dispatch_main_queue_callback_4CF + 275
    19  CoreFoundation                      0x00007fff93dd70fe __CFRunLoopRun + 1614
    20  CoreFoundation                      0x00007fff93dd66b2 CFRunLoopRunSpecific + 290
    21  HIToolbox                           0x00007fff949130a4 RunCurrentEventLoopInMode + 209
    22  HIToolbox                           0x00007fff94912e42 ReceiveNextEventCommon + 356
    23  HIToolbox                           0x00007fff94912cd3 BlockUntilNextEventMatchingListInMode + 62
    24  AppKit                              0x00007fff92682613 _DPSNextEvent + 685
    25  AppKit                              0x00007fff92681ed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
    26  AppKit                              0x00007fff92679283 -[NSApplication run] + 517
    27  AppKit                              0x00007fff9261dcb6 NSApplicationMain + 869
    28  Chatty Mac                          0x00000001000056d2 main + 34
    29  Chatty Mac                          0x0000000100001da4 start + 52
    30  ???                                 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

[self fullDMXReceived:buffer];で発生しています。削除してもクラッシュしないため、と呼ばれます。

ここで何が起こっているのですか?誰かがこのエラーをデコードできますか?

参考までに、次のような別のクラスからreceivedDataを呼び出しています。AppDelegate* theAppDelegate = [[AppDelegate alloc] init]; [theAppDelegate receiveData:string]; これは問題になる可能性がありますか?

AppDelegateを実行すると*theAppDelegate=(AppDelegate *)[[NSApplicationsharedApplication]デリゲート];

このエラーメッセージが表示されます:

2012-12-03 06:00:12.791 Chatty Mac[1580:303] An uncaught exception was raised
2012-12-03 06:00:12.793 Chatty Mac[1580:303] -[__NSCFString substringWithRange:]: Range or index out of bounds
2012-12-03 06:00:12.797 Chatty Mac[1580:303] (
0   CoreFoundation                      0x00007fff8d0ee0a6 __exceptionPreprocess + 198
1   libobjc.A.dylib                     0x00007fff887e73f0 objc_exception_throw + 43
2   CoreFoundation                      0x00007fff8d0ede7c +[NSException raise:format:] + 204
3   CoreFoundation                      0x00007fff8d0c555d -[__NSCFString substringWithRange:] + 109
4   Chatty Mac                          0x0000000100006ecb -[AppDelegate receivedData:] + 283
5   Chatty Mac                          0x000000010000e772 -[ORSSerialPortDemoController serialPort:didReceiveData:] + 466
6   Chatty Mac                          0x000000010000a6ba -[ORSSerialPort receiveData:] + 170
7   Chatty Mac                          0x0000000100009636 __block_global_1 + 38
8   libdispatch.dylib                   0x00007fff8f634f01 _dispatch_call_block_and_release + 15
9   libdispatch.dylib                   0x00007fff8f6310b6 _dispatch_client_callout + 8
10  libdispatch.dylib                   0x00007fff8f6360c8 _dispatch_main_queue_callback_4CF + 275
11  CoreFoundation                      0x00007fff8d0900fe __CFRunLoopRun + 1614
12  CoreFoundation                      0x00007fff8d08f6b2 CFRunLoopRunSpecific + 290
13  HIToolbox                           0x00007fff8dbcc0a4 RunCurrentEventLoopInMode + 209
14  HIToolbox                           0x00007fff8dbcbe42 ReceiveNextEventCommon + 356
15  HIToolbox                           0x00007fff8dbcbcd3 BlockUntilNextEventMatchingListInMode + 62
16  AppKit                              0x00007fff8b93b613 _DPSNextEvent + 685
17  AppKit                              0x00007fff8b93aed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
18  AppKit                              0x00007fff8b932283 -[NSApplication run] + 517
19  AppKit                              0x00007fff8b8d6cb6 NSApplicationMain + 869
20  Chatty Mac                          0x0000000100005692 main + 34
21  Chatty Mac                          0x0000000100001d64 start + 52
22  ???                                 0x0000000000000003 0x0 + 3
)
2012-12-03 06:00:12.799 Chatty Mac[1580:303] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range or index out of bounds'
*** First throw call stack:
(
0   CoreFoundation                      0x00007fff8d0ee0a6 __exceptionPreprocess + 198
1   libobjc.A.dylib                     0x00007fff887e73f0 objc_exception_throw + 43
2   CoreFoundation                      0x00007fff8d0ede7c +[NSException raise:format:] + 204
3   CoreFoundation                      0x00007fff8d0c555d -[__NSCFString substringWithRange:] + 109
4   Chatty Mac                          0x0000000100006ecb -[AppDelegate receivedData:] + 283
5   Chatty Mac                          0x000000010000e772 -[ORSSerialPortDemoController serialPort:didReceiveData:] + 466
6   Chatty Mac                          0x000000010000a6ba -[ORSSerialPort receiveData:] + 170
7   Chatty Mac                          0x0000000100009636 __block_global_1 + 38
8   libdispatch.dylib                   0x00007fff8f634f01 _dispatch_call_block_and_release + 15
9   libdispatch.dylib                   0x00007fff8f6310b6 _dispatch_client_callout + 8
10  libdispatch.dylib                   0x00007fff8f6360c8 _dispatch_main_queue_callback_4CF + 275
11  CoreFoundation                      0x00007fff8d0900fe __CFRunLoopRun + 1614
12  CoreFoundation                      0x00007fff8d08f6b2 CFRunLoopRunSpecific + 290
13  HIToolbox                           0x00007fff8dbcc0a4 RunCurrentEventLoopInMode + 209
14  HIToolbox                           0x00007fff8dbcbe42 ReceiveNextEventCommon + 356
15  HIToolbox                           0x00007fff8dbcbcd3 BlockUntilNextEventMatchingListInMode + 62
16  AppKit                              0x00007fff8b93b613 _DPSNextEvent + 685
17  AppKit                              0x00007fff8b93aed2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
18  AppKit                              0x00007fff8b932283 -[NSApplication run] + 517
19  AppKit                              0x00007fff8b8d6cb6 NSApplicationMain + 869
20  Chatty Mac                          0x0000000100005692 main + 34
21  Chatty Mac                          0x0000000100001d64 start + 52
22  ???                                 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminate called throwing an exception
(lldb) 
4

1 に答える 1

2

コメントで、あなたは言います:

AppDelegate *theAppDelegate = [[AppDelegate alloc] init];
[theAppDelegate receivedData:string];

これにより、新しい AppDelegateオブジェクトが作成されます。これは、applicationDidFinishLaunchingメソッドが呼び出されていないことを意味します(最初のインスタンスですでに呼び出されていAppDelegateます)。代わりに元のものを使用してください。

AppDelegate *theAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
于 2012-12-03T01:56:49.053 に答える