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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:50:35+00:00 2026-05-24T11:50:35+00:00

I recently started to develop an iPhone App. Coming from C#, Objective-C has some

  • 0

I recently started to develop an iPhone App. Coming from C#, Objective-C has some traps for me. I dont’t understand what happened in the following snippet:

@interface RootViewController : UITableViewController {
    NSString *simpleProperty;
    NSString *propertyWithUnderscoreIvar;
}

@property (nonatomic, retain) NSString *simpleProperty;
@property (nonatomic, retain) NSString *propertyWithUnderscoreIvar;

@end

@implementation RootViewController

@synthesize simpleProperty;
@synthesize propertyWithUnderscoreIvar = _propertyWithUnderscoreIvar;

- (NSString *)simpleProperty {
    return @"Simple property value";
}
- (NSString *)propertyWithUnderscoreIvar {
    return @"Property with underscore value";
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog([NSString stringWithFormat:@"%i %@", 1, simpleProperty]);
    // --> 1 (null)

    NSLog([NSString stringWithFormat:@"%i %@", 2, propertyWithUnderscoreIvar]);
    // --> 2 (null)

    NSLog([NSString stringWithFormat:@"%i %@", 3, _propertyWithUnderscoreIvar]);
    // --> 3 (null)

    NSLog([NSString stringWithFormat:@"%i %@", 4, self.simpleProperty]);
    // --> 4 Simple property value

    NSLog([NSString stringWithFormat:@"%i %@", 5, self.propertyWithUnderscoreIvar]);
    // --> 5 Property with underscore value
}

Why are the first three outputs null? Is my own implementation for the properties incorrect?

  • 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-24T11:50:36+00:00Added an answer on May 24, 2026 at 11:50 am

    In your viewDidLoad method you are logging the values of your instance variables (ivars), not your properties. From your code sample above:

    @interface RootViewController : UITableViewController {
         NSString *simpleProperty;
         NSString *propertyWithUnderscoreIvar;
    }
    

    This declares two variables in your class – simpleProperty and propertyWithUnderscoreIvar. The following code, on the other hand, declares the properties:

    @property (nonatomic, retain) NSString *simpleProperty;
    @property (nonatomic, retain) NSString *propertyWithUnderscoreIvar;
    

    These are declarations only. Objective C is somewhat similar to C#, in that it provides you with easy ways to generate getter’s and setters for your class properties. In Objective-C this is done via the @synthesize keyword (which is roughly analogous to C# automatic properties).

    @synthesize simpleProperty;
    @synthesize propertyWithUnderscoreIvar = _propertyWithUnderscoreIvar;
    

    Those @synthesize keywords in your implementation file create getter and setter methods for you, for your properties. Your first synthesize looks good, it will create a getter and setter for ‘simpleProperty’, backed by the instance variable of the same name. Your second @synthesize is hokey though. It will create a getter and setter for ‘propertyWithUnderscoreIvar’, backed by the instance variable ‘_propertyWithUnderscoreIvar’, which you never declared. This code will work on modern runtimes but not legacy ones (note that even on modern runtimes, your ‘propertyWithUnderscoreIvar’ ivar will be ignored by the @synthesize).

    Now as to why your code is printing nulls, in your logging code you do this:

    NSLog([NSString stringWithFormat:@"%i %@", 1, simpleProperty]);
    

    This is accessing the instance variable directly. But you haven’t set the instance variable to any value at this point. What you really meant to do is access the property, like so:

    NSLog([NSString stringWithFormat:@"%i %@", 1, [self simpleProperty]);
    

    Using [self simpleProperty] instead will invoke the method simpleProperty and return your hard coded value, which is what you’re trying to do.

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

Sidebar

Related Questions

We recently started to develop a Java desktop app and management has requested that
I have recently started to develop applications for iPhone with MonoTouch and have to
I've recently started on a project to develop some portlets which will run on
I have recently started learning C++ and coming from a Ruby environment I have
Recently I've started to develop Android Soft Keyboard and got some problem with preferences.
I've started Android programming recently so bear with me :) I develop an app
I develop mostly for iPhone and have started very recently with Android. Tried the
When I started to develop with .NET under VS2008 recently, I was very happy
I recently started learning Emacs . I went through the tutorial, read some introductory
I recently started using Git as my version control system for some Cocoa projects

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.