 |
 |
Objective C Warning?
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Sep 2000
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
Or, you could just use NSString values :-)
That's probably easier than dealing with the c-string memory management...
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|