Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8763195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:43:29+00:00 2026-06-13T15:43:29+00:00

I am using ARC. This is my .h file … – (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t;

  • 0

I am using ARC.

This is my .h file

...
- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
...

This is my .m file

....
@synthesize coordinate, title;

- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
{
    self = [super init];
    if (self) {
        coordinate = c;
        [self setTitle:t];
     }
    return self;
}
....
  1. Is setting coordinate this way, the right way to do it? Given that I declare it as readonly, it seems like it is the only way to do it. What if I just use the default (i.e. readwrite), in this case, should I use the setter method [self setCoordinate] instead?

  2. I could set the title by doing title = t as well. Compare to using the setter method, the result is the same, but what is the difference ?


Thanks! Wish I could accept all of your answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:43:30+00:00Added an answer on June 13, 2026 at 3:43 pm

    1: As your code is now, yes, that is the right way to do it. If you weren’t using ARC (assuming you are currently), you’d also want to retain the value to assert ownership. This will be done automatically under ARC. Keep in mind that that is not the only way of doing it; you could redeclare the property as readwrite in the class extension in the implementation file. This is a common practice which allows you to have the benefits of a readwrite property while having the property still be readonly to users of the class. Ex.

    //MyClass.h
    
    @interface MyClass : NSObject
    @property (nonatomic, strong, readonly) NSNumber* number;
    - (void) initWithNumber:(NSNumber*)number;
    @end
    
    //MyClass.m
    
    @interface MyClass ()
    @property (nonatomic, strong, readwrite) NSNumber* number;
    @end
    
    @implementation MyClass
    //this changes the instance variable backing the property to _number.
    @synthesize number = _number;
    
    - (void) initWithNumber:(NSNumber*)number{
        self = [super init];
        if (self) {
            self.number = number;
        }
        return self;
    }
    @end
    

    At the end of the day, I’d say it’s a good habit to use setters whenever you can to keep things KVO compliant and so that you always know when values change. For instance, if you have a custom UIView with a property that is reflected in its appearance, chances are you’d want to redisplay yourself when it changes. The easiest way to do this is to implement the setter yourself and call setNeedsDisplay after setting the value. You couldn’t do that if you set the instance value backing the property directly; the user of the class would have to remember to call setneedsDisplay every time they set it, manually.

    2: One goes through the setter method, giving you a way to know when a value is going to be set, while one sets a value to the instance variable backing the property. The setter method will always handle memory management in the way it was told to, while it’s up to you to do things such as copying values for a copy setter if you assign directly to an instance variable, so that you maintain some consistent scheme. Going through setters sometimes, and not others can lead to some nasty bugs if you don’t be careful. Never going through setters makes it hard to know when values change, making it near impossible to weed out invalid values. For instance, if you had an int property you wanted to limit to values in some range and someone passed in a value under the minimum limit, you’d probably want to set the property to the lowest possible value in the range. You can’t do that without the value going through the setter first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my interface (.h) file, I have @property(readonly) NSString *foo; and in my implementation
I'm having a problem with using delegate inside Blocks. With ARC on, this is
I would need some help solving this memory leak problem. I am using ARC.
I'm using ARC and getting a warning saying Capturing 'request' strongly in this block
When using ARC for iOS, is there any difference between the following? @property (strong,
I create a new project using xcode4.2, and view the AppDelegate: @property (strong, nonatomic)
Using ARC, is it now OK to assign a string value like this: self.userName
I am using ARC in my code and I am getting the error Object
for iOS 5.0 using ARC, here is something I want to implement and need
I'm in xCode 4.2 using ARC and Storyboard (iOS 5). I suppose to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.