Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not sure what you're trying to do, the question is a little bit unclear. Also, don't try to inherit from NSString, it's part of a class cluster so it's a lot more complicated that just overriding one method. You even have to i<a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html" rel="nofollow noreferrer">mplement the storage yourself</a>. Gets complicated real fast. </p> <p>There are two ways of adding methods to an existing class in Objective-C: <a href="http://www.cocoadev.com/index.pl?ClassCategories" rel="nofollow noreferrer">Categories</a> and <a href="http://www.cocoadev.com/index.pl?MethodSwizzling" rel="nofollow noreferrer">method Swizzling</a>. </p> <p>Categories allow you to define a new method on an existing class, you cannot <em>reliably</em> replace a method using categories. For example:</p> <pre><code>@interface NSString (MyStuff) - (id)mySpecialInit; @end; @implementation NSString (MyStuff) /* implementation of -(id)mySpecialInit would go here */ @end </code></pre> <p>Again, this method will not reliably replace an existing method on a class. </p> <p>For that you need method swizzling. Caveat - this is an advanced feature of the runtime, and it's real easy to get yourself into a debugging nightmare, so use with caution. Swizzling allows you to replace the implementation of one method with another method of the same signature. Using some tricks, you can call the super implementation as well. Matt Gallagher is generally credited with having the <a href="http://cocoawithlove.com/2008/03/supersequent-implementation.html" rel="nofollow noreferrer">best implementation of method swizzling</a>, as it allows for reliably calling the pre-existing implementation (called supersequent implementation). Take a look at the linked article on Cocoa with Love for example usages. Your edited code probably doesn't work because you're trying to swizzle a class method, but using the getInstanceMethod function. Use class_getClassMethod() instead. Also, you need to rename your category stringWithFormat method, it <strong>must not clash</strong> with the real method name. One last caveat - I've never seen method swizzling used on a method with variadic arguments (...), and <a href="http://c-faq.com/varargs/handoff.html" rel="nofollow noreferrer">this C q&amp;a</a> leads me to believe this won't be possible (try <a href="https://stackoverflow.com/questions/2345196/objective-c-passing-around-nil-terminated-argument-lists">this post</a> as a possible solution). As an academic exercise, here is what it would <em>probably</em> look like:</p> <p><strong>NSString+MyMethods.h</strong></p> <pre><code>@interface NSString (MyMethods) + (id)clu_stringWithFormat:(NSString *)format,...; @end </code></pre> <p><strong>NSString+MyMethods.m:</strong></p> <pre><code>@implementation NSString (MyMethods) + (id)clu_stringWithFormat:(NSString *)format,... { //Do your stuff here //call supersequent implementation //again, I have no idea if this will work as is, you might need to tweak how it passes the variable argument list invokeSupersequent(format); } +(void)load { //do your method swizzle in here, this is called one time only Method origMethod = class_getClassMethod([NSString class], @selector(stringWithFormat:); Method replMethod = class_getClassMethod([NSString class], @selector(clu_stringWithFormat:); method_exchangeImplementations(origMethod, replMethod); } </code></pre> <p>Given that this is likely impossible, and is a very bad idea otherwise - you're changing how a specific class in a class cluster operates, which Apple says "don't do this". And for good reason - what happens if someones calls [NSMutableString stringWithFormat:], or allocs an NSString and then calls -initWithFormat:? There are a lot of valid paths into creating a string with a format, you'd have a miserable time trying to maintain code which intercepts all of them. I would encourage you to rethink the design of this - whatever problem you're trying to solve, there is likely a much easier and more maintainable way of achieving it. Adding your own String Factory, for example.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload