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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:43:27+00:00 2026-05-14T18:43:27+00:00

Update : Unfortunately the help offered below did not solve my problem of sharing

  • 0

Update: Unfortunately the help offered below did not solve my problem of sharing a class property across functions. Can anyone else suggest a possible problem? Here’s the latest code:

Header .h:

@interface FirstViewController:UIViewController <UITableViewDataSource, UITableViewDelegate, UITabBarControllerDelegate> {
 NSDictionary *sectorDictionary;
 NSInteger sectorCount;
}

@property (nonatomic, retain)  NSDictionary *sectorDictionary;

- (id)initWithData:(NSMutableDictionary*)inData;

Implementation .m:

@synthesize sectorDictionary;

- (id) testFunction:(NSDictionary*)dictionary {
 NSLog(@"Count #1: %d", [dictionary count]);
 return nil;
}

- (id)initWithData:(NSMutableDictionary *)inData {
 self = [self init];
 if (self) {
    [self testFunction:inData];
    // set the retained property
    self.sectorDictionary = inData;
  }

 return self;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 NSLog(@"Count #2: %d", [self.sectorDictionaryCopy count]);
 return [self.sectorDictionaryCopy count];
}

Console output:

2010-05-05 20:11:13.126 JSONApp[17378:207] Count #1: 9
2010-05-05 20:11:13.130 JSONApp[17378:207] Count #2: 0

Following up on this question about sharing an object between classes, I now need to figure out how to share that object across various functions in a class.

First, the setup: In my App Delegate I load menu information from JSON into a NSMutableDictionary and message that through to a view controller using a function called initWithData. I need to use this dictionary to populate a new Table View, which has methods like numberOfRowsInSection and cellForRowAtIndexPath.

I’d like to use the dictionary count to return numberOfRowsInSection and info in the dictionary to populate each cell. Unfortunately, my code never gets beyond the init stage and the dictionary is empty so numberOfRowsInSection always returns zero.

I thought I could create a class property, synthesize it and then set it. But it doesn’t seem to want to retain the property’s value. What am I doing wrong here?

In the header .h:

@interface FirstViewController:UIViewController <UITableViewDataSource, UITableViewDelegate, UITabBarControllerDelegate> {
    NSMutableDictionary *sectorDictionary;
    NSInteger sectorCount;
}

@property (nonatomic, retain)  NSMutableDictionary *sectorDictionary;

- (id)initWithData:(NSMutableDictionary*)data;

@end

in the implementation .m:

@synthesize sectorDictionary;

- (id) testFunction:(NSMutableDictionary*)dictionary {
    NSLog(@"Count #1: %d", [dictionary count]);    
    return nil;
}

- (id)initWithData:(NSMutableDictionary *)data {
    if (!(self=[super init])) {
        return nil;
    }
    [self testFunction:data];

    // this is where I'd like to set a retained property
    self.sectorDictionary = data;

    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"Count #2: %d", [self.sectorDictionary count]);
    return [self.sectorDictionary count];
}

Output from NSLog:

2010-05-04 23:00:06.255 JSONApp[15890:207] Count #1: 9
2010-05-04 23:00:06.259 JSONApp[15890:207] Count #2: 0
  • 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-14T18:43:28+00:00Added an answer on May 14, 2026 at 6:43 pm

    Any init function should return self if it completes successfully. Change the return nil to return self at the end of initWithData.

    -(id) initWithData:(NSMutableDictionary *)inData {
        self = [self init]; // best practice use super for method with same name
        if ( self ) {
            self.sectorDictionary = inData;
        }
        return self;
    }
    

    The sectorDictionary member should not be mutable. If it changes, you need to call reloadData on the table. Your setter function should make an immutable copy.

    -(void) setSectorDictionary:(NSDictionary *)inData {
        NSDictionary *old = sectorDictionary;
        sectorDicitonary = inData ? [[NSDictionary alloc] intWithDictionary:inData] : nil;
        [self.table reloadData];
        [old release];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.