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

  • Home
  • SEARCH
  • 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 8316453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:22:03+00:00 2026-06-08T21:22:03+00:00

I am trying to get more into detail with Objective-C programming language. Right now

  • 0

I am trying to get more into detail with Objective-C programming language.

Right now I have a question regarding calling [self] when setting values.

The Apple documentation says “If you do not use self., you access the instance variable directly. (…)”

So assuming I have a dog object that has an instance variable NSString *name.

I create a setter for that instance variable like this without using the [self] keyword:

(void)setName:(NSString *)_name
{
  name = _name;
}

When I alternatively create a setter WITH the [self] keyword it looks like this:

(void)setName:(NSString *)_name
{
  self->name = _name;
}

In the main-method I create a new dog object and set and return its name value:

Dog *myDog = [[Dog alloc] init];

myDog.name = @"Yoda";

NSLog(@"name of the dog: %@", myDog.name);

In both cases I get a return value of Yoda. But where is the difference between an instance variable call with and without [self] technically? Is it that I call the same variable (memory) just without using the setter method?

  • 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-08T21:22:05+00:00Added an answer on June 8, 2026 at 9:22 pm

    self is an implicit reference to the object itself, and generally you only really need to specify it when a parameter and an instance variable have the same name, for example if you had:

    (void)setName:(NSString *)name
    {
      self->name = name;    // self used here to differentiate between param and ivar
    }
    

    However, you need to be careful with your naming conventions and implementation of methods:

    1. Generally a leading underscore is used as a convention to name the instance variables, not the parameter passed to a method.
    2. In order to set the NSString * object, generally you are going to need to retain it in order to take ownership of the object and to avoid it getting released (which will cause an exception when you access it later).

    Therefore your setName method should look more like this:

    // _name is the instance variable
    - (void)setName:(NSString *)name
    {
        [name retain];
        [_name release];
        _name = name;
    }
    

    This is only true if you are using MRR, instead of ARC, but you don’t specify that so I’ll assume you are using MRR.

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

Sidebar

Related Questions

I'm just trying to get more into stl semantics, and converting old loops over
I'm currently trying to get into Java Web Development in general in Spring more
I am trying to get more adept and my C programming and I was
I have a Javascript Highchart that I'm displaying and trying to get more information
Trying to get a bit more out of the command line: I have a
I've been trying to get an app hosted on EngineYard, and have run into
Today I have been trying to get an image to save into a database,
i'm trying to get more info in the nimbuzz protocol, and possibly find any
I'm calculating pi using a long winded formula. I'm trying to get more familiar
I'm trying to get an Excel function to return more arguments than are passed

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.