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 6068323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:42:11+00:00 2026-05-23T09:42:11+00:00

I have an Objective-C class that implements a node for a data tree. Its

  • 0

I have an Objective-C class that implements a node for a data tree. Its properties are read-only to the public while a private extension of the class (not shown here) implements the properties’ setters so a manager class can create the tree.

// Interface
@interface DataSet : NSObject {
    NSString        *name;
    NSString        *data;
@private
    DataSet         *parent;
    NSMutableArray  *children;
}
@property (nonatomic, readonly, copy) NSString *name;
@property (nonatomic, readonly, copy) NSString *data;

I want to implement a custom getter for one of the properties that, if the property is nil, will walk up the tree until it finds an ancestor node that has a non-nil value for that property.

My problem is implementing the getter without causing an infinite recursion of the getter calling itself.

// Implementation
@interface DataSet ()
@property (nonatomic, retain) DataSet           *parent;
@property (nonatomic, retain) NSMutableArray    *children;
@end

@implementation DataSet

@synthesize name;
// do not @synthesize data
@synthesize parent, children;

// custom getter walks up tree to find first non-nil 'data' property
- (NSString*) data {
    NSString *result = nil;
    DataSet *set = self;
    while (set != nil && result == nil) {
        result = [set data];    // <=== INFINITE RECURSION HERE
        set = set.parent;
    }
    return result;
}

I’ve searched through this and other forums but haven’t found any examples of what I’m trying to do here. Anyone have any suggestions?

Also, should the last line in the getter be

return [result copy];

?

  • 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-23T09:42:11+00:00Added an answer on May 23, 2026 at 9:42 am

    Hmmm, I think you want something like this:

    -(NSString *) data {
        // Determine result from current instance data.
        NSString *result = ....; 
    
        // If nothing, ask parent instance of this instance.
        if (result == nil) {
            result = [parent data];
        }
    
        // Might still be nil if parent returns nothing.
        return result;
    }
    

    Hmmm, actually seeing as you have a data variable containing some textural data, it could be done like this:

    -(NSString *) data {
        // If data is nil, ask parent instance for a value, otherwise return a copy.
        return data == nil ? [parent data] : [data copy];
    }
    

    So each instance of DataSet doesn’t need to have a loop. All they do is check with their immediate parent. This way if you have a data graph of A -> B -> C -> D and execute [D data]; the D will check itself and then as C, which will check itself and then as B, which will check itself and then ask A. You will get back the first successful non-nil value for result.

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

Sidebar

Related Questions

While attempting my first sub-class in Objective-C I have come across the following warning
so in Objective-C I can have a reference to object that implements certain protocol
I have a class that implements IAuthorizationPolicy. I set up a custom Principal object
I'm relatively new to the world of Objective-C and have a class that I've
I have the following objective C class. It is to store information on a
Say I have the following Objective-C class: @interface Foo { int someNumber; NSString *someString;
I have a peculiar problem. I created a normal Objective C class called SampleTable.
If subclass in objective-c wants to override a super class's method, does it have
Given I have a class with two constructors: public class TestClass { ObjectOne o1;
I have some Objective-C code that looks like this: #define myVar 10 float f

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.