views:

925

answers:

4

I am using Three20 for the iphone and I am trying to change what a method does within it by using a class category. It compiles fine, but I never reach the break point in it.

I'm assuming a class category affects all instances of the class, so I don't have to recompile the static library for it to work.

I also know that the class I'm using for the category is being seen because if I add a 'x' to the end of the class name when I interface and implement the category the compiler fails.

Any guidance here is VERY appreciated.

A: 

When two categories override the same method on the same class in the same binary, it isn't defined which override will take precedence. For best results, create a subclass and then override the method on that. Alternatively, since Three20 is Apache-licensed, you can make your change in the library itself.

rpetrich
I know I can change the library, but I was hoping for a quick fix, I don't want to research how to recompile the static library, but I may have to. Thanks.
Brenden
A really quick fix would be to use method_(get|set)Implementation to replace the method at runtime.
rpetrich
+3  A: 

As is pointed out here, you'll need the -all_load option to be set in your application's Other Linker Flags in order to have categories be linked against static libraries in iPhone OS 3.0. -ObjC should do the trick as well, but it's not working on the current SDK.

This might be contributing to your problem, although I think Three20 requires those linker flags to be set for it to link properly with your application in the first place, so you might already have these flags in place.

Brad Larson
Ya, already have those flags, thanks.
Brenden
+1 the -all_load flag helped me with a similar (but not identical) problem, although I already had -ObjC set.
Felixyz
A: 

I doubt that you can override methods with categories.

According to Objective-C 2.0 documentation, you can only add methods:

Although the language currently allows you to use a category to override methods the class inherits, or even methods declared in the class interface, you are strongly discouraged from using this functionality. A category is not a substitute for a subclass.

mouviciel
They discourage it, but you can. I understand why they discourage it, but this was supposed to be a quick fix as opposed to having to recompile the library into a custom static library. I want my changes to be separate unless the community needs them in the library.
Brenden
If you really want to replace the implementation of a method, not just add methods, you may want to look into method swizzling: http://www.cocoadev.com/index.pl?MethodSwizzling
Brad Larson
A: 

I have a project that is linking to a static library and yes the -all_load flag is required when installing on the device with the 3.0 sdk. -ObjC was all that was needed for the simulator.

isaac