iOS 10 - [UIDeviceRGBColor countByEnumeratingWithState:objects:count:]: unrecognized selector

Hello.


When our application is compiled on iOS 10 Developer Preview SDK, when launching one of the first view controllers of the application, the application crashes with this exception :


*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceRGBColor countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x17adb5a0'
*** First throw call stack:
(0x23c9b91b 0x23436e17 0x24474da3 0x24474f5f 0x24447e81 0x24446f25 0x286c8a0b 0x2857baa3 0x2834effb 0x28214a1f 0x28214971 0x28aa9ec1 0x28560d01 0x285846e5 0x28586d65 0x28223541 0x28586ca9 0x28586f91 0x2830ddb9 0x28586fcf 0x1c13b3 0x1beb89 0x1bf927 0x244c6655 0x23c5e58f 0x23c5e1c1 0x23c5c00d 0x23bab229 0x23bab015 0x2519bac9 0x2827d189 0xe3b65 0x23853873)
libc++abi.dylib: terminating with uncaught exception of type NSException


In the code of our application, this selector of UIDeviceRGBColor is never called.


Does someone have the same problem ? Is it a bug of iOS 10 Beta ?


When compiled with iOS 10 Beta SDK, the problem occurs as well on devices running on iOS 10 Beta as on iOS 9.


The problem does not appear (not on iOS 10, not on iOS 9) when the application is compiled with iOS 9 SDK


Thank you.


Best regards.

I have the same problem, and there's a thread about it on StackOverflow as well (without any help).


Any ideas anyone?

Apparently it is happening during the call:

[self.window makeKeyAndVisible];


Does anyone have any idea why?

Still the same problem with Xcode 8 beta 6.


I have managed to find a very ugly workaround by simply adding the missing methods as a category. Needless to say this is only a temporary stop gap so I can keep developing until this gets fixed properly.


I might make a slightly safer version by swizzling the method at runtime if and only if it does not exist yet, but still, even that is not really an acceptable solution.


@interface UIColor (HackToFixBugInIOS10)

@end

@implementation UIColor (HackToFixBugInIOS10)

- (NSUInteger) countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len

{

return 0;

}

- (NSUInteger) length

{

return 0;

}

@end

Meanwhile I have isolated the bug by removing almost all code from my project, with the bug still remaining. You can download it here:


https://www.dropbox.com/s/b7jggae97889xka/Bugtest.zip?dl=0


The xib references a lot of undefined classes (which I removed for clarity) but those are just warnings, not errors. The code compiles and runs just fine when compiled by XCode 7.x with the iOS 9 SDK.


When compiled with the XCode 8 beta 6 and iOS 10 SDK, even when running on an iOS 9.x device, you get the exception. On a device you get a bunch of hex addresses, but on the simulator you get a nice calling tree showing clearly that the crash happens deep down inside "initWithNibName:bundle:".


If you enable the ugly hack in "HackForUnrecognizedSelectorInIOS10.m" by changing "#if 0" to "if 1", the code runs without problems with the 10.0 SDK. But obviously you can't really ship code like that.


Can anyone reproduce this so I know it's not just something on my machine?


Thanks,


Michel

I think I encountered exactly the same issue, except that in the console the log is reporting "length" is being called:


*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceRGBColor length]: unrecognized selector sent to instance 0x600000479280'


It happens right after [UIView initWithCoder:] is called. It is invoked by pushing another view controller after the app is launched and settled at landing page (the first view controller), however the call to [UIView initWithCoder:] before that seems to be fine.


Call stack if it is useful (CSView is a subclass of UIView):

  0   CoreFoundation                      0x00000001159f434b __exceptionPreprocess + 171
  1   libobjc.A.dylib                     0x000000011545521e objc_exception_throw + 48
  2   CoreFoundation                      0x0000000115a63f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
  3   CoreFoundation                      0x0000000115979c15 ___forwarding___ + 1013
  4   CoreFoundation                      0x0000000115979798 _CF_forwarding_prep_0 + 120
  5   UIKit                               0x0000000113771468 CGRectFromString + 68
  6   UIKit                               0x00000001137721e5 -[NSCoder(UIGeometryKeyedCoding) decodeCGRectForKey:] + 117
  7   UIKit                               0x00000001137b9f72 -[UIView initWithCoder:] + 247
  8   MyApp_dev                           0x000000010f3dd3b6 -[CSView initWithCoder:] + 86
  9   UIKit                               0x0000000113b27853 -[UIClassSwapper initWithCoder:] + 240
  10  Foundation                          0x0000000114f25f12 _decodeObjectBinary + 2622
  11  Foundation                          0x0000000114f253c6 _decodeObject + 249
  12  UIKit                               0x0000000113b27524 -[UIRuntimeConnection initWithCoder:] + 178
  13  UIKit                               0x0000000113b27c5d -[UIRuntimeEventConnection initWithCoder:] + 60
  14  Foundation                          0x0000000114f25f12 _decodeObjectBinary + 2622
  15  Foundation                          0x0000000114f27089 -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] + 1829
  16  Foundation                          0x0000000114f2b216 -[NSArray(NSArray) initWithCoder:] + 200
  17  Foundation                          0x0000000114f25f12 _decodeObjectBinary + 2622
  18  Foundation                          0x0000000114f253c6 _decodeObject + 249
  19  UIKit                               0x0000000113b266c8 -[UINib instantiateWithOwner:options:] + 1249
  20  UIKit                               0x00000001138c4ddd -[UIViewController _loadViewFromNibNamed:bundle:] + 386
  21  UIKit                               0x00000001138c56ff -[UIViewController loadView] + 177
  22  UIKit                               0x00000001138c5a34 -[UIViewController loadViewIfRequired] + 201
  23  UIKit                               0x00000001138cc2be -[UIViewController __viewWillAppear:] + 118
  24  UIKit                               0x00000001138f717b -[UINavigationController _startCustomTransition:] + 1290
  25  UIKit                               0x0000000113907df0 -[UINavigationController _startDeferredTransitionIfNeeded:] + 697
  26  UIKit                               0x0000000113908f83 -[UINavigationController __viewWillLayoutSubviews] + 58
  27  UIKit                               0x0000000113af573f -[UILayoutContainerView layoutSubviews] + 223
  28  UIKit                               0x00000001137eaae4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
  29  QuartzCore                          0x00000001123b2cdc -[CALayer layoutSublayers] + 146
  30  QuartzCore                          0x00000001123a67a0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
  31  QuartzCore                          0x00000001123a661e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
  32  QuartzCore                          0x000000011233462c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
  33  QuartzCore                          0x0000000112361713 _ZN2CA11Transaction6commitEv + 475
  34  UIKit                               0x000000011371fba7 _UIApplicationFlushRunLoopCATransactionIfTooLate + 206
  35  UIKit                               0x0000000113f17032 __handleEventQueue + 5672
  36  CoreFoundation                      0x0000000115999311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
  37  CoreFoundation                      0x000000011597e59c __CFRunLoopDoSources0 + 556
  38  CoreFoundation                      0x000000011597da86 __CFRunLoopRun + 918
  39  CoreFoundation                      0x000000011597d494 CFRunLoopRunSpecific + 420
  40  GraphicsServices                    0x0000000118a61a6f GSEventRunModal + 161
  41  UIKit                               0x0000000113726a74 UIApplicationMain + 159
  42  MyApp_dev                           0x000000010f188bff main + 111
  43  libdyld.dylib                       0x00000001163af68d start + 1

I happened to find a way to solve this crash.


I noticed that some other projects using that UIView subclass of mine calling that method compiles and runs without problem. And those projects are using the class with a minor different in the configuration of the interface.


I solved the issue by doing the following:

  1. Open your interface file, in the right panel, navigate to the File Inspector tab.
  2. Change the attribute for "Builds for" to "iOS 7.0 and Later" (or whatever iOS version later) (Screen cap: http://i.stack.imgur.com/q8leG.png)
  3. Compile and run the project, in my case I solved the issue and everything are fine again


But I am not sure about the reason behind this.


You may refer to my answer in Stack Overflow here:

http://stackoverflow.com/a/39342106/1764327

The ugly hack I mentioned does solve the crash, but also damages functionality. Some buttons stop working, for example.


Meanwhile alanlo found a better solution: the "Builds for" menu for the Interface Builder Document needs to be set to at least 6.0. It's the lowest selectable version, but it can go lower if you import an old project, or if it's set to "Deployment Target" with a deployment target before 6.0.

Even I am getting the length error


[2060:685244] -[UIDeviceRGBColor length]: unrecognized selector sent to instance 0x170875800
[2060:685244] invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.
[2060:685244] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceRGBColor length]: unrecognized selector sent to instance 0x170875800'
*** First throw call stack:
(0x1899d76f8 0x188fd0578 0x1899de7a8 0x1899db7b0 0x1898d7e2c 0x18f94dc48 0x18fab0128 0x18fada420 0x18a3aa3bc 0x18a3a9a8c 0x18fce3c38 0x18a3aa3bc 0x18a3b0e7c 0x18a34693c 0x18a3aa3bc 0x18a3a9a8c 0x18fce2f38 0x18fb524f8 0x18f9261cc 0x18f7f2244 0x18f8abf7c 0x18f8abe54 0x18f8ab19c 0x18f8aabac 0x18f8aa758 0x18f8aa6bc 0x18f7ef670 0x18cce9524 0x18ccde200 0x18ccde0c0 0x18cc5b980 0x18cc82c50 0x18f7e5054 0x18998597c 0x1899835ac 0x189983a3c 0x1898b38d8 0x18b2ba198 0x18f85d7c8 0x18f858534 0x101795248 0x1894545b8)
libc++abi.dylib: terminating with uncaught exception of type NSException


I followed your SO answer. Sadly it didn't work. And pin pointing the source of the crash is proving to be very difficult.


EDIT: It did work after I set Builds for - Deployment Target(8.0). No idea why it works but I'm not complaining.

iOS 10 - [UIDeviceRGBColor countByEnumeratingWithState:objects:count:]: unrecognized selector
 
 
Q