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 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

Related Questions

given the following class definition: public class Order { public IProduct Product {get;set;} }
Given the following JavaScript class definition: var Quota = function(totalMinutes){ this.totalMinutes = parseInt(totalMinutes ||
Given a class with the following definition: public class MyTestClass { public int ValueA
Given the following class definitions: public class BaseClass { public string SomeProp1 { get;
Given a generic class definition like public class ConstrainedNumber<T> : IEquatable<ConstrainedNumber<T>>, IEquatable<T>, IComparable<ConstrainedNumber<T>>, IComparable<T>,
Given the following code class T { public: virtual ~T () {} virtual void
Given the following Model, public class A { public string Name { get; set;
Given the following module: class Dummy(dict): def __init__(self, data): for key, value in data.iteritems():
I have the following class definition, written in C++, residing in it's own header
I have the following class definition and it needs a copy constructor so deep

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.