0

I'd like to read data from an array and write data to a file and viceversa. The file itself has a known path.

The BTTableCell class manages and populates the tableView. I'm using dati_Riga class in order to have a single array. I thought it would be easier to read and save data using WriteToFile in order to write data and NSMutableArray to read them, but I'm having trouble.

In the following code snippet of the ViewController file you can see functions I wrote in order to open and save data which don't work.

Where did I get wrong? My idea is to save data using BTTableCell but I wasn't able to make it work.

Data structure:

import Cocoa

class BPTableCell:NSTableCellView {

    @IBOutlet weak var item_IconaFile: NSImageView!
    @IBOutlet weak var item_NomeFile: NSTextField!
    @IBOutlet weak var item_PathFile: NSTextField!
    @IBOutlet weak var item_MD5: NSTextField!
    @IBOutlet weak var item_SHA1: NSTextField!
}

class dati_Riga {
    var dati_Riga_item_IconaFile: NSImage!
    var dati_Riga_item_NomeFile: String!
    var dati_Riga_item_PathFile: String!
    var dati_Riga_item_MD5: String!
    var dati_Riga_item_SHA1: String!
        init (dati_Riga_item_IconaFile: NSImage, dati_Riga_item_NomeFile: String, dati_Riga_item_PathFile: String, dati_Riga_item_MD5: String, dati_Riga_item_SHA1: String)
        {
            self.dati_Riga_item_IconaFile = dati_Riga_item_IconaFile
            self.dati_Riga_item_NomeFile = dati_Riga_item_NomeFile
            self.dati_Riga_item_PathFile = dati_Riga_item_PathFile
            self.dati_Riga_item_MD5 = dati_Riga_item_MD5
            self.dati_Riga_item_SHA1 = dati_Riga_item_SHA1
        }
}

Extract from "ViewController.swift":

import Cocoa
import Foundation
import AppKit
import CryptoSwift

class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {

@IBOutlet weak var tableview: NSTableView!

var item_IconaFile = [NSImage]()
var item_NomeFile = [String]()
var item_PathFile = [String]()
var item_MD5 = [String]()
var item_SHA1 = [String]()

var miei_Dati_Riga : [dati_Riga] = [dati_Riga(dati_Riga_item_IconaFile: NSImage(), dati_Riga_item_NomeFile: "", dati_Riga_item_PathFile: "", dati_Riga_item_MD5: "", dati_Riga_item_SHA1: "")]

override func viewDidLoad() {
    super.viewDidLoad()
    self.miei_Dati_Riga.removeAtIndex(0)
}

override var representedObject: AnyObject? {
    didSet {
    // Update the view, if already loaded.
    }
}


 // MARK: Funzioni relative la TableView

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {

    let result : BPTableCell = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! BPTableCell

    result.item_IconaFile.image = item_IconaFile[row]
    result.item_NomeFile.stringValue = item_NomeFile[row]
    result.item_PathFile.stringValue = item_PathFile[row]
    result.item_MD5.stringValue = item_MD5[row]
    result.item_SHA1.stringValue = item_SHA1[row]

    return result
}

func numberOfRowsInTableView(tableView: NSTableView) -> Int {
    let num_Righe = item_NomeFile.count
    return num_Righe
}

// MARK: Apri e Salva file
@IBAction func Save_File(sender: NSMenuItem) {
    let salva_File = NSSavePanel()
    salva_File.extensionHidden = true
    salva_File.canSelectHiddenExtension = true
    salva_File.allowedFileTypes = ["cpa"]
    salva_File.beginWithCompletionHandler { (result:Int) -> Void in
        if result == NSFileHandlingPanelOKButton {
            let save_URL_File = salva_File.URL

            let mio_Array: NSArray = self.miei_Dati_Riga
            print(self.miei_Dati_Riga.count)
            print(self.miei_Dati_Riga[0])
            if mio_Array.writeToFile((save_URL_File?.path)!, atomically: true) {
                print("File Salvato")}
            else {
              print("File Non Salvato")
            }
        }
    }
}

@IBAction func Open_File(sender: NSMenuItem) {
    let apri_File = NSOpenPanel()
    apri_File.allowedFileTypes = ["cpa"]
    apri_File.beginWithCompletionHandler { (result:Int) -> Void in
        if result == NSFileHandlingPanelOKButton {
            let contenuto_File = NSMutableArray (contentsOfURL: apri_File.URL!)
        self.tableview.reloadData()
        }
    }

}

  • Senza offesa ma il tuo codice è abbastanza difficile da leggere (no offense but your code is quite hard to read). Class names are supposed to start with a capital letter, methods, functions and variables with a lowercase letter and Apple strongly discourages developers to use underscore separators. The issue is probably that you can only save property list types (`NSString`, `NSNumber`, `NSDate` and `NSData`) via an array to disk, but not for example `NSImage` – vadian Jan 10 '16 at 14:11
  • Non sono un programmatore, comunque ti ringrazio per i consigli che mi hai dato, di cui terrò conto per la modifica del codice, ma come posso fare per salvare e leggere su un file che ha una property list contenente una immagine (NSImage)? – C. Piersigilli Jan 10 '16 at 17:06
  • In English: I'm not a programmer, however I thank you for the advice you gave me, which I will take to the code change, but how can I do to save and read a file that has a property list containing an image '(NSImage)' ? Can you give me an example? – C. Piersigilli Jan 10 '16 at 17:15
  • For example `NSImage` can be represented by `NSData` which is property list compliant. – vadian Jan 10 '16 at 17:20
  • Could you give me an example for os x? – C. Piersigilli Jan 10 '16 at 19:51
  • If you need a placeholder like an empty string, use `NSData()`, for a concrete `NSImage` instance use `image.TIFFRepresentation!` – vadian Jan 10 '16 at 19:56

0 Answers0