Ich habe eine NSMutableDictionary
sowie eine Methode zum Hinzufügen von Objekten zum Wörterbuch erstellt. Ich weiß, dass die Funktion addObject dem Wörterbuch Objekte korrekt hinzufügt, aber später, wenn ich versuche, die Objekte mit der Methode Print_Object
zu drucken, wird jede einzelne Variable, die zum Objekt gehört, als null/0
gedruckt. Stimmt etwas mit meinem Code nicht?NSMutableDictionary-Objekte nicht ordnungsgemäß von Methoden zugegriffen?
-(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);
}
Dies ist die Erklärung von 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
Dies ist 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
Meiner Meinung nach ist das Problem in '[self.stock setValue: obj forKey: theKey];'. Könnten Sie versuchen, 'setObject' anstelle von' setValue' zu verwenden, weil Ihr obj ein Objekt ist? So '[self.stock setObject: obj forKey: theKey];' – nynohu
@nynohu das funktioniert nicht. es gibt mir immer noch die gleiche Ausgabe. Ich rufe gerade die Methode in meiner Hauptfunktion mit '[newDict Print_Object: @" Galaxy Note7 "];' (mit einer Instanz der Klasse, die meine Methoden enthält) und meine Ausgabe ist: ** Key: Galaxy Note7 Beschreibung: (Null) Einzelhandelspreis: 0.000000 Großhandelspreis: 0.000000 Anzahl zur Hand: 0 Anzahl verkauft: 0 ** – user6335453
können Sie Obj in Objekt hinzufügen Funktion vor dem Festlegen von Wert für das Wörterbuch –