I've created an NSMutableDictionary
as well as a method to add objects to the dictionary. I know that the function addObject adds objects to the dictionary properly, but later when I try to print the objects using method Print_Object
, every single variable belonging to object prints as null/0
. Is there something wrong with my code?
-(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);
}
This is the declaration of 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
This is 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
Can you show us the declaration of stock
? I suspect you've declared it as an @property
, in which case your attempt to declare -_stock
as the getter is going to be ineffective. The instance variable will be _stock
but the getter will be just -stock
. If this is a correct guess then were you to NSLog(@"%@", self.stock)
you should see (null)
as the output — your custom getter isn't called so no dictionary is ever created.
Your code generally looks very old fashioned; in particular:
@interface
now; put it in the @implementation
;object_info
could be reformulated as @property
s and then you needn't implement a thing inside that @implementation
, just allow the properties to auto-synthesise;[obj setDescription: desc];
, etc, you'd now more normally see use of dot syntax, like obj.description = desc;
. The compiler will still ensure the proper setter (or getter) is called, it's just a formulation that requires less mental context, allowing a direct left-to-right reading;dictionary[key] = value
to store and dictionary[key]
to access.