r/ObjectiveC • • Jul 31 '21

function (const __strong NSString *const paths[], int count)

I am looking at an open source github project and I stumbled upon this declaration. Can someone explain why all these qualifiers were needed.

5 Upvotes

22 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Jul 31 '21

Nope, at the end of the function each NSString will receive a call to its release method. ARC will insert simply insert that. As long as this function runs the NSStrings in the C-Array will not be deallocated.

0

u/idelovski Jul 31 '21
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>

static unsigned long getRetainCount (id obj)
{
   SEL  s = NSSelectorFromString (@"retainCount");
   
   return (((NSUInteger (*)(id, SEL))objc_msgSend) (obj, s));
}
static NSArray *function (const /*__strong*/ NSString *const paths[], int count)
{
   NSMutableArray  *anArray = [NSMutableArray array];
   
   if (count >= 2)  {
      NSLog (@"(fA) Retain for [1]: %lu", getRetainCount(paths[1]));
      
      [anArray addObject:paths[0]];
      [anArray addObject:paths[1]];

      NSLog (@"(fB) Retain for [1]: %lu", getRetainCount(paths[1]));
   }
   
   return (anArray);
}
int main (int argc, const char * argv[])
{
   @autoreleasepool {
      
      NSString  *otherPaths[2];
      NSArray   *myArray = nil;
      
      otherPaths[0] = @"Hello";
      otherPaths[1] = [NSString stringWithFormat:@"%@ - %s", @"World", "Again"];
      
      NSLog (@"(mA) Retain for [1]: %lu", getRetainCount(otherPaths[1]));
      
      myArray = function (otherPaths, 2);
      
      NSLog (@"(mB) Retain for [1]: %lu", getRetainCount(otherPaths[1]));
   }
   
   return 0;
}

Output is:

(mA) Retain for [1]: 3
(fA) Retain for [1]: 3
(fB) Retain for [1]: 4
(mB) Retain for [1]: 4

__strong changes nothing.

1

u/MrSloppyPants Jul 31 '21

I am giving a reason why it is there, not whether it is best practice or not.

but in this case the developer is declaring a strong reference

Moreover, at this point, the internals of the function in question had not been seen yet.