I need te return an NSString as char *:
char *inboxPath (void)
{
NSURL * url = [[[[NSFileManager defaultManager]
URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"inbox.txt"];
return [[NSString stringWithFormat:@"%@", url] UTF8String];
}
I get: Returning ‘const char *’ from a function with result type ‘char *’ discards qualifiers
Why I get this and how I can solve it?
Obviously I don’t know much C, is there an easier way to do this?
The function:
returns
char*while-[NSString UTF8String]returnsconst char*. The qualifier you’re dropping is theconst. To solve this, you should declare your function:Mutating immutable (
const) data is one common way to introduce undefined behaviour, so you definitely want to avoid dropping theconstqualifier.If you need a mutable
charbuffer, make a copy of the utf8 string returned by-[NSString UTF8String].