Question

I have a category on UIView that implements - (void)setWidth:(CGFLoat)newWidth, that sets the width by changing the frame of the view and a static library that has the same method. I am using the category throughout the whole project by adding it to the .pch (prefix header) file.

When I try to use the library method, my category overrides the setWidth: method and the library is never called. I don't really want to remove the category from the prefix header as that would cause serious issues with the rest of the project.

Any ideas how to resolve this issue?

Thank you!

~LSonic

Was it helpful?

Solution

Unfortunately, Objective-C methods (including those from categories) exist in the same namespace. If there are two method implementations with the same name, it is not defined which is called at runtime.

This is discussed in Avoid Category Method Name Clashes in Apple’s Programming with Objective-C.

There is no better way than prefixing the names of methods in your categories. For example:

- (void)ls_setWidth:(CGFLoat)newWidth;

Also see this question for more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top