这是我的代码:
·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 receivedData:string];这可能是个问题吗?
当我运行AppDelegate * theAppDelegate =(AppDelegate *)[[NSApplication sharedApplication] delegate];
我收到此错误消息:
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)
答案 0 :(得分:2)
在评论中,你说:
AppDelegate *theAppDelegate = [[AppDelegate alloc] init];
[theAppDelegate receivedData:string];
这会创建一个 new AppDelegate
对象,这意味着未调用applicationDidFinishLaunching
方法(它已在第一个AppDelegate
实例中调用) 。请改用原来的。
AppDelegate *theAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];