iOS中使用数组进行JSON解析时出错

iOS中使用数组进行JSON解析时出错,ios,json,parsing,nsjsonserialization,Ios,Json,Parsing,Nsjsonserialization,我刚接触iPhone,基本上在我的应用程序中,我在JSON模型下解析,需要将其保存在我的自定义实体类中 JSON模型: { "Products": [ { "Pcs": [ { "product_id": 2, "product_name": "MyProduct", "category": {

我刚接触iPhone,基本上在我的应用程序中,我在JSON模型下解析,需要将其保存在我的自定义实体类中

JSON模型:

{
    "Products": [
        {
            "Pcs": [
                {
                    "product_id": 2,
                    "product_name": "MyProduct",
                    "category": {
                        "Clamshells": [
                            {
                                "product_category_id": 11,
                                "product_category_name": "MyProductCategory",
                                "Sub_category": [
                                    {
                                        "sub_category_id": 21,
                                        "sub_category_name": "MyProductSubCategory"
                                    }
                                ]
                            }
                        ],
                        "Detachables": [
                            {
                                "product_category_id": 12,
                                "product_category_name": "MyProductCatrgory1",
                                "Sub_category": [
                                    {
                                        "sub_category_id": 31,
                                        "sub_category_name": "MyProductSubCategory1"
                                    }
                                ]
                            }
                        ],
                        "Convertibles": [
                            {
                                "product_category_id": 13,
                                "product_category_name": "MyProductCatrgory2",
                                "Sub_category": [
                                    {
                                        "sub_category_id": 41,
                                        "sub_category_name": "MyProductSubCategory2"
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    ]
}
我创建了三个实体类,如:

个人健康:

#import <Foundation/Foundation.h>
#import "MyProductCategory.h"

@interface PCs : NSObject

@property (nonatomic, retain) NSString *productTitle;
@property (nonatomic, retain) NSString *productId;
@property (nonatomic, retain) MyProductCategory *myProductCategory;

@end
但当我初始化数组或为其设置任何值时,它会崩溃。 错误消息是:

-[__NSCFDictionary setArrayClamshells:]: unrecognized selector sent to instance 0x7f8f16059730

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setArrayClamshells:]: unrecognized selector sent to instance 0x7f8f16059730'

错误在于您有一行将
myProductCategory
正确初始化为自定义对象:

pcs.myProductCategory = [[MyProductCategory alloc] init];
但在下一行中,您将丢弃它,并将其设置为
category
entry指定的字典:

pcs.myProductCategory = [item1 objectForKey:@"category"];
因此,当您到达下一行时,它将崩溃,因为
myProductCategory
不再是
myProductCategory
,而是
NSDictionary

pcs.myProductCategory.arrayClamshells = [NSMutableArray alloc] init];
你的代码在这里

pcs.myProductCategory = [[MyProductCategory alloc] init];
pcs.myProductCategory = [item1 objectForKey:@"category"];
正在正确创建您的
MyProductCategory
实例,但随后将其替换为字典。您可能打算将第二行写为

NSDictionary *category = [item1 objectForKey:@"category"];

然后在下一行中使用它解压字典内容

setArrayCamShells方法在哪里?@iDev这只是当
时调用的默认setter方法。ArrayCamShells=
是。是的,它看起来像object.array或object setArraykeep ArrayCamShells属性中的断点。尝试使用异常断点,您的问题似乎存在:
pcs.myProductCategory=[item1 objectForKey:@“category]”。将NSDictionary分配给
MyProductCategory
,然后将其视为NSDictionary(为什么会导致并解释崩溃错误)。另外,还有一些alloc/init显然做得不好。谢谢@Wain的回答。
pcs.myProductCategory.arrayClamshells = [NSMutableArray alloc] init];
pcs.myProductCategory = [[MyProductCategory alloc] init];
pcs.myProductCategory = [item1 objectForKey:@"category"];
NSDictionary *category = [item1 objectForKey:@"category"];