将谓词应用于数组中的元素

时间:2017-11-08 10:25:49

标签: ios swift nspredicate

我收到邮递员的以下回复......

"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"
                }
            ]

现在我想应用一个谓词,并且只将is_default值为1的图像添加到我的数组中。所以在这种情况下,只会添加最后一个图像。那我怎么能实现这个......?希望有人能帮忙......

截至目前,所有图像都已添加到阵列中,而且我这样做...

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)

4 个答案:

答案 0 :(得分:1)

解决方案1 ​​

你可以实现一行代码

在swift 3中你可以使用过滤器而不是谓词

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

解决方案2

正如Ahmed建议只需复制并粘贴

  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)

答案 1 :(得分:1)

在追加到productImage.isDefault数组之前,您只需productImages值:

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)
        }
    }
}

答案 2 :(得分:1)

假设始终只有一个 default图片使用first函数

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)
     }
...

修改

这是一个在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)

答案 3 :(得分:0)

这是一个可以说明你能做什么的工作场所

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

}