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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:27:56+00:00 2026-05-21T20:27:56+00:00

I’m a bit confused when it comes to synthesize properties. Lets say that I

  • 0

I’m a bit confused when it comes to synthesize properties.

Lets say that I have this property in my *.h file.

@property (nonatomic, retain) NSString *aString;

And in my *.m file I synthesize it.
@synthesize aString;

I know that the compiler will generate setters and getters when Im using synthesize. But how does these setters and getters look like?

And when I shall give aString a value, which ways are correct?

self.aString = @"My New String"; // This will use the setter?
aString = @"My new String"; //This will not use the setter and retain the string, right?
aString = [@"My new string" retain]; //This will not use the setter, but it will retain the string ?
[self setAString:@"My new string"]; //This will also use the setter?

Thanks in advance!

  • 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-21T20:27:57+00:00Added an answer on May 21, 2026 at 8:27 pm

    When you do:

    @property (nonatomic, retain) NSString *aString;
    

    …followed by:

    @synthesize aString;
    

    …your class gets three basic things:

    1. An instance-variable called aString that you can reference directly within your class.
    2. A getter function with a signature - (NSString*) aString; that returns the instance variable.
    3. A setter function with a signature - (void) setAString: (NSString*) value; that retains the parameter that is passed in and assigns it to the instance variable (and releases any previously retained value, if one exists);

    Implementation-wise, these generated methods and fields might look like:

    //in the .h file
    @interface MyClass {
        NSString* aString;
    }
    
    - (NSString*) aString;
    - (void) setAString: (NSString*) value;
    
    @end
    
    
    //in the .m file
    @implementation MyClass
    
    - (NSString*) aString {
        return aString;
    }
    
    - (void) setAString: (NSString*) value {
        if (aString != value) {
            [aString release];
            aString = [value retain];
        }
    }
    
    @end
    

    So to answer the rest of your questions:

    self.aString = @"My New String"; // This will use the setter?  Yes, and it will retain the string.
    aString = @"My new String"; //This will not use the setter and retain the string, right?  Right.
    aString = [@"My new string" retain]; //This will not use the setter, but it will retain the string?  Right again.
    [self setAString:@"My new string"]; //This will also use the setter?  Yep.
    

    As for which are correct, they all are, depending upon what you want and assuming that you understand how the behavior differs between using each one, particularly with respect to releasing/retaining property values. For instance, this code will leak memory:

    self.aString = @"Value 1";  //go through the setter, which retains the string
    aString = @"Value 2";        //bypass the setter, the 'Value 1' string will not be released
    

    …and this equivalent code will not:

    self.aString = @"Value 1";  //go through the setter, which retains the string
    self.aString = @"Value 2";  //go through the setter again to release the first string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a reasonable size flat file database of text documents mostly saved in
I know there's a lot of other questions out there that deal with this

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.