swift 我在通过按钮将项目从一个数组移动到另一个数组时遇到了问题

6tqwzwtp  于 2023-04-04  发布在  Swift
关注(0)|答案(2)|浏览(128)

所以我是Swift的新手,我很难让我的愿望列表按钮将一个项目从阵列运动鞋移动到阵列运动鞋1

class Sneakers {
    var sneakers = [
        BagProducts(image: UIImage(named: "Image")! , category: "Men" , name: "Adidas Gazelle", price: 100.00, size: "40" , color: "Blue"),
        BagProducts(image: UIImage(named: "Image")! , category: "Men" , name: "Adidas Gazelle", price: 100.00, size: "40" , color: "Blue"),
    ]
    
    var sneakers1 = [
        WishProducts(image: UIImage(named: "AdidasGalaxy")! , category: "Women" , name: "Adidas Galaxy 6", price: 55.00, size: "", color: ""),
        WishProducts(image: UIImage(named: "NewBalance")! , category: "Men" , name: "New Balance BB550", price: 150.00, size: "", color: ""),
    ]
}

这是我的数组
按钮应该一次只移动一个单元格

@objc func wishButtonTapped(_ sender: UIButton) { 
    print("ola- \(viewModel.sneakers1)") 

    for sneaker in viewModel.sneakers { 
        let wishProduct = WishProducts(image: sneaker.image, category: sneaker.category, name: sneaker.name, price: sneaker.price, size: sneaker.size, color: sneaker.color) 
        viewModel.sneakers1.append(wishProduct) 
     } 
   print("ola- \(viewModel.sneakers1)") 
   print("Moved to wishlist") 
}

这是我用来使它移动的函数,但是当我运行它时,它出现了这个错误“在调用示例方法'append'时没有精确匹配”

protocol BagProduct {
   var image: UIImage { get } 
   var category: String { get } 
   var name: String { get } 
   var price: Double { get } 
   var size: String { get } 
   var color: String { get } 
}
protocol WishProduct { 
   var image: UIImage { get } 
   var category: String { get } 
   var name: String { get } 
   var price: Double { get } 
   var size: String { get } 
   var color: String { get } 
}
struct WishProducts: WishProduct { 
   let image: UIImage 
   let category: String 
   let name: String 
   let price: Double 
   let size: String 
   let color: String 

init(image: UIImage, category: String, name: String, price: Double, size: String, color: String) { 
   self.image = image 
   self.category = category 
   self.name = name 
   self.price = price 
   self.size = size 
   self.color = color 
  } 
}

struct BagProducts: BagProduct { 
   let image: UIImage 
   let category: String 
   let name: String 
   let price: Double 
   let size: String 
   let color: String 
init(image: UIImage, category: String, name: String, price: Double, size: String, color: String) { 
   self.image = image 
   self.category = category 
   self.name = name 
   self.price = price 
   self.size = size 
   self.color = color 
  } 
}

这是我在产品中使用的结构体

5vf7fwbs

5vf7fwbs1#

所以我明白你需要通过点击按钮将选中的项目移动到另一个数组中

swvgeqrz

swvgeqrz2#

我不确定是否可以不费很大的力气就添加两个不同类型的数组(即使它们符合相同的协议),也不知道是否需要自定义函数。但有两个简单的替代方案可以考虑:使它们成为相同的类型,或者使用map将一种类型转换为另一种类型。

sneakers.append(contentsOf: sneakers1.map { wishProduct in
    BagProducts(
        image: wishProduct.image,
        category: wishProduct.category,
        name: wishProduct.name,
        price: wishProduct.price,
        size: wishProduct.size,
        color: wishProduct.color
    )
})

相关问题