사전에 객체를 추가하는 방법뿐만 아니라 NSMutableDictionary
을 만들었습니다. addObject 함수가 제대로 사전에 객체를 추가하지만 나중에 Print_Object
메서드를 사용하여 객체를 인쇄하려고하면 객체에 속한 모든 단일 변수가 null/0
으로 인쇄됩니다. 내 코드에 문제가 있습니까?NSMutableDictionary 개체가 메서드에서 제대로 액세스되지 않습니까?
#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
제 생각에는 문제는'[self.stock setValue : obj forKey : theKey];'에 있습니다. 당신의 obj가 Object이기 때문에'setValue' 대신'setObject'를 사용하려고 할 수 있습니까? Like '[self.stock setObject : obj forKey : theKey]; ' – nynohu
@nynohu가 작동하지 않습니다. 그것은 여전히 나에게 같은 결과를 준다. 나는 현재 나의 주요 함수에서'[newDict Print_Object : @ "Galaxy Note7"]; (내 메소드를 포함하는 클래스의 인스턴스 사용)를 호출 중이며, 출력은 다음과 같다. ** Key : Galaxy Note7 Description : null) Retail Cost : 0.000000 Wholesale Cost : 0.000000 Hand on 수 : 0 Number Sold : 0 ** – user6335453
사전에 값을 설정하기 전에 객체 추가 기능에 obj를 인쇄 할 수 있습니까 –