4

There is a method:

- (void)doSmth:(NSString *__strong*)str {
    NSLog(@"%@", *str);
}

What does it mean when __strong follows the class of a method parameter? Why are there two asterisks?

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Gikas
  • 961
  • 1
  • 8
  • 20

2 Answers2

7

Two asterisks mean it's a pointer to a pointer.

__strong is the opposite of __weak which you probably already know. It means we are talking about a strong reference here. While we hold to that reference, the object won't be deallocated.

Also we need to know that writing __strong Type *varName is technically wrong (although it works and almost everybody uses it). The correct syntax is Type * __strong varName.

Your syntax is a pointer to a strong reference to NSString. It means that when an object is returned from the method, there must be a release call from ARC to properly deallocate that object.

Please see the related question: NSError and __autoreleasing and the official documentation: Transitioning to ARC

Sulthan
  • 128,090
  • 22
  • 218
  • 270
1

Though this question has already answer, but people need to know the exact words or easiest description.

__strong

An object remains “alive” as long as there is a strong pointer to it.

When an object is assigned to that pointer, it is retained for as long as that pointer refers to it

When you want to ensure the object you are referencing is not deallocated while you are still using it.

Above points are meaning of __strong.

The Best Example of __strong from BJ Homer

Here dog is a object, and that the dog wants to run away (be deallocated).

Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached.

Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog!" As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it.

As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out.

Two asterisks

It is pointer to pointer

If you use pointer to pointer you have to do like Sulthan said when an object is returned from the method, there must be a release call from ARC to properly deallocate that object. So we have to know the way of pointer to pointer use

Here is the way for Handling pointer to pointer ownership issues

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39