当视图加载时,我初始化两个NSMutableArray。一个用于NSStrings,另一个用于NSData:
- (void)viewDidLoad {
[super viewDidLoad];
highlightStrings = [[NSMutableArray alloc] initWithObjects:@"initializer", nil];
highlightColorsData = [[NSMutableArray alloc] initWithObjects:@"initializer", nil];
}
在IBAction中,当点击按钮时,我在UITextView中获得UIButton背景颜色和所选文本。 NSString被添加到NSMutableArray,UIColor被存档到NSData并添加到NSMutableArray。两者都在NSUserDefaults中保存和检索(在此IBAction中测试它以便稍后在viewDidLoad中使用)。不幸的是,错误落在了我试图取消归档NSData以应用于另一个数组中的并行NSString的行。我已经测试了空的和未初始化的数组,所以我知道在数组中访问索引时有一个对象。错误行在for循环中靠近此块的末尾:
-(IBAction)lightGreenButtonAct:(id)sender{
//get color
selectedColor = [_lightGreenButton backgroundColor];
//get selected string
NSString *lyricsText = _lyricsPad.text;
NSString *selectedString = [[_lyricsPad text]
substringWithRange:[_lyricsPad selectedRange]];
//create mutable attributed string from all text (and range from selected string)
NSMutableAttributedString *mutLyricsText =[[NSMutableAttributedString alloc] initWithString: lyricsText];
NSRange selectedRange = [lyricsText rangeOfString: selectedString];
//add color data and string to respective arrays
[highlightStrings addObject:selectedString];
NSData *lightGreenColorData = [NSKeyedArchiver archivedDataWithRootObject:selectedColor];
[highlightColorsData addObject:lightGreenColorData];
//save color data and string arrays
NSUserDefaults *saveHighlightStrings = [NSUserDefaults standardUserDefaults];
[saveHighlightStrings setObject:highlightStrings forKey:@"saveHighlightStringsKey"];
[saveHighlightStrings synchronize];
NSUserDefaults *saveHighlightColorsData = [NSUserDefaults standardUserDefaults];
[saveHighlightColorsData setObject:highlightColorsData forKey:@"saveHighlightColorsDataKey"];
[saveHighlightColorsData synchronize];
//get saved color data and string arrays
NSArray *receiveHighlightStrings = [saveHighlightStrings arrayForKey:@"saveHighlightStringsKey"];
NSArray *receiveHighlightColorsData = [saveHighlightColorsData arrayForKey:@"saveHighlightColorsDataKey"];
for (int i=0; i<[receiveHighlightColorsData count]; i++) {
NSData *receiveColorData = receiveHighlightColorsData[i];
UIColor *colorFromData = [NSKeyedUnarchiver unarchiveObjectWithData:receiveColorData];
[mutLyricsText addAttribute:NSBackgroundColorAttributeName value:colorFromData range:[receiveHighlightStrings[i] range]];
}
_lyricsPad.attributedText = mutLyricsText;
}
点击浅绿色按钮时,会出现在调试器中:
2017-09-18 13:05:16.741987 Vizzy[7211:1016832] [Common] _BSMachError: port 7103; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
2017-09-18 13:05:16.742593 Vizzy[7211:1016832] [Common] _BSMachError: port 7103; (os/kern) invalid name (0xf) "Unable to deallocate send right"
2017-09-18 13:05:20.940 Vizzy[7211:1016832] -[__NSCFString bytes]: unrecognized selector sent to instance 0x60800003a340
2017-09-18 13:05:20.977 Vizzy[7211:1016832] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x60800003a340'
*** First throw call stack:
(
0 CoreFoundation 0x0000000107e3e34b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010789f21e objc_exception_throw + 48
2 CoreFoundation 0x0000000107eadf34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000107dc3c15 ___forwarding___ + 1013
4 CoreFoundation 0x0000000107dc3798 _CF_forwarding_prep_0 + 120
5 Foundation 0x000000010736e54e -[NSKeyedUnarchiver initForReadingWithData:] + 314
6 Foundation 0x000000010736e36a +[NSKeyedUnarchiver unarchiveObjectWithData:] + 61
7 Vizzy 0x00000001072c36f0 -[ViewController lightGreenButtonAct:] + 976
8 UIKit 0x0000000108263b88 -[UIApplication sendAction:to:from:forEvent:] + 83
9 UIKit 0x00000001083e92b2 -[UIControl sendAction:to:forEvent:] + 67
10 UIKit 0x00000001083e95cb -[UIControl _sendActionsForEvents:withEvent:] + 444
11 UIKit 0x00000001083e84c7 -[UIControl touchesEnded:withEvent:] + 668
12 UIKit 0x00000001082d10d5 -[UIWindow _sendTouchesForEvent:] + 2747
13 UIKit 0x00000001082d27c3 -[UIWindow sendEvent:] + 4011
14 UIKit 0x000000010827fa33 -[UIApplication sendEvent:] + 371
15 UIKit 0x0000000108a71b6d __dispatchPreprocessedEventFromEventQueue + 3248
16 UIKit 0x0000000108a6a817 __handleEventQueue + 4879
17 CoreFoundation 0x0000000107de3311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18 CoreFoundation 0x0000000107dc859c __CFRunLoopDoSources0 + 556
19 CoreFoundation 0x0000000107dc7a86 __CFRunLoopRun + 918
20 CoreFoundation 0x0000000107dc7494 CFRunLoopRunSpecific + 420
21 GraphicsServices 0x000000010bb98a6f GSEventRunModal + 161
22 UIKit 0x0000000108261f34 UIApplicationMain + 159
23 Vizzy 0x00000001072c6b0f main + 111
24 libdyld.dylib 0x000000010ac1968d start + 1
25 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)