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

  • Home
  • SEARCH
  • 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 224721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:20:37+00:00 2026-05-11T19:20:37+00:00

Given the following definition of a class with retain properties: @interface FeedEntry : NSObject<NSCoding>

  • 0

Given the following definition of a class with retain properties:

@interface FeedEntry : NSObject<NSCoding>
{
    NSURL*  url;
    NSData* source;
}

@property (retain) NSURL*   url;
@property (retain) NSData*  source;
@end

@implementation FeedEntry

@synthesize url;
@synthesize source;

-(void)encodeWithCoder:(NSCoder*)coder
{
    [coder encodeObject:url     forKey:@"url"];
    [coder encodeObject:source  forKey:@"source"];
}

Why does the url property in initWithCoder method need the “retain”:

-(id)initWithCoder:(NSCoder*)coder
{
    url = [[coder decodeObjectForKey:@"url"] retain];
    source  = [coder decodeObjectForKey:@"source"];

    NSLog(@"got url=%@\n", url);
    return self;
}

Specifically, why doesn’t the synthesized “get url” method retain the object? (I’m guessing the source property will need a retain as well).

  • 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-05-11T19:20:37+00:00Added an answer on May 11, 2026 at 7:20 pm

    Quick answer:

    When you set:

    url = [[coder decodeObjectForKey:@"url"] retain];
    

    you are not using the @property. You are manually setting the value of the instance variable url. You must, therefore, also manually retain the value.

    To set the variable using the synthesized properties, you would instead call:

    [self setUrl:[coder decodeObjectForKey:@"url"]];
    

    or

    self.url = [coder decodeObjectForKey:@"url"];
    

    Either of these forms would make use of the synthesized methods, and handle the retain automatically.

    Details:

    In Objective-C, the @property and @synthesize keywords automatically create the getter and setter methods for you:

    @interface MyClass
    {
        id someValue;
    }
    @property (retain) id someValue;
    @end
    
    @implementation MyClass
    @synthesize someValue;
    @end
    

    Is equivalent to:

    @interface MyClass
    {
        id someValue;
    }
    - (id)someValue;
    - (void)setSomeValue:(id)newValue;
    @end
    
    @implementation MyClass
    - (id)someValue { return someValue; }
    - (void)setSomeValue:(id)newValue
    {
        [newValue retain];
        [someValue release];
        someValue = newValue;
    }
    @end
    

    This creates an important distinction between the “internal” member variable and the property having the same name. If you reference the member variable by name, you are bypassing the synthesized property methods.

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

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I suggest reading Managing Hierarchical Data in MySQL. May 11, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer I suggest that you look into the concepts of View… May 11, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer You can't publish from a Web Application, regardless of your… May 11, 2026 at 8:23 pm

Related Questions

Given the following Model, public class A { public string Name { get; set;
Given a class hierarchy where the base class defines a recursive self-type: abstract class
A little background: I'm creating a set of adapters to allow communication with mobile
If anyone can give any insight here it would be very much appreciated. I
I need some way to add a class attribute to the output of the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.