Re: Is substitution of block-function parameters OK when it's an object type?
Re: Is substitution of block-function parameters OK when it's an object type?
- Subject: Re: Is substitution of block-function parameters OK when it's an object type?
- From: Quincey Morris <email@hidden>
- Date: Tue, 05 Aug 2014 11:18:53 -0700
On Aug 5, 2014, at 09:38 , Daryle Walker <email@hidden> wrote:
> The type of “obj” was originally “id,” but I changed it to what I knew the NSArray object actually held. The compiler accepted it. Can I change any block parameter, or only ones that are Objective-C objects? (For example, would changing the type of “idx” or “stop” cause errors?) What happens if I use the wrong Objective-C type?
There are two separate issues:
1. If you change the type, does that change the internals of the way the underlying function is called?
Generally, substituting one pointer type for another is safe in this respect, since on all Apple-supported architectures, all pointers are the same size. (Same size within the architecture, I mean. Different architectures might have different sizes.)
2. If you change the type, does the value of the parameter get mishandled?
Generally, yes. But when you are downcasting an object pointer (that is, the type you specify is a subclass of the object class that the API specifies), it’s safe if you have reason to know that the object is really of the downcast type. (If not, you can make a runtime check on the object class.)
Changing idx's ‘NSUInteger’ to something else won’t cause any immediate crash, but in theory you might end up with the wrong value, because of a missing sign conversion, or a bit truncation.
Changing the ‘stop’ parameter type is a very dangerous thing to do, because it’s an ‘out’ parameter, and so involves a pointer to somewhere else in memory. Write the wrong thing there and you can unleash havoc.
The kind of object downcasting you did is fairly common, and fairly safe, pragmatically. (OTOH Swift won’t let you get away with it.)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden