9 Mar 2010

Objective-C Puzzle

Posted by admin

A blog entry with a gracious nod to Gilad and Josh’s series of Java puzzlers.

Why does this code excerpt produce different results in the simulator vs running the app on the iPhone itself?

In my AllRides class which contains the information about all the club’s bike rides I have the initialization method below. In the ViewController for the preferences screen I then access and display the AllRides.club property.

AllRides.m:

- init {

if (self = [super init]) {

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

club = (NSString *)[userDefaults objectForKey:kClub];

if (club == nil) {

club = kDefaultClub;

}

}

return self;

}

Preferences.m (in – (void) viewDidLoad):

club.text = [[AllRides sharedRides] club];

When running in the simulator all appears well. The club name nicely appears:

When running the app on the iPhone the field is empty:

Changing the AllRides init method explicitly setting self.club makes both runtime environments behave the same expected way:

- init {

if (self = [super init]) {

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

self.club = (NSString *)[userDefaults objectForKey:kClub];

if (self.club == nil) {

self.club = kDefaultClub;

}

}

return self;

}

Suggestions? Answers?
I guess the underlying question is what the subtle differences are between club and self.club.

Subscribe to Comments

8 Responses to “Objective-C Puzzle”

  1. Where do you Alloc the AllRides class?
    In Preferences.m the Class is accessed, not an Instance. Isn’t that strange? Singleton-ish behavior?

    (that does not answer your question, not yet)

     

    Leon Wubbe

  2. It’s allocated when the main viewcontroller is instantiated. AllRides is a singleton indeed.

     

    Onno

  3. Assumption:

    self.club accesses the instance variable directly
    club (or [self club]) uses the accessor methods of this property

    How is this property declared in AllRides.h?
    assign or retain or copy?

     

    Leon Wubbe

  4. retain

     

    admin

  5. retain is OK (afaik)

    How can we test if my assumption is right?
    add readonly to the declaration of the property and try to change the value, using both ways.

     

    Leon Wubbe

  6. Found on an iPhone forum:

    The following statements do exactly the same: The property ‘value’ is accessed as a property (using getter/setter)

    self.value = newValue;
    [self setValue:newValue];

    The following statement doesn’t use getter/setter: The property ‘value’ is accessed directlty

    value = newValue;

     

    Leon Wubbe

  7. And that is causing the difference. It is odd though that it leads to different behavior in the simulator vs the device.

     

    Onno

  8. I read in an iPhone Blog that one should never use self.property or [self property], just property is the best.
    Reason: No unwanted side effects (not specified)

    That is exactly the opposite of what you have discovered…

     

    Leon Wubbe

  • Browse

    or
  • Let’s Connect

    Email:
    onno@onno-consulting.com
    AIM:
    onnoweb
    Phone:
    +1 585 733 5130
  • Twitter

  • Software

    Club Rides - keeping track of your cycling club's activities

    GPX2ProfileImage - elevation profiles from GPX files

  • Social Homes

    LinkedIn  Twitter  Naymz  
  • Recent Posts

  • Tag Cloud

  • Calendar

    March 2010
    M T W T F S S
    « Feb   Apr »
    1234567
    891011121314
    15161718192021
    22232425262728
    293031  
  • Meta