Objective-C Question NSString

I need to return an NSString from a function:

NSString myfunc ( int x ) { // do something with x NSString* myString = [NSString string]; myString = @"MYDATA"; // NSLog(myString); return *myString; } 

So, I call this function and get * myString. Is this a pointer to data? How can I get MYDATA data?

+4
source share
1 answer

I would rewrite this function as follows:

 NSString* myfunc( int x ) { NSString *myString = @"MYDATA"; // do something with myString return myString; } 

In Objective-C, it most often works with a pointer to objects, rather than with the objects themselves, i.e. in your example with NSString* , not NSString .

Also, @"MYDATA" already a string, so you don't need to allocate and initialize myString before assignment.

+13
source

All Articles