Coding for iOS apps, I find there is potential reason to prohibit inheriting NSNumber, NSNull, ...blabla.
If I try to do this, compiler does not fail but running will crash at once without any usage info, does anyone know the reason?
Coding for iOS apps, I find there is potential reason to prohibit inheriting NSNumber, NSNull, ...blabla.
If I try to do this, compiler does not fail but running will crash at once without any usage info, does anyone know the reason?
As with any class cluster, if you create a subclass of NSNumber, you have to override the primitive methods of its superclass, NSValue. Furthermore, there is a restricted set of return values that your implementation of the NSValue method objCType can return, in order to take advantage of the abstract implementations of the non-primitive methods. The valid return values are “c”, “C”, “s”, “S”, “i”, “I”, “l”, “L”, “q”, “Q”, “f”, and “d”.
More precisely, I believe it's because you never actually have an instance of NSNumber. Objective-C has the interesting property that the initializer of a class can actually change the object to which self points (this might actually be true of any method, but I've only ever heard of it being used in the init methods). I believe that NSNumber does this very thing.
See also the documentation on Class Clusters.
Hmmm… from NSNumber Class Reference, it looks like subclassing is allowed as long as you appropriately subclass NSValue.
Subclassing Notes
As with any class cluster, if you create a subclass of NSNumber, you have to override the primitive methods of its superclass, NSValue. Furthermore, there is a restricted set of return values that your implementation of the NSValue method objCType can return, in order to take advantage of the abstract implementations of the non-primitive methods. The valid return values are “c”, “C”, “s”, “S”, “i”, “I”, “l”, “L”, “q”, “Q”, “f”, and “d”.
I don't see any subclassing notes in NSValue, but my guess is that you would need to implement every method in NSValue.