gpt4 book ai didi

ios - 我必须创建一个可变字典,但出现 "Incompatible pointer types initializing"错误

转载 作者:行者123 更新时间:2023-11-28 21:21:26 30 4
gpt4 key购买 nike

我正在尝试创建一个字典,其中将包含一个值为“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;
}

最佳答案

您遇到的问题是您试图将 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],
}];

关于ios - 我必须创建一个可变字典,但出现 "Incompatible pointer types initializing"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39692145/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com