1

In iOS development, is it possible to use NSString and return it from a function?

e.g.

(NSString * ) foo {
  return @"";

} 

This is not an objective c method, just function

2
  • 1
    Yes, you can, but you don't do it like that... Commented Aug 30, 2012 at 11:53
  • 1
    Please learn C before Objective-C. You don't even know the syntax for a C function... Commented Aug 30, 2012 at 11:54

4 Answers 4

5

Yes, seeing as objective-c objects are just pointers, you can create a C function to return one:

NS_RETURNS_RETAINED NSString *myFunction() {
    return [[NSString alloc] init];
}

Notice the use of NS_RETURNS_RETAINED. This is a hint to ARC and the static analyzer that this function returns a retained value to the receiver, and that it's their responsibility to release it.

If you were returning an autoreleased value, try using NS_RETURNS_NOT_RETAINED instead.

Sign up to request clarification or add additional context in comments.

7 Comments

Note that either attribute is sufficient for returning constant strings (and neither one is needed) - I'm just sure that'll be the next questio of OP :)
@H2CO3 Not necessarily true. With objective-c 2.5 (what I call the addition of the new literals), a NSString literal is not guaranteed to be a constant (e.g. allocated in the executable's memory). It could very well be allocated on the heap at run-time, you don't know.
That's perfectly true! (But, you know, I have no access to these new tools - I don't know how the new literals work...)
@RichardJ.RossIII Do you mean if I have alloc init in the return than I should use NS_RETURNS_RETAINED, and if I return @"xxx" then I should use NS_RETURNS_NOT_RETAINED? What if I don't provide the hints (Assume I am using ARC already)
@Yoga if you don't provide hints, then for all methods that aren't alloc and init, NS_RETURNS_NOT_RETAINED is the default.
|
1

Yes, but the syntax is different:

NSString *foo()
{
    return @"bar";
}

13 Comments

And, of course, the file must be a an m or mm file or have it's file type attribute set appropriately in Info.
@HotLicks no, it's completely valid to have a C-function in a header file as long as it is defined as static, so that you don't run into linker errors. In fact, it's quite common to have a static inline C function in a header, such as NSMakeRange.
@HotLicks not necessarily. 1. Have you used gcc -x objc foo.someExoticExtension for forcing the compiler to interpret a file with any extension as a given language? 2. "have it's file type attribute set appropriately in Info" - What's info? What's that file type attribute?
@H2CO3 I agree. In fact, there are excellent 3rd party alternatives to Xcode, such as AppCode by JetBrains (but it costs a fortune). And nothing beats the command line compiler.
@RichardJ.RossIII -- The point is that, without the right file type, the file will be compiled as C and the the @ will be a syntax error.
|
1
NSString *foo()
{
    const char* yourString = "bar";
    NSString* yourNSString = [NSString stringWithFormat:@"%s", yourString];

    return yourNSString;
}

4 Comments

Yes, it's a way to return a NSString from a C function, but it's not how you create a NSString from a C-string. Try using +stringWithUTF8String: instead.
@RichardJ.RossIII this method has an advantage, indeed. It doesn't crash upon the C string being NULL.
@H2CO3 if your C-string is NULL, then you have other issues to worry about most likely.
@RichardJ.RossIII not necessarily... Why wouldn't it be, for example, a valid out-of-range indicator (that is what NULL is for...)
0

Yes, why not? Check NSStringFromRect() and similar for a reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.