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 3460070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:11:07+00:00 2026-05-18T10:11:07+00:00

In the Apple Documentation, it is stated that NSDate objects are immutable which I

  • 0

In the Apple Documentation, it is stated that NSDate objects are immutable which I presume means that once they are initialised with a value, they cannot/shouldn’t be changed.

However, in the following code I need an NSDate to be one of two possible values so naturally i might use:

for (class* object in array) {

        if (i == 0) {
            NSDate* fromDate = //...a date
        } else {
            NSDate* fromDate = //...a different date
        }

        //Use fromDate
        i++ 

    }

As far as I’m aware, this is not valid because using fromDate outside the if block is outside of scope.

The solution would normally be:

    for (class* object in array) {

        NSDate* fromDate = [[NSDate alloc] init];

        if (i == 0) {
            fromDate = //...a date
        } else {
            fromDate = //...a different date
        }

        //Use then release fromDate
        i++ 

    }

However, according to the Apple Docs, when an NSDate receives ‘init’ it’s initialised with todays date and since it’s immutable, I can’t reassign it.

What’s the correct thing to do here? Is it simply to copy all of my code into the if blocks twice? Or have I misunderstood the term immutable? Or perhaps I need a retain after the assignment in the first example?

thanks

  • 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-05-18T10:11:08+00:00Added an answer on May 18, 2026 at 10:11 am
    NSDate * fromDate = nil;
    if (i == 0) {
      fromDate = //...a date
    } else {
      fromDate = //...a different date
    }
    

    Your question also reveals a fundamental misunderstanding of how pointers work. Yes, the documentation says that NSDate objects are immutable. But fromDate is not an NSDate. It is an NSDate*. In other words, it is simply a forwarding address that points to where the actual NSDate object lives. The pointer itself is mutable (unless it’s declared as const), and you can feel free to change the pointer‘s value as much as you like. However, the thing that the pointer points to (the actual NSDate) is correctly immutable.

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

Sidebar

Related Questions

I have read the Apple documentation. They list the apps registered, yet in their
The Apple Developer Documentation (link is dead now) explains that if you place a
In the Apple documentation for NSRunLoop there is sample code demonstrating suspending execution while
As a Java developer who is reading Apple's Objective-C 2.0 documentation: I wonder what
Most of Apples documentation seems to avoid using autoreleased objects especially when creating gui
I'm trying to find a definitive answer (e.g. Apple developer documentation) for the maximum
Reading the Apple documentation on the UITabBar states, The most common use of a
The apple documentation made this seem like it was pretty easy but it's not
The Apple documentation gives the following warning regarding using View Controllers to manage part
I'm reading the Core Data Utility tutorial from apple documentation and there's a part

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.