On Oct 18, 2016, at 01:56 , Patrick J. Collins <patr...@collinatorstudios.com>
> 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>
> // other stuff
> .... Meanwhile a class in the game library:
> @interface CCNode : NSObject
> @property(nonatomic,readwrite,assign) float scale;
> // other stuff
> so I had a class inherit from that:
> @interface MenuItem : CCNode <MenuInteractable>
> .. 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 (Objcemail@example.com)
Help/Unsubscribe/Update your Subscription:
This email sent to arch...@mail-archive.com