10

When reading the headers of Foundation I found this:

- (__strong const char *)UTF8String NS_RETURNS_INNER_POINTER;   
// Convenience to return null-terminated UTF8 representation

This is from NSString.h in the iOS 7.1 SDK, what does __strong const char * mean here?

I'm most confused about the "__strong" here.

3
  • 3
    Interesting question. Perhaps you should re-formulate it slightly to emphasize that it is about the __strong attribute used with a non-Object type (and not about const char * :-). I noted that if you define your own method -(__strong const char *)foo NS_RETURNS_INNER_POINTER; then the compiler warns that "'__strong' only applies to Objective-C object or block pointer types; type here is 'const char *'".
    – Martin R
    May 19, 2014 at 8:00
  • @MartinR The fact that the warning is not issued for the UTF8String declaration is probably only due to the fact that clang (and other compilers) suppress warnings in system headers. May 19, 2014 at 8:36
  • @NikolaiRuhe: You are right. Including a local copy of NSString.h gives the same warning.
    – Martin R
    May 19, 2014 at 9:01

3 Answers 3

6

Foundation is shared between iOS and Mac OS. On Mac OS, for a while there existed a garbage collection memory management system. It's now deprecated and not longer supported on Mac OS. It was never used on iOS.

GC used __strong as a modifier on plain pointer type declarations to make the pointed to memory collectible. This usage of __strong has no meaning in ARC or manual retain/release code. The fact that there's no warning for the declaration is probably only because clang issues no warnings in system headers.

2
  • 2
    __strong also has a meaning in ARC: clang.llvm.org/docs/AutomaticReferenceCounting.html
    – mipadi
    May 19, 2014 at 9:12
  • 2
    Well, of course it has a meaning in ARC. But the usage under ARC is constrained to "retainable object types". In this case a plain pointer is decorated with __strong and this has no meaning in ARC. May 19, 2014 at 9:18
0

NS_RETURNS_INNER_POINTER is a Clang annotation that indicates the method returns a pointer to one of its inner data structures (i.e., an instance variable), and that inner variable is not reference-counted, so whenever that method is called, ARC should increment the retain count for the receiver (because that object should not be released as long as the pointer to its inner data structures is in use).

In this case, __strong essentially acts in conjunction with NS_RETURNS_INNER_POINTER to indicate the lifetime of an object that returned a pointer to its inner data structures.

1
  • 1
    "__strong essentially acts in conjunction with NS_RETURNS_INNER_POINTER". I can't find documentation on this. May 19, 2014 at 9:19
-1

__strong means that the object (of class NSString in this case), that holds the pointer, retains its ownership till the end of the life of the object and the pointed memory will be garbage-collected. Hence INNER.

const char * means that the char array this pointer points to is constant, i.e. cannot be changed for example by changing some characters in it (you will get a compiler error).

If you try to compile this:

#import <Foundation/Foundation.h>

int main(int argc, char **argv)
{
    NSString *s = @"string";
    const char *str = [s UTF8String];
    printf("%s\n", str);
    str[0] = 'S';
    printf("%s\n", str);
    return 0;
}

you'll get read-only variable is not assignable error.

1
  • 3
    __strong is used on a plain pointer here, not an object type. That's not documented to mean something in the ARC specification. May 19, 2014 at 8:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.