kampbell411 kampbell411 - 7 months ago 22
Swift Question

Edit tableView where rows are immutable values

I'm trying to edit tableView rows on values that are immutable.

I keep getting the following when I allow editing on table:


Cannot use mutating member on immutable value: 'dataSet' is a get-only property


View Controller.swift

import UIKit

class TableVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

table.dataSource = self
table.delegate = self

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TableVC.reloadTable(_:)), name: "reloadData", object: nil)

DataHandler.inst.loadData()
self.navigationItem.leftBarButtonItem = self.editButtonItem()
}

func reloadTable(notif: AnyObject) {
table.reloadData()
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return DataHandler.inst.dataSet.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellOld = tableView.dequeueReusableCellWithIdentifier("PicsCell") as? PicsCell

let cell = cellOld != nil ? cellOld! : PicsCell()

cell.initializeCell(DataHandler.inst.dataSet[indexPath.row])

return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedPic = DataHandler.inst.dataSet[indexPath.row]
let view = ImageVC(pic: selectedPic)

presentViewController(view, animated: true, completion: nil)
}



func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){


if editingStyle == UITableViewCellEditingStyle.Delete{


//Error here saying Cannot use mutating member on immutable value: 'dataSet' is a get-only property
// DataHandler.inst.dataSet.removeAtIndex(indexPath.row)


tableView.reloadData()


}


}

}


DataHandler.swift

static let inst = DataHandler()

private var _dataSet = [Pics]()

var dataSet: [Pics] {
return _dataSet
}

func addPic(img: UIImage, title: String, description: String) {

let imgName = saveMyImage(img)
let entry = Pics(img: imgName, title: title, description: description)
_dataSet.append(entry)

saveData()
loadData()
}

private func saveData() {
let data = NSKeyedArchiver.archivedDataWithRootObject(_dataSet)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "data")
NSUserDefaults.standardUserDefaults().synchronize()
}

func loadData() {
guard let data = NSUserDefaults.standardUserDefaults().objectForKey("data") as? NSData else {
return
}
guard let array = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? [Pics] else {
return
}

_dataSet = array

NSNotificationCenter.defaultCenter().postNotificationName("reloadData", object: nil)
}

private func saveMyImage(img: UIImage) -> String {
let imgData = UIImagePNGRepresentation(img)
let imgPath = "img\(NSDate.timeIntervalSinceReferenceDate()).png"
let fullPath = getPathForImage(imgPath)

imgData?.writeToFile(fullPath, atomically: true)
return imgPath
}

private func getPathForImage(name: String) -> String {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let fullPath = paths[0] as NSString
return fullPath.stringByAppendingPathComponent(name)
}

func imagePathName(name: String) -> UIImage? {
let path = getPathForImage(name)
let img = UIImage(named: path)

return img
}
}

JAL JAL
Answer

You declare dataSet as a get only property here:

var dataSet: [Pics] {
    return _dataSet
}

If you want to mutate it, add a setter:

var dataSet: [Pics] {
    get {
        return _dataSet
    }

    set(newSet) {
        _dataSet = newSet
    }

}

Or change your architecture to make _dataSet public.

Comments