代码之家  ›  专栏  ›  技术社区  ›  buley

如何在Objective-C中跨多个函数获取和设置类属性?

  •  0
  • buley  · 技术社区  · 16 年前

    更新 :不幸的是,下面提供的帮助没有解决我在函数间共享类属性的问题。其他人能提出一个可能的问题吗?以下是最新代码:

    标题h:

    @interface FirstViewController:UIViewController <UITableViewDataSource, UITableViewDelegate, UITabBarControllerDelegate> {
     NSDictionary *sectorDictionary;
     NSInteger sectorCount;
    }
    
    @property (nonatomic, retain)  NSDictionary *sectorDictionary;
    
    - (id)initWithData:(NSMutableDictionary*)inData;
    

    @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];
    }
    

    控制台输出:

    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
    

    跟进 this question 关于在类之间共享对象,我现在需要弄清楚如何在类中的各种函数之间共享该对象。

    首先,设置:在我的App Delegate中,我将菜单信息从JSON加载到NSMutableDictionary中,并使用名为initWithData的函数将消息传递到视图控制器。我需要使用这个字典来填充一个新的表视图,它有numberOfRowsInSection和cellforrowatinexpath这样的方法。

    我想我可以创建一个类属性,综合它,然后设置它。但它似乎不想保留房产的价值。我做错什么了?

    在标题中。h:

    @interface FirstViewController:UIViewController <UITableViewDataSource, UITableViewDelegate, UITabBarControllerDelegate> {
        NSMutableDictionary *sectorDictionary;
        NSInteger sectorCount;
    }
    
    @property (nonatomic, retain)  NSMutableDictionary *sectorDictionary;
    
    - (id)initWithData:(NSMutableDictionary*)data;
    
    @end
    

    在实施中。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];
    }
    

    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
    
    3 回复  |  直到 8 年前
        1
  •  0
  •   drawnonward    16 年前

    如果任何init函数成功完成,它都应该返回self。将initWithData末尾的return nil改为return self。

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

    sectorDictionary成员不应是可变的。如果它改变了,您需要调用表上的reloadData。setter函数应该生成一个不可变的副本。

    -(void) setSectorDictionary:(NSDictionary *)inData {
        NSDictionary *old = sectorDictionary;
        sectorDicitonary = inData ? [[NSDictionary alloc] intWithDictionary:inData] : nil;
        [self.table reloadData];
        [old release];
    }
    
        2
  •  0
  •   user331951    16 年前

    我相信你的问题在底线之内 self.sectorDictionary = data

    首先需要为正在创建的字典分配一些内存,因此 self.sectorDictionary = [[NSMutableDictionary alloc] init];

    启动新词典后,需要用传入词典的内容填充它。

    所以。。。

      self.sectorDictionary = [[NSMutableDictionary alloc] initWithDictionary:data];
    

    而不是:

    self.sectorDictionary = data;
    
        3
  •  -1
  •   Mihir Mehta    16 年前

     self.sectorDictionary = data;
    

    self.sectorDictionary = [[NSMutableDictionary alloc] init];
    self.sectorDictionary = (NSMutableDictionary *) data;