Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 6, 2006, 12:18 PM   #1
HungrySeacow
macrumors regular
 
Join Date: Jan 2006
Location: West Palm Beach
Can someone explain the warning from this line of code?

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!
__________________
|||||| YummySoup! - Recipe management and sharing at its best!
HungrySeacow is offline   0 Reply With Quote
Old Mar 6, 2006, 12:23 PM   #2
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
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 =.
bousozoku is offline   0 Reply With Quote
Old Mar 6, 2006, 12:44 PM   #3
whooleytoo
macrumors 603
 
whooleytoo's Avatar
 
Join Date: Aug 2002
Location: Cork, Ireland.
Send a message via AIM to whooleytoo
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).
__________________
Mac <- Macintosh <- McIntosh apples <- John McIntosh <- McIntosh surname <- "Mac an toshach" <- "Son of the Chief"
whooleytoo is offline   0 Reply With Quote
Old Mar 6, 2006, 01:28 PM   #4
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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).
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Mar 6, 2006, 10:06 PM   #5
HungrySeacow
Thread Starter
macrumors regular
 
Join Date: Jan 2006
Location: 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.
__________________
|||||| YummySoup! - Recipe management and sharing at its best!
HungrySeacow is offline   0 Reply With Quote
Old Mar 6, 2006, 11:54 PM   #6
whooleytoo
macrumors 603
 
whooleytoo's Avatar
 
Join Date: Aug 2002
Location: Cork, Ireland.
Send a message via AIM to whooleytoo
Quote:
Originally Posted by HungrySeacow
[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:

Code:
[intermediateMutableString setString: [stringData substringWithRange:NSMakeRange( x, 1 ) ]] ;
__________________
Mac <- Macintosh <- McIntosh apples <- John McIntosh <- McIntosh surname <- "Mac an toshach" <- "Son of the Chief"
whooleytoo is offline   0 Reply With Quote
Old Mar 7, 2006, 09:47 AM   #7
HungrySeacow
Thread Starter
macrumors regular
 
Join Date: Jan 2006
Location: West Palm Beach
Thanks whooleytoo, I should have looked at the documentation for NSMutableString more carefully .
__________________
|||||| YummySoup! - Recipe management and sharing at its best!
HungrySeacow is offline   0 Reply With Quote
Old Mar 7, 2006, 09:50 AM   #8
whooleytoo
macrumors 603
 
whooleytoo's Avatar
 
Join Date: Aug 2002
Location: Cork, Ireland.
Send a message via AIM to whooleytoo
Quote:
Originally Posted by HungrySeacow
Thanks whooleytoo, I should have looked at the documentation for NSMutableString more carefully .
No problem - mistakes are the best way to learn!
__________________
Mac <- Macintosh <- McIntosh apples <- John McIntosh <- McIntosh surname <- "Mac an toshach" <- "Son of the Chief"
whooleytoo is offline   0 Reply With Quote
Old Mar 7, 2006, 03:18 PM   #9
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Source Code with Line Numbers Senor Cuete Mac Programming 12 Jan 17, 2014 02:33 AM
Carrier: AT&T - Upgraded 1 Line to Put 5s on Other Line Activation Issue segrobja iPhone 20 Sep 25, 2013 10:23 PM
What is a "line of code"? Sydde Mac Programming 39 Apr 8, 2013 07:36 PM
pre-ordered through AT&T for one line, can I activate it to a different line? robdam1001 iPhone 0 Sep 28, 2012 01:01 PM
Can someone who uses the extra phantom line upgrade strategy explain how it works?? snook911 iPhone 63 Sep 18, 2012 08:02 AM

Forum Jump

All times are GMT -5. The time now is 04:00 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC