Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 23, 2012, 09:05 AM   #1
ramy1989
macrumors newbie
 
Join Date: Nov 2012
How do I retain a readonly property?

Let's say that I jave this property:

Code:
@property(nonatomic,strong,readonly) NSMutableArray* tags;
I need to retain it only in the init method, because it's the only place where I assign it.How do I retain it then? I should write:

Code:
self.tags=[NSMutableArray new];
But this is a syntax error because there isn't any setter.How to allow the setter to operate only in the init method?
ramy1989 is offline   0 Reply With Quote
Old Dec 23, 2012, 09:30 AM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by ramy1989 View Post
Let's say that I jave this property:

Code:
@property(nonatomic,strong,readonly) NSMutableArray* tags;
I need to retain it only in the init method, because it's the only place where I assign it.How do I retain it then? I should write:

Code:
self.tags=[NSMutableArray new];
But this is a syntax error because there isn't any setter.How to allow the setter to operate only in the init method?
In the public header file, you make it readonly. In the implementation file, you make it read/write.

And please don't use "new". Use one of the convenience methods, like [NSMutableArray array].
gnasher729 is offline   0 Reply With Quote
Old Dec 23, 2012, 10:43 AM   #3
ramy1989
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Quote:
And please don't use "new". Use one of the convenience methods, like [NSMutableArray array].
What's the difference?
ramy1989 is offline   0 Reply With Quote
Old Dec 23, 2012, 10:45 AM   #4
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Less lost in L.A.
Quote:
Originally Posted by gnasher729 View Post
And please don't use "new". Use one of the convenience methods, like [NSMutableArray array].
There is nothing wrong with using "new".

----------

Quote:
Originally Posted by ramy1989 View Post
What's the difference?
new will alloc, init, and retain the object. array should not retain the object beyond the method. This is important when using manual memory management.
__________________
My App: CameraTime - Time lapse photography for novice and advanced users.

Last edited by xStep; Dec 23, 2012 at 11:03 AM.
xStep is offline   0 Reply With Quote
Old Dec 23, 2012, 11:30 AM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
I don't know that anyone answered the question. Using the . notation to access a property this is converted to [self propertyName] on an access or [self setPropertyName] on assignment. Normally using self.propertyName in methods of the class is fine, but obviously when there's no setter you can't use this syntax. When you are in methods in the object itself, propertyName = someValue should be fine. The readonly really means "other people". If you weren't using properties, and you had an instance variable, and wrote an accessor, but no mutator, you wouldn't have any problem. You'd just assign to the variable in your instance methods. The properties are just syntactic sugar for the same thing. All the modifiers just describe what getter/setter methods are generated, whether they make copies, how they manage memory, etc. You could do all of this yourself, the properties are just a shortcut.

TL;DR: Remove self. and proceed.

-Lee
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How do I retain iOS 6? snsking iOS 6 22 Apr 6, 2014 12:45 PM
how to retain unlimited? T4R06 iPhone 4 Jan 8, 2013 08:31 PM
retain UIColor IDMah iPhone/iPad Programming 11 Aug 21, 2012 04:18 PM
Retain the crispy! JMEANS Apple TV and Home Theater 7 Jul 10, 2012 04:53 PM

Forum Jump

All times are GMT -5. The time now is 02:28 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC