“ShopDisplay”类型的不可变值仅具有名为“setAllShopItems”的变异成员

时间:2015-09-13 21:38:40

标签: ios swift struct private mutating-function

我正在尝试设置一个不是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
    }

因此,如果设置的所有变量都是变量,并且每当我设置它们mutating时我都会使用关键字struct,那么我仍然会收到此错误?:

  

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

0 个答案:

没有答案