Objective-C NSString Question

核能气质少年 提交于 2019-12-10 16:08:02

问题


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"?


回答1:


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, i.e., 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.



来源:https://stackoverflow.com/questions/602211/objective-c-nsstring-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!