NSMutableDictionary 对象没有从方法中正确访问?

NSMutableDictionary objects not properly accessed from methods?

我创建了一个 NSMutableDictionary 以及一个将对象添加到字典的方法。我知道函数 addObject 正确地将对象添加到字典中,但是稍后当我尝试使用方法 Print_Object 打印对象时,属于对象的每个变量都打印为 null/0。我的代码有问题吗?

-(NSMutableDictionary *)_stock{
    if (!_stock) {
        _stock = [[NSMutableDictionary alloc]init];
    }
    return _stock;  
}

-(void) addObject:(NSString*) desc key: (NSString*) theKey floatrc: (float) retailcost floatwsc: (float) wholesalecost intnoh: (int) numonhand floatns: (int) numsold {
    object_info* obj = [[object_info alloc]init];
    [obj setDescription: desc];
    [obj setRetailCost: retailcost];
    [obj setWholeSaleCost:wholesalecost];
    [obj setNumOnHand: numonhand];
    [obj setNumItemsSold: numsold];
    [self.stock setValue:obj forKey:theKey];   
}

-(void) Print_Object:(NSString *) theKey {
    object_info* obj = [self.stock objectForKey:theKey];
    NSLog(@"Key: %@ Description: %@ Retail Cost: %f Wholesale Cost: %f Number on Hand: %i Number Sold: %i", theKey, obj.description, obj.retail_cost, obj.wholesale_cost, obj.num_on_hand, obj.num_items_sold);     
}

这是object_info的声明:

#import "object_info.h"

@implementation object_info

//Getters
-(NSString *)description {
    return description;
}


-(float) retail_cost {
    return retail_cost;
}

-(float) wholesale_cost {
    return wholesale_cost;
}

-(int) num_on_hand {
    return num_on_hand;
}

-(int) num_items_sold {
    return num_items_sold;
}

//Setters
-(void)setDescription:(NSString *) value {
    description = value;
}

-(void) setRetailCost: (float) n {
    retail_cost = n;
}

-(void) setWholeSaleCost: (float) n {
    wholesale_cost = n;
}

-(void) setNumOnHand: (int) n {
    num_on_hand = n;
}

-(void) setNumItemsSold: (int) n {
    num_items_sold = n;
}

@end

这是object_info.h:

#import <Foundation/Foundation.h>

@interface object_info : NSObject
{
    NSString *description;
    float retail_cost;
    float wholesale_cost;
    int num_on_hand;
    int num_items_sold;
}

-(NSString *)description;
-(void)setDescription:(NSString *)value;

-(float) retail_cost;
-(void) setRetailCost:(float) n;

-(float) wholesale_cost;
-(void) setWholeSaleCost:(float) n;

-(int) num_on_hand;
-(void) setNumOnHand: (int) n;

-(int) num_items_sold;
-(void) setNumItemsSold:(int) n;

@end

你能给我们看看stock的声明吗?我怀疑您已将其声明为 @property,在这种情况下,您尝试将 -_stock 声明为 getter 将无效。实例变量将是 _stock 但 getter 将只是 -stock。如果这是一个正确的猜测那么你是 NSLog(@"%@", self.stock) 你应该看到 (null) 作为输出——你的自定义 getter 没有被调用所以没有字典被创建。

您的代码通常看起来很老套;特别是:

  1. 你现在几乎从来没有把实例存储放在 @interface 里面;把它放在 @implementation;
  2. 您的整个 object_info 可以重新表述为 @property,然后您无需在 @implementation 中实现任何东西,只需允许属性 auto-synthesise;
  3. 而不是 [obj setDescription: desc]; 等,您现在更常看到点语法的使用,例如 obj.description = desc;。编译器仍将确保调用正确的 setter(或 getter),这只是一个需要较少心理上下文的公式,允许直接 left-to-right 读取;
  4. 更正常的现代访问词典的方式是 dictionary[key] = value 存储和 dictionary[key] 访问。
  [self addObject:@"AAA" key:@"BBB" floatrc:100 floatwsc:200 intnoh:300 floatns:400];
  [self Print_Object:@"BBB"];

这对我来说很好用,你用错密钥了吗?

  [self addObject:@"AAA" key:@"BBB" floatrc:100 floatwsc:200 intnoh:300 floatns:400];
  [self Print_Object:@"AAA"];

这是错误的,因为对象是 nil。

  2017-02-16 10:48:34.086 Test[53298:14250944] Key: AAA Description: (null) Retail Cost: 0.000000 Wholesale Cost: 0.000000 Number on Hand: 0 Number Sold: 0