Why does my app crash when backgrounding when using [NSAttributedString initWithData] - ios

I have a viewController that manages a UICollectionView. I have a helper method that is called from cellForItemAtIndexPath that provides an NSAttributedString for a label in the cell. The helper method formats an NSAttributedString from an html string. The app will crash when moving to the background, but only if the indexPath.item is greater than 1. In other words, I can exit the app without crashing from the first or second cell, but crash consistently on the third, forth, ... cell.
Here are my helper method and stack trace. Any idea why I am crashing on exiting the app?
#pragma mark - === Utility Methods === -
- (NSAttributedString *)stepDescriptionStringForIndexPath:(NSIndexPath *)indexPath {
NSString *headerString;
NSString *htmlString;
NSString *categoryString = [NSString stringWithFormat:#"Category: %#", self.knot.category.categoryName];
NSString *abokString = [NSString stringWithFormat:#"ABOK #: %#", self.knot.abokNumber];
NSMutableString *activitiesString = [NSMutableString stringWithCapacity:10];
[activitiesString appendString:#"Activities: "];
// build a string of activities to append to the description html
NSArray *activities = [self.knot.activities allObjects];
if ([activities count] > 0) {
int counter = 1;
for (Activity *activity in activities) {
[activitiesString appendString:activity.activityName];
if (counter < [activities count]) {
[activitiesString appendString:#", "];
}
counter ++;
}
}
// build an HTML string by concatinating the activities to the step description
// and add the header string
if(indexPath.item > 0){
Step *step = (Step *)self.steps[indexPath.item - 1];
headerString = [NSString stringWithFormat:#"Step %ld of %lu", (long)indexPath.item, (unsigned long)[self.steps count]];
htmlString =[NSString stringWithFormat:#"<p>%#</p>%#", headerString, step.stepDescription];
} else {
headerString = #"Overview";
htmlString = [NSString stringWithFormat:#"<p>%#</p>%#<p>%#</br>%#</br>%#</p>", headerString, self.knot.knotDescription, categoryString, abokString, activitiesString];
}
// convert the html string to an attributed string
NSMutableAttributedString *attrStringFromHTML = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
documentAttributes:nil
error:nil];
// set the font for the body
NSRange totalRange;
totalRange.location = 0;
totalRange.length = attrStringFromHTML.length;
[attrStringFromHTML addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:totalRange];
// set the font for the header
NSString *temp = [attrStringFromHTML string];
NSRange headerRange = [temp rangeOfString:headerString];
NSRange categoryRange = [temp rangeOfString:categoryString];
NSRange abokRange = [temp rangeOfString:abokString];
NSRange activitiesRange = [temp rangeOfString:activitiesString];
[attrStringFromHTML addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:headerRange];
//set the font for the activities paragraph
if(indexPath.item == 1){
[attrStringFromHTML addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:categoryRange];
[attrStringFromHTML addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:abokRange];
[attrStringFromHTML addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:activitiesRange];
}
return attrStringFromHTML;
}
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unexpected start state'
*** First throw call stack:
(
0 CoreFoundation 0x00000001131aad85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000112c1edeb objc_exception_throw + 48
2 CoreFoundation 0x00000001131aabea +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000110b96e1e -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 169
4 UIKit 0x00000001113a7d4e _prepareForCAFlush + 256
5 UIKit 0x00000001113b40b4 _beforeCACommitHandler + 12
6 CoreFoundation 0x00000001130cfc37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
7 CoreFoundation 0x00000001130cfba7 __CFRunLoopDoObservers + 391
8 CoreFoundation 0x00000001130c511c CFRunLoopRunSpecific + 524
9 UIFoundation 0x000000011a697a7a -[NSHTMLReader _loadUsingWebKit] + 2093
10 UIFoundation 0x000000011a698e74 -[NSHTMLReader attributedString] + 22
11 UIFoundation 0x000000011a6323c0 _NSReadAttributedStringFromURLOrData + 5623
12 UIFoundation 0x000000011a630d34 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:] + 115
13 WhatKnotToDo 0x000000010e84d3ac -[CSC_iPad_KnotDetailViewController stepDescriptionStringForIndexPath:] + 2476
14 WhatKnotToDo 0x000000010e84c31d -[CSC_iPad_KnotDetailViewController collectionView:cellForItemAtIndexPath:] + 477
15 UIKit 0x0000000111bff08f -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] + 483
16 UIKit 0x0000000111c02d96 -[UICollectionView _updateVisibleCellsNow:] + 4988
17 UIKit 0x0000000111c07575 -[UICollectionView layoutSubviews] + 258
18 UIKit 0x0000000111442980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
19 QuartzCore 0x0000000112af6c00 -[CALayer layoutSublayers] + 146
20 QuartzCore 0x0000000112aeb08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
21 QuartzCore 0x0000000112aeaf0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
22 QuartzCore 0x0000000112adf3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
23 QuartzCore 0x0000000112b0d086 _ZN2CA11Transaction6commitEv + 486
24 UIKit 0x0000000111394a0b __65-[UIApplication _beginSnapshotSessionForScene:withSnapshotBlock:]_block_invoke2222 + 601
25 UIKit 0x0000000111395201 __65-[UIApplication _performSnapshotsWithAction:forScene:completion:]_block_invoke2243 + 131
26 FrontBoardServices 0x00000001153e3039 -[FBSSceneSnapshotAction _finishAllRequests] + 65
27 FrontBoardServices 0x00000001153e2de3 -[FBSSceneSnapshotAction executeRequestsWithHandler:completionHandler:expirationHandler:] + 218
28 UIKit 0x0000000111395024 __65-[UIApplication _performSnapshotsWithAction:forScene:completion:]_block_invoke + 305
29 UIKit 0x0000000111394592 -[UIApplication _beginSnapshotSessionForScene:withSnapshotBlock:] + 1138
30 UIKit 0x0000000111394eb2 -[UIApplication _performSnapshotsWithAction:forScene:completion:] + 629
31 UIKit 0x0000000111394bbc -[UIApplication _handleSnapshotAction:forScene:completion:] + 153
32 UIKit 0x0000000111390a8f __102-[UIApplication _handleApplicationDeactivationWithScene:shouldForceExit:transitionContext:completion:]_block_invoke1993 + 290
33 UIKit 0x0000000111390657 __102-[UIApplication _handleApplicationDeactivationWithScene:shouldForceExit:transitionContext:completion:]_block_invoke1979 + 1258
34 UIKit 0x0000000111393f62 _runAfterCACommitDeferredBlocks + 317
35 UIKit 0x00000001113a7e4c _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
36 UIKit 0x00000001113b4147 _afterCACommitHandler + 90
37 CoreFoundation 0x00000001130cfc37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
38 CoreFoundation 0x00000001130cfba7 __CFRunLoopDoObservers + 391
39 CoreFoundation 0x00000001130c57fb __CFRunLoopRun + 1147
40 CoreFoundation 0x00000001130c50f8 CFRunLoopRunSpecific + 488
41 GraphicsServices 0x0000000115203ad2 GSEventRunModal + 161
42 UIKit 0x0000000111387f09 UIApplicationMain + 171
43 WhatKnotToDo 0x000000010e820b7f main + 111
44 libdyld.dylib 0x000000011396592d start + 1
45 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I've come across this issue too, after a few hours, i've found a solution for this one.
You'll just need to wrap the code for converting the NSAttributedString into dispatch_async.
For example:
DispatchQueue.MainQueue.DispatchAsync(() =>
{
var encodingData = ((NSString)html).Encode(NSStringEncoding.Unicode, true);
NSAttributedString data = new NSAttributedString(
encodingData,
new NSAttributedStringDocumentAttributes()
{
DocumentType = NSDocumentType.HTML,
}, ref error);
});
I'm using Xamarin, so these are C# code, but i believe they're similar in Swift and Obj C.
Look like when the system is making transition between pages (view controller), it will try to prevent any heavy task running on UI Thread. Which cause this issue.

Related

Trying to Append Retweeted Tweet

I am trying to append the string of a Retweeted tweet in Objective-C however I am always getting an issue with certain types of tweets. I believe that there is something wrong in the way I am attempting to substring the tweets. My code is shown below
2015-08-20 15:40:20.699 Floadt[21417:932559] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range {9223372036854775807, 52} out of bounds; string length 70'
*** First throw call stack:
(
0 CoreFoundation 0x000000010cd58c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c631bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010cd58b9d +[NSException raise:format:] + 205
3 CoreFoundation 0x000000010cc7fb98 -[__NSCFString substringWithRange:] + 136
4 Floadt 0x000000010929efa4 -[TwitterTableViewController tableView:cellForRowAtIndexPath:] + 2260
5 UIKit 0x000000010b465a28 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
6 UIKit 0x000000010b444248 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853
7 UIKit 0x000000010b45a8a9 -[UITableView layoutSubviews] + 210
8 UIKit 0x000000010b3e4a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
9 QuartzCore 0x000000010b1a6ec2 -[CALayer layoutSublayers] + 146
10 QuartzCore 0x000000010b19b6d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
11 QuartzCore 0x000000010b19b546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
12 QuartzCore 0x000000010b107886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
13 QuartzCore 0x000000010b108a3a _ZN2CA11Transaction6commitEv + 462
14 QuartzCore 0x000000010b1090eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
15 CoreFoundation 0x000000010cc8bca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
16 CoreFoundation 0x000000010cc8bc00 __CFRunLoopDoObservers + 368
17 CoreFoundation 0x000000010cc81a33 __CFRunLoopRun + 1123
18 CoreFoundation 0x000000010cc81366 CFRunLoopRunSpecific + 470
19 GraphicsServices 0x000000010eb09a3e GSEventRunModal + 161
20 UIKit 0x000000010b364900 UIApplicationMain + 1282
21 Floadt 0x000000010936574f main + 111
22 libdyld.dylib 0x000000010f32f145 start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Here is my code to Append the string:
if ([self Contains:#"RT" on:[data objectForKey:#"text"]]) {
TwitterRetweetCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"TwitterRetweetCell"];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
if (cell == nil) {
cell = [[TwitterRetweetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TwitterRetweetCell"];
}
// Lookup RT User
NSString *text = [data objectForKey:#"text"];
NSRange start = [text rangeOfString:#"RT #"];
NSRange end = [text rangeOfString:#":"];
NSString *shortString = [text substringWithRange:NSMakeRange(start.location, end.location)];
NSString *evenShorterString = [shortString substringFromIndex:4];
[self lookupTwitterUser:evenShorterString];
//Set username for twitter
[cell.nameLabel setText:evenShorterString];
//Set Retweet Status
NSString *retName = [NSString stringWithFormat:#"Retweeted by %#",data[#"user"][#"screen_name"]];
[cell.retweetedLabel setText:retName];
//Set Profile Pic for Twitter
return cell;
}
The two ranges:
NSRange start = [text rangeOfString:#"RT #"];
NSRange end = [text rangeOfString:#":"];
You need to check that those substrings were actually found and only use the ranges if they were found. Otherwise the rest of your code will fail because the range's location will be NSNotFound, which is the large number in your crash log. That suggests that your code encountered text containing "RT" but not "RT #". You should perform the following check before attempting to use start and end:
if (start.location != NSNotFound && end.location != NSNotFound)
{
// use start and end
}
Or better still, use this if condition instead of if ([self Contains:#"RT" on:[data objectForKey:#"text"]]).

Using Chat Application XMPPFramework Iam getting This Error

Using Chat Application with XMPPFramework Iam getting This Error
Message SendButton Action as given below, plz help me if any one have idea then tell me plz..............
- (IBAction)sendAction:(id)sender
{
if([_chatTextField.text length] > 0)
{
NSString* po = getUser.ofUser;
bubbleTable.typingBubble = NSBubbleTypingTypeNobody;
NSBubbleData *messageBubble = [NSBubbleData dataWithText:_chatTextField.text date:[NSDate dateWithTimeIntervalSinceNow:0] type:BubbleTypeMine];
[bubbleData addObject:messageBubble];
[bubbleTable reloadData];
[bubbleTable scrollBubbleViewToBottomAnimated:YES];
[[QuoteMessageController SharedInstance] SendChatMessageTo:getUser.account withContent:_chatTextField.text toUserId:[NSString stringWithFormat:#"%ld", (long)getUser.Id] andOFId:po andVerifyKey:chatMessageKey];
}
Getting error in output as given bellow>>>>>>
*** First throw call stack:
(
0 CoreFoundation 0x000000010e18ff35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010da7bbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010e18fe6d +[NSException raise:format:] + 205
3 Foundation 0x000000010ba211f8 -[NSString stringByAppendingString:] + 96
4 SourceSage 0x000000010ae0c939 +[SSWriteReadInDb writeQuotesTable:UserImageUrl:UserId:OfUser:] + 409
5 SourceSage 0x000000010adb8d56 -[QuoteMessageController SendChatMessageTo:withContent:toUserId:andOFId:andVerifyKey:] + 2454
6 SourceSage 0x000000010ad6ac30 -[SSChatViewController sendAction:] + 752
7 UIKit 0x000000010beb48be -[UIApplication sendAction:to:from:forEvent:] + 75
8 UIKit 0x000000010bfbb410 -[UIControl _sendActionsForEvents:withEvent:] + 467
9 UIKit 0x000000010bfba7df -[UIControl touchesEnded:withEvent:] + 522
10 UIKit 0x000000010befa308 -[UIWindow _sendTouchesForEvent:] + 735
11 UIKit 0x000000010befac33 -[UIWindow sendEvent:] + 683
12 UIKit 0x000000010bec79b1 -[UIApplication sendEvent:] + 246
13 UIKit 0x000000010bed4a7d _UIApplicationHandleEventFromQueueEvent + 17370
14 UIKit 0x000000010beb0103 _UIApplicationHandleEventQueue + 1961
15 CoreFoundation 0x000000010e0c5551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16 CoreFoundation 0x000000010e0bb41d __CFRunLoopDoSources0 + 269
17 CoreFoundation 0x000000010e0baa54 __CFRunLoopRun + 868
18 CoreFoundation 0x000000010e0ba486 CFRunLoopRunSpecific + 470
19 GraphicsServices 0x000000010fe7c9f0 GSEventRunModal + 161
20 UIKit 0x000000010beb3420 UIApplicationMain + 1282
21 SourceSage 0x000000010ae02893 main + 115
22 libdyld.dylib 0x000000010ee2e145 start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
After looking at this part of your stack trace:
2 CoreFoundation 0x000000010e18fe6d +[NSException raise:format:] + 205
3 Foundation 0x000000010ba211f8 -[NSString stringByAppendingString:] + 96
4 SourceSage 0x000000010ae0c939 +[SSWriteReadInDb writeQuotesTable:UserImageUrl:UserId:OfUser:] + 409
5 SourceSage 0x000000010adb8d56 -[QuoteMessageController SendChatMessageTo:withContent:toUserId:andOFId:andVerifyKey:] + 2454
6 SourceSage 0x000000010ad6ac30 -[SSChatViewController sendAction:] + 752
You'll see the exception is raised after a string is appended to a string. That is usually caused from appending nil to the string. By guess is that one of your NSString arguments that you're sending to the method below is nil.
[[QuoteMessageController SharedInstance] SendChatMessageTo:getUser.account
withContent:_chatTextField.text
toUserId:[NSString stringWithFormat:#"%ld", (long)getUser.Id]
andOFId:po
andVerifyKey:chatMessageKey];
To find out if this is in fact the case, either set a breakpoint here and check all the values, or add in the following before you call this method:
NSLog(#"%#", getUser.account);
NSLog(#"%#", _chatTextField.text);
NSLog(#"%#", po);
NSLog(#"%#", [NSString stringWithFormat:#"%ld", (long)getUser.Id]);
NSLog(#"%#", chatMessageKey);
I don't know what your parameters actually are, so remove the NSLogs for the variables that are not NSString. If one of these logs come back as nil or null (I forget what the console will output), then you found your issue. If it's an argument that you don't necessarily need, you should pass an empty NSString instead. I usually do this with a ternary. So assuming the culprit is _chatTextField.text, I would pass that argument as:
[[QuoteMessageController SharedInstance] SendChatMessageTo:getUser.account
withContent:_chatTextField.text ?: #""
toUserId:[NSString stringWithFormat:#"%ld", (long)getUser.Id]
andOFId:po
andVerifyKey:chatMessageKey];
Doing this basically says, if _chatTextField.text is true (which it will equate to if it is not nill), use that value, otherwise use #""
EDIT:
- (IBAction)sendAction:(id)sender
{
if([_chatTextField.text length] > 0)
{
NSString* po = getUser.ofUser ?: #"";
NSString *acct = getUser.account ?: #"";
bubbleTable.typingBubble = NSBubbleTypingTypeNobody;
NSBubbleData *messageBubble = [NSBubbleData dataWithText:_chatTextField.text date:[NSDate dateWithTimeIntervalSinceNow:0] type:BubbleTypeMine];
[bubbleData addObject:messageBubble];
[bubbleTable reloadData];
[bubbleTable scrollBubbleViewToBottomAnimated:YES];
[[QuoteMessageController SharedInstance] SendChatMessageTo:acct withContent:_chatTextField.text toUserId:[NSString stringWithFormat:#"%ld", (long)getUser.Id] andOFId:po andVerifyKey:chatMessageKey];
}

NSRange blockStartRange

Need your insight here...
This keeps crashing. Im calling a table on certain site, some say its the site that is causing but I can also make an exception. Can I get your two cents here?
NSRange blockStartRange = [pageContent rangeOfString:#"\t<table border=\"0\" cellpadding=\"4\" cellspacing=\"0\" id=\"table"];
NSRange blockEndRange = [pageContent rangeOfString:#"\t</table>" options:NSBackwardsSearch range:NSMakeRange(blockStartRange.location, [pageContent length] - blockStartRange.location)];
[HTMLString appendString:[pageContent substringWithRange:NSMakeRange(blockStartRange.location, blockEndRange.location + blockEndRange.length - blockStartRange.location)]];
// [HTMLString replaceOccurrencesOfString:#"width: 700px;" withString:#"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [HTMLString length])];
}
[webView loadHTMLString:HTMLString baseURL:[NSURL URLWithString:#"http://bowlatrabs.com/"]];
}
Error Log
2014-08-01 15:52:22.092 Bowl at Rabs[61401:90b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFString rangeOfString:options:range:locale:]: Range {2147483647, 2147542871} out of bounds; string length 59222'
*** First throw call stack:
(
0 CoreFoundation 0x0270b1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x020178e5 objc_exception_throw + 44
2 CoreFoundation 0x0270afbb +[NSException raise:format:] + 139
3 Foundation 0x000c38cd -[NSString rangeOfString:options:range:locale:] + 185
4 Foundation 0x000cf156 -[NSString rangeOfString:options:range:] + 69
5 Bowl at Rabs 0x0002d9e8 -[LeagueController viewDidLoad] + 920
6 UIKit 0x005dc33d -[UIViewController loadViewIfRequired] + 696
7 UIKit 0x005dc5d9 -[UIViewController view] + 35
8 UIKit 0x005f6942 -[UINavigationController _startCustomTransition:] + 778
9 UIKit 0x006038f7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 688
10 UIKit 0x006044e9 -[UINavigationController __viewWillLayoutSubviews] + 57
11 UIKit 0x007450d1 -[UILayoutContainerView layoutSubviews] + 213
12 UIKit 0x0052c964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
13 libobjc.A.dylib 0x0202982b -[NSObject performSelector:withObject:] + 70
14 QuartzCore 0x04d9b45a -[CALayer layoutSublayers] + 148
15 QuartzCore 0x04d8f244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
16 QuartzCore 0x04d8f0b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
17 QuartzCore 0x04cf57fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
18 QuartzCore 0x04cf6b85 _ZN2CA11Transaction6commitEv + 393
19 QuartzCore 0x04cf7258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
20 CoreFoundation 0x026d336e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
21 CoreFoundation 0x026d32bf __CFRunLoopDoObservers + 399
22 CoreFoundation 0x026b1254 __CFRunLoopRun + 1076
23 CoreFoundation 0x026b09d3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x026b07eb CFRunLoopRunInMode + 123
25 GraphicsServices 0x035765ee GSEventRunModal + 192
26 GraphicsServices 0x0357642b GSEventRun + 104
27 UIKit 0x004bdf9b UIApplicationMain + 1225
28 Bowl at Rabs 0x0000239c main + 76
29 libdyld.dylib 0x0325d725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
You need to handle the case where the strings do not exist. Apple's documentation for the rangeOfString: method says: "Returns {NSNotFound, 0} if aString is not found or is empty (#"")."
So you need to check to see if the returned ranges have range.location == NSNotFound and handle that in whatever way is best.
Other way to check is using NSRangeFromString
NSRangeFromString: Returns a range from a textual representation.
If the string passed into NSRangeFromString does not represent a valid range, it will return a range with its location and length set to 0.
NSString *string = #"invalid";
NSRange range = NSRangeFromString(string);
// {.location=0, .length=0}
You might check nshipster/range

Access Keys after adding NSDictionary to NSMutableArray. iOS

I have a NSDictionary that I add to a mutable array but when I try add the array to populate a uitableview I get an error.
NSMutableArray *array = [[NSMutableArray alloc] init];
for (id element in self.categoriesMutableNameArray) {
[array addObject:#"No"];
}
self.categoryDict = #{ #"title" : self.categoriesMutableNameArray, #"selected" : array};
self.categoryArr = [[NSMutableArray alloc] init];
self.categoryMutableDict = [NSMutableDictionary dictionaryWithDictionary:self.categoryDict];
[self.categoryArr addObject:self.categoryDict];
and the following categoryArr is printed like this:
2014-02-27 15:09:07.397 App[7982:70b] (
{
selected = (
No,
No,
No,
No,
No,
No,
No,
No,
No,
No,
No,
No,
No,
No
);
title = (
"Fashion - Women",
"Fashion - Men",
Kids,
"Accessories - Women",
"Accessories - Men",
"Styling / Hair",
Inspiration,
"Decoration / Architecture",
"Great Places",
"Art / Design",
"Music / Movie / Books",
"Food / Drink",
"Gadgets / Tech",
Rides
);
}
)
The trouble I am having is in the uitableview cellforrowatindexpath method I try and add the title key for the categoryArr to populate the uitableview and I get the following error on this line:
UILabel *categoryLabel = (UILabel *)[cell viewWithTag:111];
categoryLabel.text = [NSString stringWithFormat:#"%#",[[self.categoryArr objectAtIndex:indexPath.row] objectForKey:#"title"];
And the error log:
2014-02-27 15:24:33.804 App[8153:70b] -[__NSArrayM length]: unrecognized selector sent to instance 0xa9bc3a0
2014-02-27 15:24:33.807 App[8153:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0xa9bc3a0'
*** First throw call stack:
(
0 CoreFoundation 0x020c75e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01e4a8b6 objc_exception_throw + 44
2 CoreFoundation 0x02164903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x020b790b ___forwarding___ + 1019
4 CoreFoundation 0x020b74ee _CF_forwarding_prep_0 + 14
5 Foundation 0x006e18ed -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 39
6 Foundation 0x006e255a -[NSConcreteMutableAttributedString initWithString:attributes:] + 293
7 UIKit 0x01172bc6 -[UILabel _setText:] + 97
8 UIKit 0x01172d84 -[UILabel setText:] + 40
9 App 0x00047ebd -[PiccImageCategoriesViewController tableView:cellForRowAtIndexPath:] + 1533
10 UIKit 0x010b461f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
11 UIKit 0x010b46f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
12 UIKit 0x01098774 -[UITableView _updateVisibleCellsNow:] + 2378
13 UIKit 0x010abe95 -[UITableView layoutSubviews] + 213
14 UIKit 0x01030267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
15 libobjc.A.dylib 0x01e5c81f -[NSObject performSelector:withObject:] + 70
16 QuartzCore 0x00c8e2ea -[CALayer layoutSublayers] + 148
17 QuartzCore 0x00c820d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
18 QuartzCore 0x00c81f40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
19 QuartzCore 0x00be9ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
20 QuartzCore 0x00beae71 _ZN2CA11Transaction6commitEv + 393
21 QuartzCore 0x00beb544 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
22 CoreFoundation 0x0208f4ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
23 CoreFoundation 0x0208f41f __CFRunLoopDoObservers + 399
24 CoreFoundation 0x0206d344 __CFRunLoopRun + 1076
25 CoreFoundation 0x0206cac3 CFRunLoopRunSpecific + 467
26 CoreFoundation 0x0206c8db CFRunLoopRunInMode + 123
27 GraphicsServices 0x027779e2 GSEventRunModal + 192
28 GraphicsServices 0x02777809 GSEventRun + 104
29 UIKit 0x00fc5d3b UIApplicationMain + 1225
30 App 0x0001406d main + 141
31 libdyld.dylib 0x02f4d70d start + 1
32 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Thanks
The return of [[self.categoryArr objectAtIndex:indexPath.row] objectForKey:#"title"]; is a array of NSString, categoryLabel.text = [NSString stringWithFormat:#"%#", /*the return array*/]; this is the problem.
Maybe what you want is:
NSArray *categoryDetailArr = [[self.categoryArr objectAtIndex:indexPath.section] objectForKey:#"title"];
categoryLabel.text = [[categoryDetailArr objectAtIndex:indexPath.row];
Write this code
categoryLabel.text = [[[self.categoryArr objectAtIndex:0] objectForKey:#"title"]objectAtIndex:indexPath.row];
instead OF
categoryLabel.text = [NSString stringWithFormat:#"%#",[[self.categoryArr objectAtIndex:indexPath.row] objectForKey:#"title"];
And plz check that in UITableView dataSource methods i.e
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [categoryLabel objectAtIndex:0]valueForKey:#"selected"].count;
}
OR
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [categoryLabel objectAtIndex:0]valueForKey:#"title"].count;
}

Xcode: App crashes because I deleted a class no other class uses anymore?

Very strange behaviour...I've used before an NSNumber category called NSNumber+Currencies. Yesterday I've changed everything including core data to NSDecimalNumber so I have now a class NSDecimalNumber+Currencies. There is really no link at all! to NSNumber+Currencies anymore. However if I delete it and try to run my app in the simulator, my app crashes with the message "unrecognized selector sent to instance ..." This instance is a NSDecimalNumber and the methods I use in this category are basically the same as before. And I really import ONLY the NSDecimalNumber+Currencies. But it makes no sense...I've deleted the app from the simulator, cleaned my project, closed Xcode, removed the old category even manually from the build phases but nothing helps..is there another trick? If I leave this old category in it it runs smoothly...
My error log. However after the 2nd run my app doesn't start at all and the error message is a different one:
2014-01-28 11:51:29.458 NetIncome[2341:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(
0 CoreFoundation 0x01ecf5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01abe8b6 objc_exception_throw + 44
2 CoreFoundation 0x01e839c2 -[__NSArrayI objectAtIndex:] + 210
3 CoreData 0x01899c1f -[NSFetchedResultsController objectAtIndexPath:] + 255
4 NetIncome 0x00003dee -[MainCategoriesViewController tableView:cellForRowAtIndexPath:] + 206
5 UIKit 0x003ac61f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
6 UIKit 0x003ac6f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
7 UIKit 0x00390774 -[UITableView _updateVisibleCellsNow:] + 2378
8 UIKit 0x0038eb81 -[UITableView _updateVisibleCellsImmediatelyIfNecessary] + 66
9 UIKit 0x0039cb5f -[UITableView _visibleCells] + 35
10 UIKit 0x0039cbb4 -[UITableView visibleCells] + 33
11 UIKit 0x0039fcc0 -[UITableView setSeparatorStyle:] + 115
12 NetIncome 0x000037d4 -[MainCategoriesViewController styleTableView] + 100
13 NetIncome 0x0000300a -[MainCategoriesViewController viewDidLoad] + 1194
14 UIKit 0x003d1318 -[UIViewController loadViewIfRequired] + 696
15 UIKit 0x003f6b15 -[UINavigationController _layoutViewController:] + 39
16 UIKit 0x003f702b -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 235
17 UIKit 0x003f7123 -[UINavigationController _startTransition:fromViewController:toViewController:] + 78
18 UIKit 0x003f809c -[UINavigationController _startDeferredTransitionIfNeeded:] + 645
19 UIKit 0x003f8cb9 -[UINavigationController __viewWillLayoutSubviews] + 57
20 UIKit 0x00532181 -[UILayoutContainerView layoutSubviews] + 213
21 UIKit 0x00328267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
22 libobjc.A.dylib 0x01ad081f -[NSObject performSelector:withObject:] + 70
23 QuartzCore 0x001972ea -[CALayer layoutSublayers] + 148
24 QuartzCore 0x0018b0d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
25 QuartzCore 0x00197235 -[CALayer layoutIfNeeded] + 160
26 UIKit 0x003e3613 -[UIViewController window:setupWithInterfaceOrientation:] + 304
27 UIKit 0x00302177 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
28 UIKit 0x00300d16 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
29 UIKit 0x00300be8 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
30 UIKit 0x00300c70 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
31 UIKit 0x002ffd0a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
32 UIKit 0x002ffc6c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
33 UIKit 0x003009c3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
34 UIKit 0x00303fb6 -[UIWindow setDelegate:] + 449
35 UIKit 0x003d5737 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
36 UIKit 0x002f9c1c -[UIWindow addRootViewControllerViewIfPossible] + 609
37 UIKit 0x002f9d97 -[UIWindow _setHidden:forced:] + 312
38 UIKit 0x002fa02d -[UIWindow _orderFrontWithoutMakingKey] + 49
39 UIKit 0x0030489a -[UIWindow makeKeyAndVisible] + 65
40 UIKit 0x002b7cd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
41 UIKit 0x002bc3a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
42 UIKit 0x002d087c -[UIApplication handleEvent:withNewEvent:] + 3447
43 UIKit 0x002d0de9 -[UIApplication sendEvent:] + 85
44 UIKit 0x002be025 _UIApplicationHandleEvent + 736
45 GraphicsServices 0x035202f6 _PurpleEventCallback + 776
46 GraphicsServices 0x0351fe01 PurpleEventCallback + 46
47 CoreFoundation 0x01e4ad65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
48 CoreFoundation 0x01e4aa9b __CFRunLoopDoSource1 + 523
49 CoreFoundation 0x01e7577c __CFRunLoopRun + 2156
50 CoreFoundation 0x01e74ac3 CFRunLoopRunSpecific + 467
51 CoreFoundation 0x01e748db CFRunLoopRunInMode + 123
52 UIKit 0x002bbadd -[UIApplication _run] + 840
53 UIKit 0x002bdd3b UIApplicationMain + 1225
54 NetIncome 0x000027ad main + 141
55 libdyld.dylib 0x031c070d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
EDIT2: And this is the original error message, only shown once and immediately after deleting the relevant files:
2014-01-28 12:00:15.598 NetIncome[2872:70b] -[__NSCFNumber getLocalizedCurrencyString]: unrecognized selector sent to instance 0x8c59720
2014-01-28 12:00:15.602 NetIncome[2872:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber getLocalizedCurrencyString]: unrecognized selector sent to instance 0x8c59720'
*** First throw call stack:
(
0 CoreFoundation 0x01ecf5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01abe8b6 objc_exception_throw + 44
2 CoreFoundation 0x01f6c903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01ebf90b ___forwarding___ + 1019
4 CoreFoundation 0x01ebf4ee _CF_forwarding_prep_0 + 14
5 NetIncome 0x000358da -[NetIncomeViewController viewDidLoad] + 954
6 UIKit 0x003d1318 -[UIViewController loadViewIfRequired] + 696
7 UIKit 0x003f6b15 -[UINavigationController _layoutViewController:] + 39
8 UIKit 0x003f702b -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 235
9 UIKit 0x003f7123 -[UINavigationController _startTransition:fromViewController:toViewController:] + 78
10 UIKit 0x003f809c -[UINavigationController _startDeferredTransitionIfNeeded:] + 645
11 UIKit 0x003f8cb9 -[UINavigationController __viewWillLayoutSubviews] + 57
12 UIKit 0x00532181 -[UILayoutContainerView layoutSubviews] + 213
13 UIKit 0x00328267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
14 libobjc.A.dylib 0x01ad081f -[NSObject performSelector:withObject:] + 70
15 QuartzCore 0x001972ea -[CALayer layoutSublayers] + 148
16 QuartzCore 0x0018b0d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
17 QuartzCore 0x0018af40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
18 QuartzCore 0x000f2ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
19 QuartzCore 0x000f3e71 _ZN2CA11Transaction6commitEv + 393
20 QuartzCore 0x001b0430 +[CATransaction flush] + 52
21 UIKit 0x002d9dc9 _afterCACommitHandler + 131
22 CoreFoundation 0x01e974ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
23 CoreFoundation 0x01e9741f __CFRunLoopDoObservers + 399
24 CoreFoundation 0x01e75344 __CFRunLoopRun + 1076
25 CoreFoundation 0x01e74ac3 CFRunLoopRunSpecific + 467
26 CoreFoundation 0x01e748db CFRunLoopRunInMode + 123
27 GraphicsServices 0x0351e9e2 GSEventRunModal + 192
28 GraphicsServices 0x0351e809 GSEventRun + 104
29 UIKit 0x002bdd3b UIApplicationMain + 1225
30 NetIncome 0x00002b3d main + 141
31 libdyld.dylib 0x031c070d start + 1
32 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
The lines of code my app crashes the first time (the last line is the one):
//Get or set and get the gross income
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDecimalNumber *grossIncome = [defaults objectForKey:#"grossincome"];
if (!grossIncome) {
[defaults setObject:[NSDecimalNumber zero] forKey:#"grossincome"];
[defaults synchronize];
grossIncome = (NSDecimalNumber *)[defaults objectForKey:#"grossincome"];
}
self.incomeValueTextField.enabled = NO;
self.incomeValueTextField.delegate = self;
self.incomeValueTextField.keyboardType = UIKeyboardTypeDecimalPad;
self.incomeValueTextField.text = [grossIncome getLocalizedCurrencyString];
And my complete category:
#import "NSDecimalNumber+Currency.h"
#import "Singletons.h"
#implementation NSDecimalNumber (Currency)
- (NSString *) getLocalizedCurrencyString
{
NSNumberFormatter *numberFormatter = [[Singletons sharedManager] numberFormatter];
NSString *numberString = [numberFormatter stringFromNumber:self];
return numberString;
}
- (NSString *) getLocalizedCurrencyStringWithDigits:(int)digits
{
if(digits == 2){
return [self getLocalizedCurrencyString];
}
NSNumberFormatter *numberFormatter;
if (digits == 0){
numberFormatter =[[Singletons sharedManager] numberFormatterNoDigits];
} else {
numberFormatter =[[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setMinimumFractionDigits:digits];
[numberFormatter setMaximumFractionDigits:digits];
}
NSString *numberString = [numberFormatter stringFromNumber:self];
return numberString;
}
+ (NSDecimalNumber *) getUnLocalizedDecimalNumberWithString:(NSString *)currencyString
{
NSNumberFormatter *numberFormatter = [[Singletons sharedManager] numberFormatter];
NSNumber *formatedCurrency = [numberFormatter numberFromString:currencyString];
BOOL isDecimal = formatedCurrency != nil;
if(isDecimal){
return [NSDecimalNumber decimalNumberWithDecimal:[formatedCurrency decimalValue]];
} else {
return [NSDecimalNumber zero];
}
}
- (NSDecimalNumber *)abs {
if ([self compare:[NSDecimalNumber zero]] == NSOrderedAscending) {
// Number is negative. Multiply by -1
NSDecimalNumber * negativeOne = [NSDecimalNumber decimalNumberWithMantissa:1
exponent:0
isNegative:YES];
return [self decimalNumberByMultiplyingBy:negativeOne];
} else {
return self;
}
}
Yesterday you have been warned about subclassing/categories on class clusters, now you see why. (Okay, actually it's more or a problem with unexpected behaviour of NSUserDefaults, but it's related ^^)
Your problem is that the object you get from NSUserDefaults is not a NSDecimalNumber, because NSDecimalNumbers are not plist values that can be saved in NSUserDefaults. But NSDecimalNumber is a subclass of NSNumber, which can be saved in NSUserDefaults, so NSUserDefaults will only save the "NSNumber part" of the NSDecimalNumber, basically it converts it to a NSNumber. Through this process it loses its precision and it's class.
When you get the object from NSUserDefaults you will actually get an instance of NSNumber. And your category does not exist on NSNumber.
I added some debug log to your code and ran it for you:
// Wrong!
NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:#"1.23456789123456789"];
NSLog(#"%# - %#", decimalNumber, NSStringFromClass([decimalNumber class]));
[[NSUserDefaults standardUserDefaults] setObject:decimalNumber forKey:#"___number"];
NSDecimalNumber *objectFromUserDefaults = [[NSUserDefaults standardUserDefaults] objectForKey:#"___number"];
NSLog(#"%# - %#", objectFromUserDefaults, NSStringFromClass([objectFromUserDefaults class]));
Which yields this result:
xxx[98912:70b] 1.23456789123456789 - NSDecimalNumber
xxx[98912:70b] 1.234567891234568 - __NSCFNumber
As you can see the returned object is an instance of __NSCFNumber
Here is the correct way:
// Correct
NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:#"1.23456789123456789"];
NSLog(#"%# - %#", decimalNumber, NSStringFromClass([decimalNumber class]));
NSData *archivedDecimalNumber = [NSKeyedArchiver archivedDataWithRootObject:decimalNumber];
[[NSUserDefaults standardUserDefaults] setObject:archivedDecimalNumber forKey:#"___archivedNumber"];
NSData *dataFromUserDefaults = [[NSUserDefaults standardUserDefaults] objectForKey:#"___archivedNumber"];
NSDecimalNumber *decimalNumberFromUserDefaults = [NSKeyedUnarchiver unarchiveObjectWithData:dataFromUserDefaults];
NSLog(#"%# - %#", decimalNumberFromUserDefaults, NSStringFromClass([decimalNumberFromUserDefaults class]));
which yields:
xxx[98912:70b] 1.23456789123456789 - NSDecimalNumber
xxx[98912:70b] 1.23456789123456789 - NSDecimalNumber
as expected.
BUT you should still get rid of that NSDecimalNumber category. Just put that code into it's own class. Call it ZHNumberFormatter and do your formatting in there.
NSNumberFormatters take their time during creation, you don't want to create them for every single conversion.
Reset your simulator and try to run the app.
iOS Simulator --> Reset content and settings.
User NSKeyedArchiver and NSKeyedUnarchiver to store the object in user defaults.
Try to use below code its working fine.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDecimalNumber *grossIncome = [defaults objectForKey:#"grossincome"];
if (!grossIncome) {
NSDecimalNumber *decNum = [[NSDecimalNumber alloc] initWithDouble:0.0f];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:decNum];
[defaults setObject:myEncodedObject forKey:#"grossincome"];
[defaults synchronize];
NSData *myDecodedObject = [defaults objectForKey:#"grossincome"];
grossIncome = (NSDecimalNumber*)[NSKeyedUnarchiver unarchiveObjectWithData:myDecodedObject];
}
[grossIncome getLocalizedCurrencyString];
You probably still refer to the category somewhere.
You should create an exception Breakpoint. (look for "create exception Breakpoint" in the help menu)
This will stop you on the guilty line of code!

Resources