我正在尝试创建一个字典,其中包含一个值为' Object_Info'的键。 我有以下代码并得到此错误:
Incompatible pointer types initializing 'NSMutableDictionary *' with an expression of type 'NSDictionary *'
这是我的代码:
#import <Foundation/Foundation.h>
@interface Object_Info : NSObject
{
NSString *product;
float cost;
int qty;
}
@end
@implementation Object_Info
-(id) init {
if (self = [super init]){
product = @"";
cost = 0.0;
qty = 0;
}
return self;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableDictionary *stock = @{ //ERROR IS HERE!!!!!
@"Lawn Chair" : [Object_Info new],
@"Beach Chair" : [Object_Info new],
@"Kitchen Chair" : [Object_Info new],
@"Futon" : [Object_Info new],
};
for(NSString *key in [stock allKeys]) {
NSLog(@"%@",[stock objectForKey:key]);
}
}
return 0;
}
答案 0 :(得分:1)
您遇到的问题是您正在尝试将NSDictionary分配给NSMutableDictionary。
试试这个:
NSMutableDictionary *stock = [NSMutableDictionary new];
[stock setDictionary:@{
@"Lawn Chair" : [Object_Info new],
@"Beach Chair" : [Object_Info new],
@"Kitchen Chair" : [Object_Info new],
@"Futon" : [Object_Info new],
}];