2

According to the docs the designated initializer should always be called, however when I try to get the blood type of user without having the permissions an NSError is created but its designated initializer (- [NSError initWithDomain:code:userInfo:]) is never called.

The code I'm using is:

HKHealthStore *healthStore = [[HKHealthStore alloc] init];
HKBloodTypeObject *bloodType = [healthStore bloodTypeWithError:&error];
NSLog(@"Blood type: %@, error: %@", bloodType, error);

Obs: I know that the designated initializer is not being called because I've swizzled it. Other methods like - [NSFileManager contentsOfDirectoryAtPath:error:] behave as expected.

Why is it happening? Am I missing something? Should I open a radar?

fpg1503
  • 7,492
  • 6
  • 29
  • 49
  • What are you trying to accomplish by swizzling the NSError initializer methods? – Allan Oct 27 '15 at 21:43
  • Automatic error logging: [https://github.com/fpg1503/FPGSilentLogger](https://github.com/fpg1503/FPGSilentLogger) – fpg1503 Oct 27 '15 at 21:45

1 Answers1

2

When an error gets returned to you by a framework like HealthKit which does most of its work out of process, it's likely that the errors that get returned to you have been instantiated in another process and then delivered over XPC. That means that the NSError you get in your process was actually reconstructed by NSXPCConnection using NSSecureCoding. If you look at the NSCoding protocol, you'll notice that initWithCoder: is also a pseudo-designated initializer. Any class that implements NSCoding or NSSecureCoding has initWithCoder: as an additional designated initializer which can be called instead of the other designated initializers.

Allan
  • 7,039
  • 1
  • 16
  • 26
  • Are there any other "pseudo-designated initializers"? Doesn't that destroy the point of having a designated initializer in the first place? The way I see it `initWithCoder:` is a convenience intializer created for the sole purpose of conforming to `NSCoding`, shouldn't it call `- [NSError initWithDomain:code:userInfo:]` instead of `- [NSObject init]` then? – fpg1503 Oct 30 '15 at 01:11