When the view loads, I initialize two NSMutableArray. One is for NSStrings and the other is for NSData:
- (void)viewDidLoad {
[super viewDidLoad];
highlightStrings = [[NSMutableArray alloc] initWithObjects:@"initializer", nil];
highlightColorsData = [[NSMutableArray alloc] initWithObjects:@"initializer", nil];
}
In an IBAction, I get the UIButton background color and the selected text in a UITextView when the button is tapped. The NSString is added to an NSMutableArray and the UIColor is archived to NSData and added to an NSMutableArray. Both are saved and retrieved in NSUserDefaults (testing this within this IBAction to be used in viewDidLoad later). Unfortunately, the error falls on the line where I try to unarchive the NSData to be applied to the parallel NSString in the other array. I have tested for empty and uninitialized arrays already so I know there is an object at the indices being accessed in the array. The error line is near the end of this block in a for-loop:
-(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;
}
When the Light Green Button is tapped, this appears in the debugger:
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)