PDA

View Full Version : Can someone explain the warning from this line of code?




HungrySeacow
Mar 6, 2006, 12:18 PM
The compiler gives me warning: "warning: assignment from distinct Objective-C type" for this line of code:
NSMutableString = [NSString substringWithRange:NSMakeRange( 1, 1 ) ];

I changed the names of my umm, pointers to NSMutableString and NSString :P. Would they be considered variables or pointers to classes or... you tell me :P.

Thanks!



bousozoku
Mar 6, 2006, 12:23 PM
Of what are you getting a substring? You have the location of the substring defined, but you're not pointing to any string on either side of the =.

whooleytoo
Mar 6, 2006, 12:44 PM
I think you mean to do something like this:


NSString* source = @"Hello World"
NSString* subString = [source substringWithRange:NSMakeRange(1,1)] ;

subString would now be "e" (since string indices start with 0).

HiRez
Mar 6, 2006, 01:28 PM
Right, you can't assign anything directly to a class, you need to create an instance variable of a class type and assign to that, as whooleytoo has it. He's creating subString as a variable of type NSString (actually a pointer to an NSString).

HungrySeacow
Mar 6, 2006, 10:06 PM
Here is the rundown of the code that I have. In my original post I had changed the name of my instance variables to just NSMutableString and NSString figuring that would bring clarity as to what type of class they were. Sorry, my mistake :).

stringData = [[NSString alloc] initWithString:@"My text goes here."];
mutableString = [[NSMutableString alloc] initWithString:@""];
intermediateMutableString = [[NSMutableString alloc] initWithString:@""];
intermediateMutableString = [stringData substringWithRange:NSMakeRange( x, 1 ) ];
[mutableString appendString:intermediateMutableString];

This code is in a loop and the variable "x" gets upped in the end of each loop.

The question that I had was why the compiler gives me the warning:"warning: assignment from distinct Objective-C type" for the line: intermediateMutableString = [stringData substringWithRange:NSMakeRange( x, 1 ) ];

EDIT: Just to let everyone know, allocating and initializing the variables are done outside the loop as to make sure they are not allocated more then once.

whooleytoo
Mar 6, 2006, 11:54 PM
[CODE]stringData = [[NSString alloc] initWithString:@"My text goes here."];
mutableString = [[NSMutableString alloc] initWithString:@""];
intermediateMutableString = [[NSMutableString alloc] initWithString:@""];
intermediateMutableString = [stringData substringWithRange:NSMakeRange( x, 1 ) ];


The line immediately above is incorrect, you're overwriting an NSMutableString pointer (intermediateMutableString) with an NSString; which will cause a memory leak and possibly other issues later on.

It should read:

[intermediateMutableString setString: [stringData substringWithRange:NSMakeRange( x, 1 ) ]] ;

HungrySeacow
Mar 7, 2006, 09:47 AM
Thanks whooleytoo, I should have looked at the documentation for NSMutableString more carefully :).

whooleytoo
Mar 7, 2006, 09:50 AM
Thanks whooleytoo, I should have looked at the documentation for NSMutableString more carefully :).

No problem - mistakes are the best way to learn! ;)

HiRez
Mar 7, 2006, 03:18 PM
One thing about Xcode (and compilers in general) is that they will often give you warning and error messages that look little like they're describing what's actually wrong with the code, so they can sometimes be hard to decipher. I would have eventually found the problem here if this were my code, but it wouldn't have been initially obvious to me by that message. Also, don't necessarily read to much into the location of an Xcode error. One might appear on some random line (which itself is fine and has nothing to do with the error) because of, for example, the omission of a bracket, brace, or semicolon many lines prior.