what does this warning mean?

Discussion in 'Mac Programming' started by tawpie, Aug 2, 2008.

  1. tawpie macrumors newbie

    Joined:
    Jul 8, 2008
    #1
    and mostly, is it serious?

    Greetings all... Xcode 3.1 gives me the following warning mostly when dealing with NSString both mutable and not:

    !warning: Initialization from a distinct Objective-C type

    This has variants with "assignment", and "passing argument xx...", all complaining about distinct Objective-C types. So two questions:

    First: what is this telling me is wrong?
    Second: is what is wrong something that needs to be fixed?

    TIA
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Normally this means you are using the wrong sort of argument to a function, etc.

    Maybe you can post some code? Are you accidentally using char * constants such as:
    "My String"
    instead of NSString constants like:
    @"My String"
    ?

    -Lee
     
  3. tawpie thread starter macrumors newbie

    Joined:
    Jul 8, 2008
    #3
    sure: an example that complains about initialization from a distinct Objective-C...


    NSMutableArray *nbeComponents = [formattedString componentsSeparatedByString:stringThatSeparatesSides];


    formattedString is an NSMutableString
    stringThatSeparatesSides is an NSString

    another example that warns "assignment from distinct..."

    formattedString = [numberFormatter stringFromNumber:inputStringAsNSNumber];


    formattedString is an NSMutableString
    numberFormatter is an NSNumberFormatter
    inputStringAsNSNumber is an NSNumber
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    An NSMutableArray is an NSArray.
    An NSArray is *not* an NSMutableArray.
    An NSMutableString is an NSString.
    An NSString is *not* an NSMutableString.

    Those methods are returning the non-mutable versions of those things, and you are assigning them to a pointer to the mutable versions. This is no good, and will cause problems. You can initialize a newly allocated NSMutableArray with -initWithArray and pass in an NSArray, and you can initialize a newly allocated NSMutableString with -initWithString and pass in an NSString. That's probably the best way to get a mutable copy of the things returned by those functions.

    -Lee
     
  5. tawpie thread starter macrumors newbie

    Joined:
    Jul 8, 2008
    #5
    had to be something like that. I figured that something that was mutable would be able to accept something that was non-mutable since mutable came from non but what you say makes good sense.

    Now I have a lot of work to make things match.

    But thank you! (I hate trouble later on...)
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    When you have a variable of type NSMutableArray*, then anyone would think that the array it points to can be modified. So if this variable contains a pointer to an actual non-mutable array, that would be a recipe for disaster, because any attempt to actually modify the array would fail in some way. For that reason, Objective-C must not just allow an assignment assigning an NSArray* to an NSMutableArray* variable.

    The other way round is harmless: Anyone looking at a variable of type NSArray* would think the array cannot be modified and therefore won't try to modify it. If the actual object is an NSMutableArray, no harm is done.

    Basically the rationale behind the warning is: The compiler must not let you write code that allows to modify a non-modifiable object without giving some warning.
     

Share This Page