so im trying to add new method for testing using Category from NSString, but some how i must declared like this with following step:
Create Category from
NSStringwith nameStringExtensionso it will beNSString+StringExtension, after that i declared my own methos that return type isStringso after i define in
NSString+StringExtension@interfaceand@implementation, i tried in my viewController to called it, but first i import the classNSString+StringExtensionafter that i do like this
NSString *testString = @"as d a s d"; NSLog(@"===== %@", [testString removeWhiteSpaceStringWithString:testString]);and it says
No visible @interface for 'NSString' declares the selector 'removeWhiteSpaceStringWithString:'
the question is, why it cannot use like that? i already search and see tutorial doing like that and its possible, but why i'm not able to do that?
so i found this way, but i don't know is this the correct code to use?
NSLog(@"===== %@", [[testString class] removeWhiteSpaceStringWithString:testString]);
anyone have the same case like i am?
Based upon what you have shared with us, it would appear that you defined a class method (with
+). It should be an instance method (with-) and then you don’t need the parameter, either. You can simply referenceself.For example:
And
Then you can do:
Obviously, do whatever you want in your implementation, but it illustrates the idea, that you want an instance method and you do not need to pass the string again as a parameter.