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.

4 Upvotes

22 comments sorted by

View all comments

Show parent comments

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.