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
293
votes
4 answers

Objective-C declared @property attributes (nonatomic, copy, strong, weak)

Can someone explain to me in detail when I must use each attribute: nonatomic, copy, strong, weak, and so on, for a declared property, and explain what each does? Some sort of example would be great also. I am using ARC.
Gaurav_soni
  • 6,064
  • 8
  • 32
  • 49
103
votes
6 answers

Do declared properties require a corresponding instance variable?

Do properties in Objective-C 2.0 require a corresponding instance variable to be declared? For example, I'm used to doing something like this: MyObject.h @interface MyObject : NSObject { NSString *name; } @property (nonatomic, retain) NSString…
indragie
  • 18,002
  • 16
  • 95
  • 164
62
votes
4 answers

Should an NSString property under ARC be strong or copy?

When not compiling with ARC, it is recommended to use copy properties for data types such as NSString. I could not find proper documentation on the use of copy in ARC mode. Can someone tell me what's applicable for ARC?
35
votes
7 answers

Expose a private Objective-C method or property to subclasses

According to some official talk, a class in Objective-C should only expose public methods and properties in its header: @interface MyClass : NSObject @property (nonatomic, strong) MyPublicObject *publicObject; - (void)publicMethod; @end and…
hzxu
  • 5,753
  • 11
  • 60
  • 95
31
votes
4 answers

myView.frame.origin.x = value; does not work - But why?

I know that I can't use this: myView.frame.origin.x = 25.0; and that I have to use this instead: CGRect myFrame = myView.frame; myFrame.origin.x = 25.0; myView.frame = myFrame; And I'm doing it all the time, but I don't know why I must do it that…
Allisone
  • 8,434
  • 4
  • 32
  • 54
25
votes
10 answers

Get property name as a string

I need a way to pass a property and get the name assigned to it. Any suggestions? @property (nonatomic, retain) MyObject *crazyObject; NSString *str = SOME_WAY_TO_GET_PROPERTY_NAME(crazyObject); // Above method should return @"crazyObject"
aryaxt
  • 76,198
  • 92
  • 293
  • 442
25
votes
2 answers

Access Objective-C property dynamically using the name of the property

I know the string name of a property of an object. How would I go about getting and setting that property using the string?
quano
  • 18,812
  • 25
  • 97
  • 108
24
votes
2 answers

Why does a declared property use both retain and readonly?

I've noticed that some of Apple's examples include both a retain and readonly modifier on properties. What's the point of including retain if no setter gets generated when we're using the readonly modifier? Example: @property (retain, readonly)…
Matt
  • 1,710
  • 3
  • 14
  • 15
22
votes
3 answers

Does @synchronized(self) create a block where the self prefix is unecessary on properties?

I have read something in some foreign code and I want to check my assumption: @synchronized(self) is used to get rid of the self prefix when setting a property. So in my example below, I'm setting the strText of the instance, not just a local…
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55
13
votes
1 answer

Declare properties in .h interface or in an extension in .m file?

In Objective-C, is it best practice to: Declare objects such as buttons in the .h and then synthesize in the .m .h @interface SomeViewController : UIViewController @property (strong, nonatomic) UIButton *someButton; …
HighFlyingFantasy
  • 3,789
  • 2
  • 26
  • 38
10
votes
1 answer

Does an Objective-C readonly property need to specify strong or copy?

If I have a read-only string property, is it necessary to specify strong (or retain) or copy in the declaration? If I don't specify, is one of them assumed? It seems to me the ownership attribute is only useful when you have a setter. @property…
Boon
  • 40,656
  • 60
  • 209
  • 315
10
votes
5 answers

Protect from adding object to NSMutableArray in public interface

I want to protect access to NSMutableArray in public interface I am trying to do this by defining property as NSArray in public interface and as NSMutableArray in private interface like this: @interface Order : NSObject @property (readonly, strong,…
Vladimir
  • 4,782
  • 7
  • 35
  • 56
8
votes
2 answers

Assignment to ivar in a Block via weak pointer

I have a read-only property isFinished in my interface file: typedef void (^MyFinishedBlock)(BOOL success, NSError *e); @interface TMSyncBase : NSObject { BOOL isFinished_; } @property (nonatomic, readonly) BOOL isFinished; and I want to set…
7
votes
2 answers

Why is [foo view] behaving differently than foo.view in my code?

Purely by accident I discovered that calling [bar.view addSubview:[foo view]] doesn't work, but [bar.view addSubview:foo.view] does in the following code. foo=[fooViewController alloc] initWithNibName:@"fooViewController" andBundle:nil]; [self.view…
RonLugge
  • 5,086
  • 5
  • 33
  • 61
7
votes
3 answers

Difference between self.var and simply var

What is the difference between using self.var vs. just var in an Objective-C class? Are there benefits or dangers to one or the other?
1
2 3 4 5 6