Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8912311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:10:01+00:00 2026-06-15T04:10:01+00:00

I’ve just read the accepted excellent answer to this question that clarifies the conceptual

  • 0

I’ve just read the accepted excellent answer to this question that clarifies the conceptual differences between strong and weak pointers in Objective-C, and I’m still trying to understand the practical differences. I come from a C++ background where these concepts don’t exist, and I’m having trouble figuring out where I would use one vs the other.

Could someone please provide a practical example, using Objective-C code, that illustrates the different uses of strong and weak pointers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:10:02+00:00Added an answer on June 15, 2026 at 4:10 am

    Concept

    It’s all about the retain counts. ARC is a convenience, to prevent developers worrying about manually retaining and releasing. At its core, a strong variable will knock up the retain count by 1, whereas a weak variable won’t.

    See below:

    @interface Test () {
        NSString* strongVariable; // Instance variables default to strong
        __weak NSString* weakVariable;
    }
    @end
    
    // This has a retain count of 1, it has been allocated to memory by the user, and stored in a local variable (which is strong)
    NSString* str = [[NSString alloc] initWithString:@"Test"];
    
    // The string object will now have a retain count of 2, as the strong variable has increased its retain count
    strongVariable = str;
    
    // This weak variable does **not** increase the retain count, and as such it will still be 2
    weakVariable = str;
    
    // --
    
    // Now, lets remove some references
    // This will reduce the retain count to 1, as a strong variable has lost its link
    strongVariable = nil;
    
    // This will also reduce the retain count, as another strong variable has lost it's reference. This means the retain count is 0, and the object can now be considered to not exist
    str = nil;
    
    // What happens to weakVariable?
    // Because it is referencing an object with a 0 retain count, the runtime will set the value of this variable automatically to nil (iOS 5 and above).
    NSLog(@"%@", (weakVariable == nil) ? @"nil" : @"Not nil") // Will print "nil"
    

    You can’t get into a situation where a strong variable is referencing an object with a retain count of 0, that defies the core concept of a strong variable. It is worth noting, along side __weak, there is __unsafe_unretained. This acts just like a weak variable, except it isn’t automatically set to nil once the retain count reaches zero, meaning it will contain a pointer to a random part of memory (and will crash if you access it, you need to nil it yourself). The reason this exists is due to iOS 4 supporting ARC, but not __weak. In most cases, you’d use __weak.

    The above description is just a practical glance, you can read a lot more in depth using this documentation.

    Practical applications

    Everything is __strong by default. If you want weak, you need to use __weak.

    You would typically use weak variables when you conceptually don’t want to own a particular object. Whilst a car would own its engine and wheels, it wouldn’t own the driver.

    Wheel* wheel;
    Engine* engine;
    __weak Driver* driver;
    

    Conversely, a driver would own the Car.

    Car* car;
    

    If the car owned the driver, we would have a retain cycle. The car owns the driver, and the driver owns the car. If we were to release one, what would happen to the other? The whole concept of retain cycles outweighs the scope of this question, but you can read about it here.

    The same concept applies to programming patterns, for example delegates. For a table view, the view controller would own the table view, but the table view doesn’t own the view controller (Which is uses as a delegate)

    //ViewController
    UITableView* tableView;
    tableView.delegate = self;     
    
    //UITableView
    @property (nonatomic, weak) id<UITableViewDelegate> delegate;
    

    Gotchas

    One serious use of __weak is within blocks. Without them, you’re at serious risk of causing retain cycles without realising. Again, this outweighs the scope of this question, but see here for more information.

    Analogy to C++

    Within TR1 you have the ability to use shared pointers, these allow you to put a heap allocated object within a stack allocated one, and it manages the memory for us. It does this through the use of reference counting. Everytime you pass the shared pointer to another variable, the reference count is incremented. This analogises to assigning to a strong variable in Obj-C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.