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
3
votes
1 answer

Why doesn't this UIView get added as a subview under certain circumstances, but does under others?

I have a UIView viewForRootVc that is a declared property for a UIView subclass NewView. It's NewView's responsibility to initialize viewForRootVc, but then a subclass NewViewSubClass sets its background color. Once this is all done, the root view…
maxedison
  • 17,243
  • 14
  • 67
  • 114
3
votes
1 answer

Checking whether an ObjC property is nullable at runtime

I'm trying to figure out at runtime whether a property of a class is nullable. For example: @interface A : NSObject + (NSSet *)nullableProperties; @property (readonly, nonatomic) NSString *description; @property (readonly, nonatomic,…
byohay
  • 272
  • 4
  • 11
3
votes
2 answers

Setting default values for inherited property without using accessor

I always see people debating whether or not to use a property's setter in the -init method. My problem is how to create a default value in a subclass for an inherited property. Say we have a class called NSLawyer -- a framework class, that I can't…
Ben Stock
  • 1,986
  • 1
  • 22
  • 30
3
votes
4 answers

Should a BOOL ivar be a pointer to allow another class to set it?

My class has a BOOL property that needs to be set by another class, so I am trying to use a pointer. I'm declaring a property for it like this: @interface SomeClass : SuperClass { BOOL *_shared; } @property(nonatomic) BOOL *shared; Is this the…
Ser Pounce
  • 14,196
  • 18
  • 84
  • 169
3
votes
3 answers

Compiler error "use of undeclared identifier" when I remove my @synthesize statements

With the latest LLVM build, the requirement for synthesizing properties has been removed. Therefore I was able to remove all my @synthesize statements except for the ones for NSFetchedResultsController. Does anyone know why the compiler is warning…
user594161
3
votes
3 answers

How to add some code to synthesized getter/setter while keeping synthesized code

I need to, for example, execute NSLog(@"Executed.") every time my synthesized getter or setter gets called. I see 2 ways to do that: Find some snippets that work probably like synthesized ones. This thread may help in that. Use KVO: add some…
folex
  • 5,122
  • 1
  • 28
  • 48
2
votes
3 answers

Do properties default to nil?

If i don't use an ivar for properties, but do this: @interface someClass : NSObject @property (nonatomic, retain) NSArray * someArray; @end @implementation someClass @synthesize someArray = _someArray; - (void) someMethod { if(…
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
2
votes
1 answer

Does key-value coding support declared property's custom accessor name?

Key-Value coding of Cocoa makes get/set operation to properties simple. Anyway documentation says it only recognized pre-defined naming patterns. I think Declared Property could be supported by this feature. Actually basic access of Declared…
eonil
  • 83,476
  • 81
  • 317
  • 516
2
votes
1 answer

What do non-ARC Objective-C property accessors look like?

I want to know how the getter and setter for an Objective-C property are implemented as part of learning the concept of memory management. I have not been able to find an actual representation other than "nonatomic" and "atomic". What does the…
NSUser
  • 1,233
  • 9
  • 15
2
votes
1 answer

If a property is defined as copy, do I still need to do a copy when assigning to the ivar?

Suppose I have a property defined as @property (copy) NSString *name. Suppose I have a init method defined as below. -(instancetype) initWithName:(NSString *)name { self = [super init]; if (self) { _name = [name copy]; //Do I need…
2
votes
3 answers

What does "back" mean in the context of Objective-C declared properties?

Reading some Objective-C manuals about properties and instance variables, I came across a lot of sentences like a readwrite property will be backed by an instance variable. or Properties are typically backed by an instance variable with a…
2
votes
1 answer

Creating an object and setting a property in one line or two

I want to set a property but I'm not sure what is the best way to do it. I can think of two ways to do this. By creating an object, storing it in a variable, and setting the property: PropertyClass *myProperty = [[PropertyClass alloc]…
Justin Moser
  • 2,005
  • 3
  • 22
  • 28
2
votes
1 answer

Why does Apple use assign rather than weak to store a delegate?

Some Cocoa and Cocoa Touch classes declare their delegate properties as assign rather than weak, which forces users of the class to nil out the property in dealloc -(void)dealloc { self.imageScrollView.delegate = nil; self.tableView.delegate…
user4951
  • 32,206
  • 53
  • 172
  • 282
2
votes
2 answers

When to use strong or weak for properties

I have a table view as an IBOutlet, and by default XCode sets its property to be strong rather than weak. Sometimes I get a "recieved memory warning" message. So I tried to change many properties from strong to weak, but it doesn't seem to affect…
1
vote
3 answers

Why does the property declaration set a boolean attribute's type to NSNumber when having Xcode automatically generate managed object class files?

When I have Xcode automatically create the class files for the entities in the data model, any attributes I've specified as Boolean get a type of NSNumber in the property declaration: @property (nonatomic, retain) NSNumber * isGood; Since I'm using…
maxedison
  • 17,243
  • 14
  • 67
  • 114