Amazon.com Widgets @property question
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 05:18:03 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Help!!!
| | |-+  @property question
Pages: [1]   Go Down
Print
Author Topic: @property question  (Read 2482 times)
esc
Global Moderator
Full Member
*****
Posts: 230






« on: February 23, 2009, 10:21:04 AM »

I can't find explanation in the book about the use of properties outside of declaring instance variables to be synthesized.  For example:

@interface ThisClass : NSObject {
    OtherClass *otherClass;
}
@property (nonatomic, retain) OtherClass *otherClass;
@property (readonly) NSDictionary *aDict;    // aDict is not an instance variable of ThisClass.
@end

Please explain the usage and pros/cons of doing this.  Thanks in advance!

Logged
skochan
Administrator
Hero Member
*****
Posts: 3104







« Reply #1 on: February 23, 2009, 11:56:08 AM »

Good question Wendy.  I think I'll include the following in the next printing/edition of my book.

Properties were added to Objective-C 2.0.  Some people like them and some people don't.  Those who defend their addition to the language regard the accessor methods that are generated almost as a "side effect" of the use of properties.

Here is a quick summary of properties to help explain their role:

  • Properties are used to declare attributes for a class' instance variables.
  • They make it clear how the instance variables are used, thereby allowing the compiler to automatically generate the accessor methods.
  • They provide a level of abstraction between the instance variables and the accessor methods (which do not have to be of the same names).
  • One property declaration for an instance variable suffices to generate both the setter and getter methods.
  • Since the accessor methods are generated there's no need to worry about what else might be taking place inside an accessor method; the role is well-defined (either fetch or set the value).
  • By using the dot operator, you can access the instance variables in a class by using a single property name,  rather than two separate method names (as in width, and setWidth:)


Cheers,


Steve

« Last Edit: February 23, 2009, 11:58:43 AM by skochan » Logged
mdeh
Full Member
***
Posts: 166






« Reply #2 on: February 23, 2009, 01:23:44 PM »


Quote
Properties are used to declare attributes for a class' instance variables.


Steve...one dumb and one new question.

Firstly, what exactly are the "attributes" of the variables?

Secondly...I often see, esp in error messages, the term "method signatures". Could you explain that.

Thank you as always
« Last Edit: February 23, 2009, 01:31:06 PM by mdeh » Logged
skochan
Administrator
Hero Member
*****
Posts: 3104







« Reply #3 on: February 23, 2009, 07:57:52 PM »

No question is "dumb".   Your specify attributes to a property after the @property declaration, inside the parentheses. 

In

Code: (Objective-C)
@property (copy, nonatomic) NSString *name;

copy and nonatomic are the attributes of the declared property.

A method signature is simply the combination of the method's name, its arguments and types, and the method's return type. 

Cheers,

Steve




Logged
mdeh
Full Member
***
Posts: 166






« Reply #4 on: February 23, 2009, 08:18:38 PM »

Thank you.
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.