Could someone please explain this error

Discussion in 'Mac Programming' started by Grumpy Mike, Aug 24, 2010.

  1. Grumpy Mike macrumors newbie

    Joined:
    Jan 25, 2010
    #1
    The two strings are NSMutable strings so why is it complaining?

    [​IMG]
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The values returned by the methods you are calling are NSString *. NSMutableString is a subclass of NSString. So you cannot (safely) assign a NSString * to a NSMutableString * as this does not make the string mutable: you cannot cast in that direction in the inheritance tree.

    I would note that you do not need them to be NSMutableStrings: all the methods you call exist on NSString and return new NSString objects. The do not mutate the existing string (even if called on an instance of NSMutableString).
     
  3. Grumpy Mike thread starter macrumors newbie

    Joined:
    Jan 25, 2010
    #3
    Thanks, the error message would suggest to me it was the other way round but it seems it is just a badly written error message. I assumed because it said "expecting" that is what it wanted to see.
    Corrected and message goes away.
    One step closer - thanks again.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    That is what it wanted to see. You declared ssd as NSMutableString * so it expected (or wanted) the value you assign to that variable to have the same type. You did not.

    The message makes perfect sense: you are assigning a value of type NSString * but the compiler expected you to assign a value of type NSMutableString *.
     
  5. akakhan macrumors newbie

    Joined:
    Aug 22, 2010
    #5
    Maybe try like this?
    Code:
    spp = (NSMutableString*)[[pan filename] stringByDeletedPathExtension];
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    As I explained in my initial post you cannot cast in that direction: you cannot cast an instance of one class to an instance of any subclass. You can only cast to superclasses. This is a very basic OO Programming truth.
     
  7. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
  8. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    To be more precise, you can cast all you like, but all it does is lie to the compiler, which means it can't warn you about the errors in your code. If you have an NSString* you can cast it to NSMutableString* but the object itself remains immutable. Calling any method that would try to modify the string will crash.

    You can also for example cast an NSView* to an NSDictionary* if you fancy doing that, but the object is still an NSView and trying to call NSDictionary methods will crash (and calling NSView methods won't work because the compiler won't let you).
     
  9. akakhan macrumors newbie

    Joined:
    Aug 22, 2010
    #9
    Thanks for the lesson (you and robbieduncan). I'm new in obj-c, but I learn quickly. ))

    If it is necessary that the string was mutable, then we can in fact create a new mutable string from the NSString.
    Code:
    NSMutableString * newstring = [NSMutableString stringWithString: [[pan filename] stringByDeletedPathExtension]];
     

Share This Page