I'm trying to take an input string and return a new string that is encoded/decoded based on a dictionary that I created. I have tried the below code using instance variables and local variables. I am having a problem using the for loop. I get out of scope errors. When I use an instance variable for newText, I get a exec bad address when I try to assign the dictionaries value to it. When I declare newText in the method, I get out of scope errors.
- (NSString *) decodeText:(NSString *)text {
NSString *parseString = text;
NSLog(@"Letterdictionary = %@",lettersDictionary);
NSInteger y = [parseString length];
NSMutableString *newText = [NSMutableString stringWithCapacity:[parseString length]];
for (int x=0; x < y; x++) {
NSRange Range = NSMakeRange(x, 1);
NSString *myChar = [parseString substringWithRange:Range];
NSLog(@"Mychar =:%@",myChar);
[newText appendString:[lettersDictionary objectForKey:myChar]];
NSLog(@"newtext=%@",newText);
}
NSLog(@"NewText=%@",newText);
return newText;
}
here is the output from the debugger:
2010-08-21 07:59:27.151 GeoWordCalc[77460:207] Letterdictionary = {
N = A;
O = B;
P = C;
}
2010-08-21 07:59:47.891 GeoWordCalc[77460:207] Mychar =:A
2010-08-21 07:59:49.707 GeoWordCalc[77460:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString appendString:]: nil argument'
*** Call stack at first throw:
(
0 CoreFoundation 0x023df919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0252d5de objc_exception_throw + 47
2 CoreFoundation 0x02398078 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x02397fea +[NSException raise:format:] + 58
4 Foundation 0x000a585a mutateError + 156
5 GeoWordCalc 0x000050aa -[WordDecoder decodeText:] + 330
6 GeoWordCalc 0x00004cec -[DecoderViewController textFieldCharacterEntered] + 157
7 UIKit 0x003424b0 -[UIControl(D eprecated) sendAction:toTarget:forEvent:] + 67
8 UIKit 0x00344d81 -[UIControl(Internal) _sendActionsForEventMask:withEvent:] + 525
9 UIKit 0x0034a051 -[UITextField fieldEditorDidChange:] + 258
10 UIKit 0x003597f3 -[UIFieldEditor webViewDidChange:] + 76
11 Foundation 0x00022c1d _nsnote_callback + 145
12 CoreFoundation 0x023b7cf9 __CFXNotificationPost_old + 745
13 CoreFoundation 0x0233711a _CFXNotificationPostNotification + 186
14 Foundation 0x000187c2 -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
15 CoreFoundation 0x0235042d __invoking___ + 29
16 CoreFoundation 0x02350301 -[NSInvocation invoke] + 145
17 WebCore 0x02e6277a _ZL19SendDelegateMessageP12NSInvocation + 602
18 WebKit 0x03c0b72f _ZN15WebEditorClient24respondToChangedContentsEv + 63
19 WebCore 0x02ee889c _ZN7WebCore6Editor14appliedEditingEN3WTF10PassRefPtrINS_11EditCommandEEE + 460
20 WebCore 0x036bdb9b _ZN7WebCore13TypingCommand24typingAddedToOpenCommandENS0_14ETypingCommandE + 75
21 WebCore 0x02ee312f _ZN7WebCore13TypingCommand28insertTextRunWithoutNewlinesERKNS_6StringEb + 159
22 WebCore 0x02ee306b _ZN7WebCore13TypingCommand10insertTextERKNS_6StringEb + 331
23 WebCore 0x02ee2eb6 _ZN7WebCore13TypingCommand7doApplyEv + 182
24 WebCore 0x02ee2c58 _ZN7WebCore11EditCommand5applyEv + 248
25 WebCore 0x02ee1c95 _ZN7WebCore13TypingCommand10insertTextEPNS_8DocumentERKNS_6StringERKNS_16VisibleSelectionEbb + 1525
26 WebCore 0x02ee1058 _ZN7WebCore6Editor33insertTextWithoutSendingTextEventERKNS_6StringEbPNS_5EventE + 1048
27 WebCore 0x02ee0c15 _ZN7WebCore12EventHandler28defaultTextInputEventHandlerEPNS_9TextEventE + 197
28 WebCore 0x02eb4548 _ZN7WebCore4Node19defaultEventHandlerEPNS_5EventE + 152
29 WebCore 0x02eb42a8 _ZN7WebCore4Node20dispatchGenericEventEN3WTF10PassRefPtrINS_5EventEEE + 1016
30 WebCore 0x02eb3e39 _ZN7WebCore4Node13dispatchEventEN3WTF10PassRefPtrINS_5EventEEE + 217
31 WebCore 0x02ee0ae0 _ZN7WebCore11EventTarget13dispatchEventEN3WTF10PassRefPtrINS_5EventEEERi + 112
32 WebCore 0x02ee07e1 _ZN7WebCore12EventHandler20handleTextInputEventERKNS_6StringEPNS_5EventEbb + 225
33 WebCore 0x02ee06f9 _ZN7WebCore6Editor10insertTextERKNS_6StringEPNS_5EventE + 57
34 WebKit 0x03c0af07 -[WebHTMLView(WebNSTextInputSupport) insertText:] + 951
35 WebCore 0x02ee0594 -[WAKResponder tryToPerform:with:] + 84
36 WebKit 0x03c0a9b3 -[WebView(WebViewEditingActions) _performResponderOperation:with:] + 147
37 WebKit 0x03c0a910 -[WebView(WebViewEditingActions) insertText:] + 48
38 UIKit 0x003f32db -[UIKeyboardImpl addInputString:fromVariantKey:] + 3251
39 UIKit 0x003f4a1a -[UIKeyboardImpl handleKeyEvent:] + 1918
40 UIKit 0x005151c0 -[UIKeyboardLayoutStar sendStringAction:forKey:] + 684
41 UIKit 0x005199ba -[UIKeyboardLayoutStar touchUp:] + 2556
42 UIKit 0x0040b9a3 -[UIKeyboardLayout touchesEnded:withEvent:] + 550
43 UIKit 0x002dc2ff -[UIWindow _sendTouchesForEvent:] + 567
44 UIKit 0x002be1ec -[UIApplication sendEvent:] + 447
45 UIKit 0x002c2ac4 _UIApplicationHandleEvent + 7495
46 GraphicsServices 0x02c45afa PurpleEventCallback + 1578
47 CoreFoundation 0x023c0dc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
48 CoreFoundation 0x02321737 __CFRunLoopDoSource1 + 215
49 CoreFoundation 0x0231e9c3 __CFRunLoopRun + 979
50 CoreFoundation 0x0231e280 CFRunLoopRunSpecific + 208
51 CoreFoundation 0x0231e1a1 CFRunLoopRunInMode + 97
52 GraphicsServices 0x02c442c8 GSEventRunModal + 217
53 GraphicsServices 0x02c4438d GSEventRun + 115
54 UIKit 0x002c6b58 UIApplicationMain + 1160
55 GeoWordCalc 0x000027c4 main + 102
56 GeoWordCalc 0x00002755 start + 53
) terminate called after throwing an instance of 'NSException'