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

HungrySeacow

macrumors regular
Original poster
Jan 11, 2006
137
1
West Palm Beach
The compiler gives me warning: "warning: assignment from distinct Objective-C type" for this line of code:
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

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
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

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
I think you mean to do something like this:

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

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

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
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

macrumors regular
Original poster
Jan 11, 2006
137
1
West Palm Beach
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 :).

Code:
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:
Code:
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

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
HungrySeacow said:
Code:
stringData = [[NSString alloc] initWithString:@"My text goes here."];
mutableString = [[NSMutableString alloc] initWithString:@""];
intermediateMutableString = [[NSMutableString alloc] initWithString:@""];
intermediateMutableString = [stringData substringWithRange:NSMakeRange( x, 1 ) ];
[/QUOTE]

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:

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

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.