Am 13.03.2015 um 07:20 schrieb Jonas Maebe <[email protected]>:
> I don't have 10.10 handy, but in that version Apple reportedly changed > those method signatures to literally return resp. NSDictionary* and > NSArray* rather than id/instancetype as before. That would mean even the mutable version needs to override with a conflicting type. > It's also shown like that now in the documentation: > https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/#//apple_ref/occ/instm/NSDictionary/initWithContentsOfFile: > and > https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/#//apple_ref/occ/instm/NSArray/initWithContentsOfFile: The documentation is kind of wrong here. I'm guessing instancetype is replaced by the class (which is basically what is done by the compiler) and then it's not shown anymore that it's instancetype. >> This means they both have the same signature and there's no need for the >> compiler to know whether it's an NSDictionary or an NSArray. You explicitly >> tell the compiler the type when you assign it to a variable of that type, >> but before that, it's only an id for the compiler and it doesn't care. > > Even with the different return types it doesn't matter for the message > dispatch (the selector is the same, as also indirectly pointed out by > Uli), but it does matter for static type checking at compile time. It never matters for the dispatch. You can assign an NSDictionary to an NSArray and then call objectForKey: on the NSArray typed variable - the compiler will warn, but it will work. The point here is that with instancetype you do have type checking, while with id you don't (and it will still work because id tells the compiler not to care). instancetype works because it always gets replaced with the class on which is it called. For example, if you have class A and B and a defines (instancetype)foo, then on A, it will return A*, while on B, it will return B* - even if foo is not redeclared in B. > To provide some more context: I'm asking this to determine how I should > deal with this in the Free Pascal Compiler for our Objective-Pascal dialect. You'd either have to live without type checking and have a type for id, or implement something like instancetype. -- Jonathan _______________________________________________ Do not post admin requests to the list. They will be ignored. Objc-language mailing list ([email protected]) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com This email sent to [email protected]
