NSUserDefaults Crash

I have a NSMutableDictionary saved to NSUserDefaults ( accessed by [NSUserDefaults standardDefault] );

I put some keys and values into the mutable dictionary, and then save the dictionary into NSUserDefaults;

There were no crash occurred When I used my phones for testing!

However, when the app was published through AppStore, crash happen many times;

I user tencent bugly to collect these crashes, and the stack error information is below:


0 libobjc.A.dylib

objc_msgSend + 16

1 CoreFoundation

CFEqual + 592

2 CoreFoundation

-[CFPrefsPlistSource alreadylocked_setValues:forKeys:count:from:] + 1072

3 CoreFoundation

-[CFPrefsSource setValues:forKeys:count:removeValuesForKeys:count:from:] + 220

4 CoreFoundation

-[CFPrefsSearchListSource alreadylocked_setValues:forKeys:count:from:] + 600

5 CoreFoundation

-[CFPrefsSource setValues:forKeys:count:removeValuesForKeys:count:from:] + 220

6 CoreFoundation

-[CFPrefsSource setValue:forKey:from:] + 64

7 CoreFoundation

___108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke + 268

8 CoreFoundation

_normalizeQuintuplet + 356

9 CoreFoundation

-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:] + 108

10 CoreFoundation

-[_CFXPreferences setValue:forKey:appIdentifier:container:configurationURL:] + 92

11 CoreFoundation

_CFPreferencesSetAppValueWithContainer + 128

12 Foundation

-[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 68

13 The App Name

-[FCHUserSettings setDesc:WithKey:](FCHUserSettings.m:324)

14 The App Name

-[FCHRootViewController viewDidLoad](FCHRootViewController.m:239)

15 UIKit

-[UIViewController loadViewIfRequired] + 1020

16 UIKit

-[UINavigationController _updateScrollViewFromViewController:toViewController:] + 76

17 UIKit

-[UINavigationController _startTransition:fromViewController:toViewController:] + 172

18 UIKit

-[UINavigationController _startDeferredTransitionIfNeeded:] + 1164

19 UIKit

-[UINavigationController __viewWillLayoutSubviews] + 164

20 UIKit

-[UILayoutContainerView layoutSubviews] + 188

21 UIKit

-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1420

22 QuartzCore

-[CALayer layoutSublayers] + 184

23 QuartzCore

CA::Layer::layout_if_needed(CA::Transaction*) + 324

24 QuartzCore

CA::Context::commit_transaction(CA::Transaction*) + 320

25 QuartzCore

CA::Transaction::commit() + 580

26 UIKit

___34-[UIApplication _firstCommitBlock]_block_invoke_2 + 140

27 CoreFoundation

___CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20

28 CoreFoundation

___CFRunLoopDoBlocks + 264

29 CoreFoundation

___CFRunLoopRun + 1224

30 CoreFoundation

CFRunLoopRunSpecific + 552

31 GraphicsServices

GSEventRunModal + 100

32 UIKit

UIApplicationMain + 236

33 The App Namee

main (main.m:15)

34 libdyld.dylib

_start + 4


If you the possible reason, please tell you!


Thank you in advance!!!



-----------------------------supplement0

The Crash Tips is :


#0 Thread

SIGSEGV

SEGV_ACCERR

Replies

TMI.

When you save an NSMutableDictionary to NSUserDefaults it is saved as an NSDictionary. I suspect you are reading it back and trying to change a key.



https://developer.apple.com/documentation/foundation/nsuserdefaults

A default object must be a property list—that is, an instance of (or for collections, a combination of instances of)

NSData
,
NSString
,
NSNumber
,
NSDate
,
NSArray
, or
NSDictionary
. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

Values returned from

NSUserDefaults
are immutable, even if you set a mutable object as the value. For example, if you set a mutable string as the value for “MyStringDefault”, the string you later retrieve using the
stringForKey:
method will be immutable. If you set a mutable string as a default value and later mutate the string, the default value won’t reflect the mutated string value unless you call
setObject:forKey:
again.