user8411642
user8411642

Reputation:

Applying predicate to an element in array

I'm getting the following response from Postman...

"product_images": [
                {
                    "id": "973",
                    "image": "http://sellerapp.binaryicdirect.com/public/uploads/products/263_22_1_image",
                    "is_default": "0"
                },
                {
                    "id": "988",
                    "image": "http://sellerapp.binaryicdirect.com/public/uploads/products/263_22_10_image",
                    "is_default": "0"
                },
                {
                    "id": "989",
                    "image": "http://sellerapp.binaryicdirect.com/public/uploads/products/263_22_11_image_1507874590",
                    "is_default": "1"
                }
            ]

Now I want to apply a predicate and add to my array only that image with an is_default value of 1. So in this case just the last image will be added. So how can I achieve this...? Hope somebody can help...

As of now all images are added to the array and that I'm doing like so...

var productImages :[ProductImage] = []
        if let images = anItem["product_images"] as? [[String:String]]
        {
          for image in images {
            guard let imageId = image["id"],
                  let url1 = image["image"],
                  let isDefaultValue = image["is_default"] else {continue}
                  print(imageId)
                 let productImage = ProductImage(id: imageId, url: URL(string: url1)!, isDefault: (Int(isDefaultValue) ?? 0) != 0)                        
                productImages.append(productImage)
            }}

        let theProduct = Product(name: name, id: id, theRate: rate, quantity: qty, sku: skuCode, prdCateg: prodCat, prodDescr: description, images: productImages, mrp: mrp)

        self.productData1.append(theProduct)

Upvotes: 0

Views: 109

Answers (4)

vadian
vadian

Reputation: 285059

Assuming there is always only one default image use the first function

var productImages = [ProductImage]()
if let images = anItem["product_images"] as? [[String:String]]
{
     if let defaultImage = images.first(where: {$0["is_default"] == "1"}) {
        let productImage = ProductImage(id: defaultImage["id"]!, 
                                       url: URL(string: defaultImage["image"]!)!, 
                                 isDefault: true)                        
        productImages.append(productImage)
     }
...

Edit

This is a stand-alone version to be tested in a Playground

struct ProductImage {
    let id : String
    let url : URL
    let isDefault : Bool
}

let images = [
    ["id": "973", "image": "http://sellerapp.binaryicdirect.com/public/uploads/products/263_22_1_image", "is_default": "0"],
    ["id": "988", "image": "http://sellerapp.binaryicdirect.com/public/uploads/products/263_22_10_image", "is_default": "0"],
    ["id": "989", "image": "http://sellerapp.binaryicdirect.com/public/uploads/products/263_22_11_image_1507874590", "is_default": "1"]
]

var productImages = [ProductImage]()
if let defaultImage = images.first(where: {$0["is_default"] == "1"}) {
    let productImage = ProductImage(id: defaultImage["id"]!,
                                    url: URL(string: defaultImage["image"]!)!,
                                    isDefault: true)
    productImages.append(productImage)
}
print(productImages)

Upvotes: 0

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16416

Solution 1

You can achieve it one line of code

In swift 3 You can use filter instead of predicate

let productsWithDefaultImage = productImages.filter{$0.isDefault == 1}

Solution 2

As Ahmed Suggest Just copy and paste it

  var productImages :[ProductImage] = []
    if let images = anItem["product_images"] as? [[String:String]]
    {
      for image in images {
        guard let imageId = image["id"],
              let url1 = image["image"],
              let isDefaultValue = image["is_default"],
              isDefaultValue == "1" else {continue}
              print(imageId)
             let productImage = ProductImage(id: imageId, url: URL(string: url1)!, isDefault: (Int(isDefaultValue) ?? 0) != 0)                        
            productImages.append(productImage)
        }}

    let theProduct = Product(name: name, id: id, theRate: rate, quantity: qty, sku: skuCode, prdCateg: prodCat, prodDescr: description, images: productImages, mrp: mrp)

    self.productData1.append(theProduct)

Upvotes: 0

Ahmad F
Ahmad F

Reputation: 31645

You could simply productImage.isDefault value before appending to productImages array:

if let images = anItem["product_images"] as? [[String:String]]
{
    for image in images {
        guard let imageId = image["id"],
              let url1 = image["image"],
              let isDefaultValue = image["is_default"] else {continue}

        print(imageId)

        let productImage = ProductImage(id: imageId, url: URL(string: url1)!, isDefault: (Int(isDefaultValue) ?? 0) != 0)

        // just add a simple check here:
        if productImage.isDefault == 1 {
            productImages.append(productImage)
        }
    }
}

Upvotes: 1

Damo
Damo

Reputation: 12890

Here's a working playground that illustrates what you could do

import UIKit
import PlaygroundSupport


let productImages: [[String:String]] = [
    [
    "id": "973",
    "image": "http://sellerapp.binaryicdirect.com/public/uploads/products/263_22_1_image",
    "is_default": "0"
    ],
    [
    "id": "988",
    "image": "http://sellerapp.binaryicdirect.com/public/uploads/products/263_22_10_image",
    "is_default": "0"
    ],
    [
    "id": "989",
    "image": "http://sellerapp.binaryicdirect.com/public/uploads/products/263_22_11_image_1507874590",
    "is_default": "1"
    ]
]

let productsWithDefaultImage = productImages.filter {

    if let is_default: String = $0["is_default"] {
        if is_default == "1" {
            return true
        }
    }

    return false

}

Upvotes: 0

Related Questions