Questions tagged [declared-property]

In Objective-C, declared properties are a convenient way to replace the declaration and manual implementation of accessor methods for objects.

A property declaration begins with the keyword @property. You can decorate a property with attributes by using the form @property(attribute [, attribute2, ...]).

You use the @synthesize directive to tell the compiler that it should synthesize the setter and/or getter methods for a property if you do not supply them within the @implementation block. The @synthesize directive also synthesizes an appropriate instance variable if it is not otherwise declared.

Declared properties provide a useful simplification of the usual requirement to explicitly declare all accessor methods, in the following ways (excerpt from Apple documentation on Declared Properties):

  • The property declaration provides a clear, explicit specification of how the accessor methods behave.
  • The compiler can synthesize accessor methods for you, according to the specification you provide in the declaration.
  • Properties are represented syntactically as identifiers and are scoped, so the compiler can detect use of undeclared properties.

Questions relating to the proper use of @property declarations, @synthesize, and the use of dot notation to assign values to these properties (object.propertyName = propertyValue;) should use this tag.

Further reading:

  1. The Objective-C Programming Language: Declared Properties
78 questions
7
votes
2 answers

Does `nonatomic` makes sense in a `readonly` declared property (including class property)?

EDIT: This question applies to normal declared properties as well (not only to class properties)! Original Post: Lets say I have the public class method sharedInstance which is currently implemented as a getter method: @interface MyClass +…
Buju
  • 1,546
  • 3
  • 16
  • 27
7
votes
1 answer

Why do I have to declare a property specified by a protocol in the header, and not the class extension (implementation)

So I have a protocol, which requires a property to be declared: @protocol MyProtocol @property MyView *myView; @end and an object who conforms to it: @interface MyViewController : NSViewController @end However, if I declare…
A O
  • 5,516
  • 3
  • 33
  • 68
7
votes
4 answers

Get the address of an Objective-c property (which is a C struct)

I have an Objective-C class which contains a C-style struct. I need to call a C function passing a pointer to this object member (a.k.a. property). For the life of me, I can't figure out how to get the address of this C struct. Using the…
jDawg
6
votes
1 answer

Is there a pattern to override a property?

The Objective-C runtime keeps a list of declared properties as meta-data with a Class object. The meta-data includes property name, type, and attributes. The runtime library also provides a couple of functions to retrieve these information. It means…
5
votes
2 answers

How to resolve property getter/setter method selector using runtime reflection in Objective-C? (or reverse)

Objective-C offers runtime reflections feature. I'm trying to find getter/setter selector name of a declared property. I know the basic rule like field/setField:. Anyway I think runtime reflection should offer a feature to resolve the name for…
eonil
  • 83,476
  • 81
  • 317
  • 516
5
votes
6 answers

self.variable and variable difference

What is the difference between self.myVariable = obj; and myVariable = obj;, when I use @propery/@synthesize to create `myVariable?
Shyne
  • 861
  • 3
  • 13
  • 18
5
votes
1 answer

Can the memory management of a property change if it's redefined in a class extension?

If I have a property like this: //test.h @interface test @property (nonatomic, readonly, weak) NSObject x; @end redefined in the implementation file to be read/write: // test.m @interface test () @property (nonatomic, readwrite) NSObject…
Wingzero
  • 9,644
  • 10
  • 39
  • 80
5
votes
2 answers

Trying to use copied NSMutableString property causes an exception

I started a small Xcode project to investigate whether an NSMutableString property should be copy or retain. I declared my property with the copy attribute: @property (nonatomic,copy) NSMutableString *stringA; Then initialized it as self.stringA =…
rustylepord
  • 5,681
  • 6
  • 36
  • 49
4
votes
3 answers

Is there a difference between setting a property with the dot or the bracket syntax?

Given the property declaration below, does method (A) work in exactly the same way as method (B)? I just want to check that self.yellowViewController = yellcon_New; is going via my setter, so that the old objects gets released and the new one…
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
4
votes
1 answer

Why would I create a property of a superclass only to use the subclass

I'm following the Stanford online course Developing iOS 7 Apps for iPhone and iPad (link to course in itunes U). The first assignment asks the students to create some classes(Card, PlayingCard, Deck, PlayingCardDeck) detailed in the notes and update…
4
votes
1 answer

Am I missing any points in my argument in favor of atomic properties?

I read this question (and several others): What's the difference between the atomic and nonatomic attributes? I fully understand (at least I hope so :-D ) how the atomic/nonatomic specifier for properties works: Atomic guarantees that a "read"…
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
4
votes
1 answer

Using performSelector: to access BOOL property

I am using performSelector:, which returns an id object, to call several other methods. The return type of those methods can actually be either be a BOOL, int, NSDate or any other kind of object. How can I figure out whether the object returned from…
Se7enDays
  • 2,543
  • 2
  • 17
  • 22
4
votes
2 answers

Why does Xcode automatically create variables with underscores?

Why in the newest version of Xcode (dp-4) are variables declared with retain,nonatomic made to use the underscore before the variable name? Does this create some sort of type safety? For example, I create a property: @property (retain, nonatomic)…
GoGauchos
  • 588
  • 5
  • 11
4
votes
3 answers

Will the compiler auto-synthesize an ivar for a property declared in a category?

Before so-called "Modern Objective-C", when creating a new property in category, we needed to implement setter and getter methods. Now, we don't have to do @synthesize; the compiler will automatically create the methods and an instance variable. But…
IPaPa
  • 368
  • 2
  • 12
4
votes
2 answers

How does the new Apple LLVM 4.0 "default synthesize" feature operate?

I was going through the release notes for Xcode 4.4 and noticed this: LLVM 4.0 Compiler Xcode now includes the Apple LLVM Compiler version 4.0, including the following newObjective-C language features: Default @synthesize: automatically…
booker
  • 1,256
  • 1
  • 12
  • 18