0

我正在尝试设置一个 var 而不是 let 的数组属性。注意setAllShopItems方法:

struct ShopDisplay {
    private var allShopItemCategories: [ShopItemCategory]
    private var currentShopItemCategory: ShopItemCategory
    private var shopItemCategoryIcon: MMImageView
    private var currentShopItemGender: Gender
    private var allShopItems: [ShopItem]
    private var allDisplayedShopItems: [ShopItem]

    init() {
        allShopItemCategories = [ShopItemCategory]()
        currentShopItemCategory = ShopItemCategory(rawValue: "TO")!
        shopItemCategoryIcon =
            MMImageView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        currentShopItemGender = .Female
        allShopItems = []
        allDisplayedShopItems = []
    }

    mutating func setShopItemsToDisplay() {
        allDisplayedShopItems.removeAll()
        for item in self.allShopItems {
            let itemCategory = item.object["category"] as! String
            if itemCategory == currentShopItemCategory.rawValue {
                allDisplayedShopItems.append(item)
            }
        }
    }

    mutating func setAllShopItems(shopItems: [ShopItem]) {
        self.allShopItems = shopItems
    }

我这样称呼这个setAllShopItems()方法:

override func prepareForSegue(  segue: UIStoryboardSegue,
        sender: AnyObject?) {
            if (segue.identifier == "dressingRoom") {
                let dressingRoomViewController = segue.destinationViewController
                    as! DressingRoomViewController
                dressingRoomViewController.getShopDisplay().setAllShopItems(self.allShopItems)
                dressingRoomViewController.getShopDisplay().setAllShopItemCategories(
                    self.categories)
            }
    }

getShopDisplay 返回的属性也是一个 var:

class DressingRoomViewController:   UIViewController,
                                    UICollectionViewDelegateFlowLayout,
                                    UICollectionViewDataSource

{
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    private let identifier = "cellIdentifier"
    private let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    private let cellSpacing: CGFloat = 5
    private let cellsPerRow: CGFloat = 5
    private var cellSize: CGFloat = 0.00
    private var shopDisplay = ShopDisplay()
    @IBOutlet weak var categoryIcon: MMImageView!

    func getShopDisplay() -> ShopDisplay {
        return self.shopDisplay
    }

因此,如果所有这些被设置的变量都是 vars,并且我mutating在设置它们时使用关键字(如果它们是structs),我怎么还会收到这个错误?:

/Users/Ben/Documents/Development/MirrorMirror/MirrorMirror/ChooseShopViewController.swift:40:44:“ShopDisplay”类型的不可变值仅具有名为“setAllShopItems”的变异成员

4

0 回答 0