Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Objective C Warning?

Objective C Warning?
Thread Tools
Fresh-Faced Recruit
Join Date: Sep 2000
Status: Offline
Reply With Quote
May 11, 2001, 01:39 PM
 
Hello all. I am making an app using cocoa w/ objective C in project builder. I am getting the following warning, and don't understand what it is getting at. Can someone explain this? Someone in another thread said you should get your objective C code to compile without warnings, to prevent any runtime errors, and while I am not getting any runtime errors, I would like to know whether this is something I can ignore or not...

Here is the function declaration:
- (void) setSavedDataWithFirst char*)fn Last char*)ln Address1 char*)a1 Address2 char*)a2 City char*)c State char*)s Zip char*)z ;


Here is the call to the function, which is responsible for throwing the errors:
[ dataManager setSavedDataWithFirst: [[firstName stringValue] cString] Last: [[lastName stringValue] cString] Address1: [[ address1 stringValue ] cString] Address2: [[address2 stringValue] cString] City: [[city stringValue] cString] State: [[state stringValue] cString] Zip: [[ zip stringValue ] cString] ];

In the above statement, firstName, lastName, address1, address2, city, state, and zip are all outlets connected to different NSTextFields.

The warnings given are all the same, one for each arg. They are in the form:
passing arg 1 of 'setSavedDataWithFirst:Last:Address1:Address2:City :State:Zip:' discards qualifiers from pointer target type.

Each arg has an equivalent warning, just the arg number changes.

What does this mean, and can or should I ignore it?

Thanks!

Spencer




[This message has been edited by swcrissman (edited 05-11-2001).]
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
May 11, 2001, 02:14 PM
 
Before all of your cString calls, stick this: (char *)

e.g.:

(char *)[myString cString];

Not sure exactly _why_ cc makes this a big deal, since (const char *) is declared as the return type for the cString method.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 11, 2001, 03:04 PM
 
The -cString method is declared as returning (const char *), which indicates the return value should not be modified. The method in question is declared as taking (char *), which indicates that the string might possibly be modifed inside the method. Therefore, the "const" qualifier is being discarded when making the call. This is more of a C warning than Objective-C specifically.

To fix it, either cast all the arguments to (char *) as was suggested (meaning you know that the method in question won't modify the contents), or just change the method to accept (const char *) parameters instead, which indicates that the method won't be modifying the strings in any way. You can pass (char *) values as (const char *) arguments without a warning.
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 11, 2001, 03:40 PM
 
Or, you could just use NSString values :-)

That's probably easier than dealing with the c-string memory management...
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
May 11, 2001, 04:14 PM
 
Originally posted by lindberg:
Or, you could just use NSString values :-)

That's probably easier than dealing with the c-string memory management...
Excellent advice. Calls to cString might fail (and return NULL) if ever any of those strings had characters that cannot be converted into the default c-string encoding (which is what the cString method) does. On a default US or European system European characters will work, for the most part, but Japanese and many other languages for instance won't.

If you are using some piece of code that is written in terms of c-strings and cannot be easily converted to NSStrings, then you might want to consider using the UTF8String method, which will always succeed. (It uses the UTF8 encoding, instead of the current c-string encoding.)


Ali
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:03 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2