kampbell411 kampbell411 - 2 years ago 111
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() {

table.dataSource = self
table.delegate = self

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

self.navigationItem.leftBarButtonItem = self.editButtonItem()

func reloadTable(notif: AnyObject) {

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


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)






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)


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

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

_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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download