@property VS accessor methods: How do you implement in XCode 2.5?

Discussion in 'Mac Programming' started by LoveMyMac2004, Aug 20, 2008.

  1. LoveMyMac2004 macrumors newbie

    Joined:
    Aug 18, 2008
    Location:
    Myrtle Beach, SC
    #1
    Hi all,

    I am learning objective-c and Cocoa, but not a newbie to programming. And while I am strolling along in "Cocoa Programming for Mac OS X" and into Chapter 8: NSArrayController. The start of the chapter has you create an application that will be built on in the later chapters.

    In the code in the .h file it uses @property. I am using XCode 2.5 on a 10.4.11 system. I get a compile error, which I figure from previous experience and posted a question here, must be an Objective-C 2.0 feature only available on 10.5.

    Here is the code in the .h file:

    Code:
    @interface Person : NSObject 
    {
    	NSString *person;
    	float expectedRaise;
    }
    @property (readwrite, copy) NSString *personName;
    @property (readwrite) float expectedRaise;
    @end
    
    The .m file does not have any methods for what I am assuming are properties for personName and expectedRaise. You would access them by using the dot method (somePerson.personName etc) instead of the normal object method ([somePerson personName]).

    My question is how do I implement the functionality of @property in a "normal" non 10.5 way? Would I do it something like this in the header (is a .h file still called a header file?) file:
    Code:
    @interface Person : NSObject 
    {
    	NSString *person;
    	float expectedRaise;
    }
    - (NSString *)personName;
    - (void)setPersonName:(NSString *)thePersonName;
    - (float) expectedRaise;
    - (void)setExpectedRaise:(float)theExpectedRaise;
    @end
    
    Then in the .m file I would implement the methods of course (at least I think I would). The second part of my question is in the @property line is what does the 'copy' option do and how do I implement that?

    Thank you in advance. I am on an old PowerBook G4 and I am not going to be upgrading either my machine or OS anytime soon. So all of what I am learning must be in XCode 2.5 (the lastest version for 10.4 according to Apple).

    Well I must be learning something! :D I did complete my exercises with the apps working as stated and my machine did not crash.

    Thank you again for your help.
    -Don
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You are correct that you need to write your own methods (although it's not about the version of XCode you have, rather the version of the language: you need to be using Objective-C 2.0 for properties to work).

    Copy means that the synthesized method will take a copy of the passed value instead of retaining the passed value. So for example:

    Code:
    - (void) setAString:(NSString *) theString
    {
      if (aString != nil)
      {
        [aString release];
      }
      aString = [theString copy];
    }
    
    instead of the retain way:

    Code:
    - (void) setAString:(NSString *) theString
    {
      if (aString != nil)
      {
        [aString release];
      }
      aString = [theString retain];
    }
    
     
  3. LoveMyMac2004 thread starter macrumors newbie

    Joined:
    Aug 18, 2008
    Location:
    Myrtle Beach, SC
    #3
    Is there any way to use Objective-C 2.0 in XCode 2.5?

    In the book, the retain is used like this:
    Code:
    - (void)setAString:(NSString *)theString
    {
        [theString retain];
        [aString release];
        aString = theString;
    }
    
    This is explained that if aString and theString point to the same object, this will increase the retain count one more so that the release does not decrement it to zero and the object is destroyed before you can use it.

    How does copy come into play then? Would you simply change the retain to copy?

    Thanks again
    -Don
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Not easily. Objective-C 2.0 is provided by the gcc compiler included in later versions of XCode. The compiler itself is a separate entity so you could possible persuade it to install on OSX 10.4 but I'm not sure if you also need a newer Objective-C runtime (which is a system level component so difficult to replace).

    The book is right. I'd simply replace retain with copy. You can see how Apple implement copy here.
     
  5. LoveMyMac2004 thread starter macrumors newbie

    Joined:
    Aug 18, 2008
    Location:
    Myrtle Beach, SC
    #5
    Thank you so much. I think I heard a quote that someone said "problems sometimes can give you a better understanding" or something like that.

    It is probably better to learn the 1.0 methods for a more clear understanding. It seems that the @property is more of a 'lazy' way of doing all the accessor methods. Seems to me that it leaves out ways of modifying special case setters/getters.

    I am still learning. I know I will continue to have questions and I thank you in advance for your patience. I do know now that I am really liking Cocoa and may not return to things like REALbasic.

    Thanks again
    -Don
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    With someone just starting out, whatever object is being stored will probably implement the NSCopying protocol. However, it's important to remember that if you are composing a new class using existing ones that you've written, you need to implement NSCopying to be able to use copy on objects of a particular class.

    -Lee
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    You don't have to synthesize the accessors in the .m file. You can write them yourself if required. You can even declare different names than expected in the @property declaration.

    And Lee makes a good point: you need to implement NSCopying if you want to copy objects.
     
  8. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #8
    As an aside, even if you use @property to declare a property...

    Code:
    @property(assign) NSInteger myInteger;
    
    You can also still access it like so:

    Code:
     NSInteger value = [object myInteger];
     [object setMyInteger:value];
    
     
  9. Bakerman macrumors member

    Joined:
    Jan 31, 2005
    Location:
    Sweden
    #9
    XCode 2.5 will help you with writing those accessors; there are text macros for creating accessors, and if you use Core Data, you can create both the interface and implementation of your accessors automatically from selected properties.
     

Share This Page