0

in an iphone application I created and extension class to the NSString.

NSString+Extensions.m class

In one of the methods I need to convert the string to NSMutableString.

I tried to use this:

NSMutableString * stringToManipulate = [NSMutableString stringWithString:self];

But it is giving a warning:

Incompatible pointer types sending 'const Class' to parameter of type 'NSString *'

To my knowlede self is a reference to the string I called the method on, right? so why shouldn't it be of type NSString*? knowing that I can use the usual NSString methods on self.

Any idea on this issue?

Thanks

Y2theZ
  • 10,162
  • 38
  • 131
  • 200

2 Answers2

1

Check whether the method is declared as a class method (with the + symbol) or an instance method (with the - symbol). If its the first case, the compiler warning is normal. Turn the method to an instance one then

Nicola Miotto
  • 3,647
  • 2
  • 29
  • 43
  • yes it is declared using the + symbol. Anyway I can make that work without changing it to -? thanks – Y2theZ Mar 04 '13 at 12:59
  • Nope. Self in a class method is a reference to the class, so no instance value provided. If you want to work with an actual value, you just instantiate the string and then call an jnstance method in it. – Nicola Miotto Mar 04 '13 at 13:02
  • 1
    Ah sorry, I see your point. Yes that was it thank you. I thought I had to change the + in the NSString+Extensions.m name :) but you meant in the method. Sorry its really late and I need to sleep :) – Y2theZ Mar 04 '13 at 13:06
1

In Objective-C, when you call self within a class method, it references the class object. For an NSString class method, calling selfis the same as calling [NSString class].

So, your line is the same as :

NSMutableString * stringToManipulate = [NSMutableString stringWithString:[self class]];

If you change your method from a class one (+ symbol) to an object one (- symbol), it will work but you must call it differently (from an NSString object).

If you want further explanations, can you post the entire method declaration ? You can also check the NSObject protocol reference for more informations on the +class method : https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html

Hope this will help,

Zedenem
  • 2,479
  • 21
  • 23