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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:31:12+00:00 2026-05-16T08:31:12+00:00

Just a simple question on NSStrings, I have a piece of code that assigns

  • 0

Just a simple question on NSStrings, I have a piece of code that assigns a value to a string, depending on what is found it is either assigned a value by substringToIndex or the constant string @”0.00″, is it okay to use

// Save if value found, otherwise set to 0.00
if (parsedCharacters == nil || [parsedCharacters isEqualToString:@""])
        self.currentDiscountedPrice = @"0.00";
else
{
  // Truncate extra digits from string to 2 decimal places (find full stop, save 2 places after it)
  NSRange fullStopRange = [parsedCharacters rangeOfString:@"."];
  self.currentDiscountedPrice = [parsedCharacters substringToIndex:(fullStopRange.location + 3)];
}

for the assignment since it will release the old value & retain the new value?

There is no way of knowing whether the var was assigned the constant string or the substringToIndex returned value in the previous iteration but I was told calling retain & release on constant strings is harmless, is this true?

  • 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-16T08:31:12+00:00Added an answer on May 16, 2026 at 8:31 am

    NSString is not special; all Cocoa objects follow the Cocoa memory-management rules. As long as you do, too, you’ll be fine.

    I was told calling retain & release on constant strings is harmless, is this true?

    Yes. You should treat them the same as any other string you don’t own: Retain it if you want to own it, or make a copy and own that; then be sure to release what you own.

    … should I rather explicitly release the old value & retain the substringToIndex value or it okay to just use self.currentDiscountedPrice = for both assignments?

    You should use the property everywhere but in init methods and dealloc. Those are the only methods that should explicitly send retain and release messages to the object in the instance variable.

    The reason for both sides of that paragraph is that you, or a subclass, may implement custom accessors for the property. The custom behavior may be dangerous to have run on a half-inited or half-deallocked object, but you’ll probably want it everywhere else.

    There is no harm in passing a constant-string object to a property setter. It will retain or copy the object as normal.

    On that note, when a property’s value is an object of a class with a mutable variant (as NSString has NSMutableString), you should declare the property as copying its value (@property(copy) or @property(nonatomic, copy)), in order that you do not take co-ownership of someone else’s mutable object. If they mutate the object, this could cause problems for you, particularly if you stored the object in a hashing collection (such as as a dictionary key) rather than in an instance variable.

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

Sidebar

Related Questions

I have a simple class that essentially just holds some values. I have overridden
Just a simple question, say I have a table and one of their attributes
Simple question, hopefully an easy way and just want to verify I'm doing it
Simple question. I use Visual studio and I've just installed subversion, how can i
I've built a simple application that applies grid-lines to an image or just simple
I just want a simple tool that will help me quickly write scripts/packages that
I just want a simple SVG image that has some arbitrary text on an
Just a simple question in onSharedPreferenceChanged you can do something based on if a
Just a simple question: in a single-task system OS copies smt to memory and
Just a simple question, say I on a research team with 10 members and

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.