2011-01-14 48 views
-2

我想提出一個IDE的東西...語法高亮凍結,當我做這樣的事情:(Obj-C)這種語法突出顯示有什麼問題?


int i = abs(); 
//then push " like so: 
int i = abs("); 
//when I push anything after that it gives me an error and deletes it: 
/* 
ERROR: 
2011-01-14 11:53:29.554 PROJECT NAME GOES HERE[6767:a0f] HIToolbox: ignoring exception '*** -[NSBigMutableString substringWithRange:]: Range or index out of bounds' that raised inside Carbon event dispatch 
(
    0 CoreFoundation      0x00007fff800b57b4 __exceptionPreprocess + 180 
    1 libobjc.A.dylib      0x00007fff83aed0f3 objc_exception_throw + 45 
    2 CoreFoundation      0x00007fff800b55d7 +[NSException raise:format:arguments:] + 103 
    3 CoreFoundation      0x00007fff800b5564 +[NSException raise:format:] + 148 
    4 Foundation       0x00007fff87e0ecf2 -[NSString substringWithRange:] + 204 
    5 PROJECT NAME GOES HERE    0x00000001000014bb -[ScriptController textStorageDidProcessEditing:] + 741 
    6 Foundation       0x00007fff87deba66 _nsnote_callback + 167 
    7 CoreFoundation      0x00007fff8005d000 __CFXNotificationPost + 1008 
    8 CoreFoundation      0x00007fff80049578 _CFXNotificationPostNotification + 200 
    9 Foundation       0x00007fff87de29ce -[NSNotificationCenter postNotificationName:object:userInfo:] + 101 
    10 AppKit        0x00007fff8577cef6 -[NSTextStorage processEditing] + 137 
    11 AppKit        0x00007fff8577d9e3 -[NSTextStorage endEditing] + 80 
    12 AppKit        0x00007fff857a5b56 -[NSTextView insertText:replacementRange:] + 2745 
    13 AppKit        0x00007fff857a3471 -[NSTextInputContext handleTSMEvent:] + 2204 
    14 AppKit        0x00007fff857a2bc6 _NSTSMEventHandler + 156 
    15 HIToolbox       0x00007fff84d6d9d5 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1002 
    16 HIToolbox       0x00007fff84d6cf28 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 395 
    17 HIToolbox       0x00007fff84d8abeb SendEventToEventTarget + 45 
    18 HIToolbox       0x00007fff84dbc2bb SendTSMEvent + 48 
    19 HIToolbox       0x00007fff84dbbe61 SendUnicodeTextAEToUnicodeDoc + 468 
    20 HIToolbox       0x00007fff84dbbbdc TSMKeyEvent + 604 
    21 HIToolbox       0x00007fff84da6724 TSMProcessRawKeyEvent + 1909 
    22 AppKit        0x00007fff857a18bb -[NSTextInputContext handleEvent:] + 620 
    23 AppKit        0x00007fff857a15e5 -[NSView interpretKeyEvents:] + 186 
    24 AppKit        0x00007fff857a13be -[NSTextView keyDown:] + 819 
    25 AppKit        0x00007fff8571306f -[NSWindow sendEvent:] + 8769 
    26 AppKit        0x00007fff85647a86 -[NSApplication sendEvent:] + 4719 
    27 AppKit        0x00007fff855de4da -[NSApplication run] + 474 
    28 AppKit        0x00007fff855d71a8 NSApplicationMain + 364 
*/ 

這裏是我的源代碼:


EDIT: TOO LONG: Here is the line that is causing the problem: 
// Otherwise a key word was found, lets go colorize it 

       // Setup a temporary range to reflect the actual position of the string within the text view 
       // by applying the offset of our current scanning location to the location of the key word 
       fooRange.location = found.location; 
       fooRange.length = found.length; 
       fooRange.location += last_location; 

       // Work around for an odd bug that only occurs occasionally 
       if (![[string substringWithRange: fooRange] isEqual: keyWord]) { 
        fooRange.location++; 
       } 

和我的字典裏:


     [[textview textStorage] setDelegate:self]; 
    keyWords = [[NSMutableDictionary alloc] init]; 
    NSColor *blue = [NSColor colorWithCalibratedRed:0 green:0 blue:0.95 alpha:1]; 
    NSColor *green= [NSColor colorWithCalibratedRed:0 green:0.65 blue:0 alpha:1]; 
    NSColor *red = [NSColor colorWithCalibratedRed:0.90 green:0 blue:0 alpha:1]; 
    NSColor *tBlue =[NSColor blueColor]; 
    [keyWords setObject:blue forKey:@"abs"]; 
    [keyWords setObject:blue forKey:@"sin"]; 
    [keyWords setObject:blue forKey:@"cos"]; 
    [keyWords setObject:blue forKey:@"pow"]; 
    [keyWords setObject:blue forKey:@"rand"]; 
    [keyWords setObject:tBlue forKey:@"int"]; 
    [keyWords setObject:tBlue forKey:@"float"]; 
    [keyWords setObject:tBlue forKey:@"double"]; 
    [keyWords setObject:tBlue forKey:@"BOOL"]; 
    [keyWords setObject:red forKey:@"debug"]; 
    [keyWords setObject:red forKey:@"quote-color"]; 
    [keyWords setObject:@"\"" forKey:@"quote-open"]; 
    [keyWords setObject:@"\"" forKey:@"quote-close"]; 
    [keyWords setObject:green forKey:@"comment-color"]; 
+8

-1只是複製並粘貼你的代碼,並說「這不行,有什麼問題」並不是這裏的工作方式。我們希望你做某種調試。例如:*哪個*可變字符串正在拋出Range Out Of Bounds異常?它在哪裏被拋出?這是否一直在或在某些情況下發生?爲什麼代碼中不包含完整的方法? – 2011-01-14 18:05:04

+0

您可能不想實際將位置添加到位置:fooRange.location + = last_location; – 2011-01-14 18:17:24

回答

1

缺乏線索,這是一個瘋狂的猜測。

  fooRange.location = found.location; 
      fooRange.length = found.length; 
      fooRange.location += last_location; 

如果location距離字符串結束的length,最後一行是相當有效地將要推的位置超出了字符串的結尾,並引起一系列的異常。

0

好的,所以你知道錯誤發生的位置,並且你有一個非常明確的錯誤信息(「範圍或索引越界」)。

下一步將是看看相關的值(fooRange.locationfound.locationfooRange.lengthfound.lengthlast_location,並且[string length]),看看爲什麼的範圍超出字符串的結尾。您計算範圍的邏輯/算術可能不正確。

您可以通過將值打印出來(NSLog)或在其中放置斷點並使用調試器運行來檢查這些值。