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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:04:27+00:00 2026-05-28T01:04:27+00:00

I run into a fairly common scenario in Objective-C where I pass in a

  • 0

I run into a fairly common scenario in Objective-C where I pass in a variable to an init method and then want to assign it to an instance variable of the same name. However I have not found a way to scope the variables to clarify which is the value from the message argument and which is the instance variable.

Say I have some class such as the following:

@interface MyObject
{
    NSString *value;
}
- (id)initWithValue:(NSString *)value;
@end

In my implementation I want my init method to look something like this:

- (id)initWithValue:(NSString *)value
{
    self = [super init];
    if(self) {
        self.value = value;  // This will not work with instance variables
    }
}

I know of three solutions:

  1. Create a property, which allows calling self.value
  2. Rename my instance variable, such as _value
  3. Rename my init argument variable, such as initValue or argValue

I am not pleased with any of these solutions. Adding a property either makes the property publicly available on the interface or, if I use an extension, hides it from inheritors. I also do not like having different names for the variables or using an underscore, which perhaps comes from developing in other languages such as Java and C#.

Is there a way to disambiguate instance variables from message arguments? If not, is there a coding guideline in Cocoa for how to solve this problem? I like following style guidelines when appropriate.

Update

After thinking about how to do this in C, I came up with the solution of self->value. This works, but it produces a compiler warning that the Local declaration of ‘value’ hides instance variable. So this is not a satisfactory solution either since I have a zero-warning goal.

  • 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-28T01:04:27+00:00Added an answer on May 28, 2026 at 1:04 am

    For setters (and by extension initializers), I believe the convention is to prefix the parameter name with new:

    - (void)setCrunk:(Crunk *)newCrunk;
    - (id)initWithCrunk:(Crunk *)newCrunk;
    

    In general, I think the most common form I’ve seen is to call the parameter theCrunk, but Apple seems to recommend aCrunk.

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

Sidebar

Related Questions

I've been using htmldoc for a while, but I've run into some fairly serious
I'm fairly new to using MSSQL and have run into a weird problem. Given
I am writing a fairly large C++ shared-object library, and have run into a
I'm trying to write a fairly polymorphic library. I've run into a situation that's
I am writing a wordpress theme, and have run into a fairly basic problem.
We're using EF4 in a fairly large system and occasionally run into problems due
I am fairly new to the .NET scene and have run into a small
I have a fairly complicated hierarchy of types and sometimes run into hard-to-debug type
I am a fairly experienced OpenMP user, but I have just run into a
We are designing a fairly large brownfield application, and run into a bit of

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.