通过核心数据在特定属性中插入值数组

时间:2014-07-31 09:41:55

标签: ios core-data nsstring nsarray

我有像这样的json值,

    {
    "product_color" = Black;
            "product_description" = "The new Macbook air is ultraslim";
            "product_id" = 1;
            "product_large_image_url" = "https://www.gstatic.com/webp/gallery3/3_webp_ll.png";
            "product_name" = "MacBook Air";
            "product_price" = "$2500";
            "product_size" = Small;
            "product_stocks" = 50;
            "product_thumb_image_url" =         (
                "https://www.gstatic.com/webp/gallery3/2_webp_ll.png",
                 ......
                 ......
            );

}

我希望通过核心数据

在单个属性中插入product_thumb_image_url数组 我试过的是:

    +(void)insertingProduct:  (int16_t) cId :(NSDictionary *)dictionary{
            DataModel *dModel = [self dataModel];
           Products *productDetails=[dModel createEntity:products];
           productDetails.product_id=[[dictionary valueForKey:productid] integerValue];
           productDetails.product_large_image_url = [dictionary valueForKey:productlargeImage];
            productDetails.product_name=[dictionary valueForKey:productname];
          productDetails.product_price=[[dictionary valueForKey:productPrice] integerValue];
          productDetails.product_sizes=[dictionary valueForKey:productsizes];
          productDetails.product_stocks=[[dictionary valueForKey:productstocks] integerValue];
          productDetails.product_colors=[dictionary valueForKey:productcolors];
          productDetails.product_description=[dictionary valueForKey:productdescription];
         productDetails.product_thumb_image_url=[dictionary valueForKey:productThumbImage];

    [dModel save];
}

但它表明您无法在NSArray中插入NSString,我正在努力解决此问题,

1 个答案:

答案 0 :(得分:2)

根据您的问题,product_thumb_image_url是核心数据中的String属性,[dictionary valueForKey:productThumbImage]会从您传入的JSON中返回NSArray个URL字符串。

所以productDetails.product_thumb_image_url=[dictionary valueForKey:productThumbImage];尝试将数组存储为字符串,这显然不可能。

您需要在核心数据中存储Array,这可以通过使属性可转换来完成,或者您只需要存储一个图像URL,这只需要从数组中获取第一个项目即可(你应该检查数组是否包含一些项目):

[[dictionary valueForKey:productThumbImage] firstObject]