简体   繁体   中英

Objective-C NSString Question

I need to return a 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 that a pointer to the data? How can I get to the data "MYDATA"?

I would rewrite this function the following way:

NSString* myfunc( int x )
{
   NSString *myString = @"MYDATA";

   // do something with myString
   return myString;        
}

In Objective-C it is more common to work with pointer to objects, not objects themselves, ie, in your example with NSString* , not NSString .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM