Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tawpie

macrumors newbie
Original poster
Jul 8, 2008
18
0
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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

tawpie

macrumors newbie
Original poster
Jul 8, 2008
18
0
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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

tawpie

macrumors newbie
Original poster
Jul 8, 2008
18
0
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...)
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.