Initialization of UISwitch causes unrecognized selector exception in iOS 6

喜夏-厌秋 提交于 2019-12-13 03:43:38

问题


I have a UISwitch in my view and everything works fine for iOS 7 both on my device and the simulator, but when I run my application in the iOS 6 simulator I get a weird exception.

The exception breakpoint fires at this particular line of code:

self.mySwitch = [[UISwitch alloc] init];

with the following output:

-[__NSCFString count]: unrecognized selector sent to instance

Is this somehow a bug in the simulator? I have no idea how to fix this and at the moment I don’t have an iOS 6 device at hand.

The relevant stack trace at this point is:

* thread #1: tid = 0x177e6, 0x0191be52 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread, stop reason = breakpoint 1.1
frame #0: 0x0191be52 libobjc.A.dylib`objc_exception_throw
frame #1: 0x01d144bd CoreFoundation`-[NSObject(NSObject) doesNotRecognizeSelector:] + 253
frame #2: 0x01c78bbc CoreFoundation`___forwarding___ + 588
frame #3: 0x01c7894e CoreFoundation`_CF_forwarding_prep_0 + 14
frame #4: 0x01c0ab10 CoreFoundation`CFArrayGetCount + 80
frame #5: 0x0106cc04 UIKit`__36-[_UISwitchInternalView _labelImage]_block_invoke_0 + 144
frame #6: 0x010a22fe UIKit`+[UIImage(_UIImageTintedInterfaceExtension) _cachedImageForKey:fromBlock:] + 131
frame #7: 0x0106cb6c UIKit`-[_UISwitchInternalView _labelImage] + 187
frame #8: 0x0106e6ed UIKit`-[_UISwitchInternalView _buildControl] + 1454
frame #9: 0x0106b7c1 UIKit`__39-[_UISwitchInternalView initWithFrame:]_block_invoke_0 + 36
frame #10: 0x00eb240c UIKit`+[UIView(Animation) _performWithoutAnimation:] + 82
frame #11: 0x0106b793 UIKit`-[_UISwitchInternalView initWithFrame:] + 420
frame #12: 0x0106f196 UIKit`-[UISwitch _commonInitNewLook] + 225
frame #13: 0x0106f741 UIKit`-[UISwitch initWithFrame:] + 143
frame #14: 0x0106f6ab UIKit`-[UISwitch init] + 118

EDIT: I use only two calls regarding UIAppearance and the crash occurs even when I comment them out.

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor slubDarkBlue]];
[[UISearchBar appearance] setImage:[UIImage imageNamed:@"searchBarIcon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

回答1:


This does not make a lot of sense, because the crash says something about sending the message "count" to a string. Which points to an issue with a dangling reference. If you are sure that the crash occurs inside [[UISwitch alloc] init], try [[UISwitch alloc] initWithFrame:CGRectZero];

Are you using any appearance methods?




回答2:


An Apple Developer in the Developer Forums got me in the right direction. Turns out it was a localization issue.

I did:

[myUserDefaults setObject:inAppLanguage forKey:@"AppleLanguages"];

instead of:

[myUserDefaults setObject:@[inAppLanguage] forKey:@"AppleLanguages"];

where inAppLanguage is a NSString like @"de" or @"en". So the UISwitch encountered the single NSString instead of an NSArray of NSStrings.




回答3:


Sounds like you're injecting an NSString where there should be an NSArray or anything countable here.

In any case it's certainly not a simulator bug, so be sure to double-check your code. It may well be in a previous instruction or initialization. Or even maybe in one of your xib files.

Also, as you don't have the same behavior in iOS 7 and 6, you may want to check for any API change you would have missed.



来源:https://stackoverflow.com/questions/20045291/initialization-of-uiswitch-causes-unrecognized-selector-exception-in-ios-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!