On Oct 18, 2016, at 01:56 , Patrick J. Collins <patr...@collinatorstudios.com> 
wrote:
> 
> The scale code I discovered was breaking due to a protocol's interface
> being used on a class that defined a different type.
> 
> So, it went like this:
> 
> @protocol MenuInteractable <NSObject>
> 
> -(void)setScale:(CGFloat)scale;
> // other stuff
> 
> @end
> 
> .... Meanwhile a class in the game library:
> 
> @interface CCNode : NSObject
> @property(nonatomic,readwrite,assign) float scale;
> // other stuff
> @end
> 
> so I had a class inherit from that:
> 
> @interface MenuItem : CCNode <MenuInteractable>
> @end
> 
> .. and an object using that was doing:
> 
> id<MenuInteractable> item = menuItem;
> item.scale = 1.0f;
> 
> //
> 
> And then when I'd get into the setter it would be assigning the value 0.

Exactly so. The bug is that you declared ‘setScale’ in your protocol with the 
wrong return type. It’s a weakness of Obj-C that its dynamism prevents the 
compiler from statically checking return types in all cases, and float vs. 
double is one of the particularly dangerous cases.

If your game library uses float consistently, you should generally use float 
throughout your code, too. If you had been using Sprite Kit, you would 
generally use CGFloat throughout your code, because that’s what it uses.

Note that using the “wrong” type in expressions and assignments is not a 
problem, because the compiler emits code for the conversion for you. So, 
something like this:

        int /* or: float */ one = 1;
        item.scale = one;

works however “scale” is declared because “one" is converted to the correct 
floating point type. The issue only occurs when a caller is expecting a 
different type from what the callee (unknown to the caller) returns.

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (Objc-language@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to