Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

LoveMyMac2004

macrumors newbie
Original poster
Aug 18, 2008
13
0
Myrtle Beach, SC
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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];
}
 

LoveMyMac2004

macrumors newbie
Original poster
Aug 18, 2008
13
0
Myrtle Beach, SC
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).

Is there any way to use Objective-C 2.0 in XCode 2.5?

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];
}

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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Is there any way to use Objective-C 2.0 in XCode 2.5?

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).

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

Thanks again
-Don

The book is right. I'd simply replace retain with copy. You can see how Apple implement copy here.
 

LoveMyMac2004

macrumors newbie
Original poster
Aug 18, 2008
13
0
Myrtle Beach, SC
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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
The book is right. I'd simply replace retain with copy. You can see how Apple implement copy here.

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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.

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.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
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];
 

Bakerman

macrumors member
Jan 31, 2005
62
7
Sweden
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.