2015-05-01 22 views
30

Ho una notizia ViewController e uno TeamViewController. Lo TeamViewController contiene una tabellaView di teamObjects che, se selezionata, viene aggiunta all'array. Voglio aggiungere questo array a NSUserDefaults così posso accedervi dallo NewsController che contiene una richiesta di url in cui è necessario il teamObjects. Tuttavia ho continuo a ricevere:Salvare oggetti personalizzati in NSUserDefaults

'tenta di inserire elenco non proprietà dell'oggetto ( '' ) per le squadre chiave'

Sono aperto per altri suggerimenti se c'è modi migliori di memorizzazione in NSUserDefaults

didSelectRowAtIndexPath metodo

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 


    let team = self.teamArray[indexPath.row] as Team 
    var removed = false 

    for (index, value) in enumerate(self.teamSelected) { 
     if (value == team) { 
      self.teamSelected.removeAtIndex(index) 
      removed = true 
     } 
    } 

    if (!removed) { 
     self.teamSelected.append(team) 
    } 

    var userDefaults = NSUserDefaults.standardUserDefaults() 
    userDefaults.setValue(self.teamSelected, forKey: "teams") 
    userDefaults.synchronize() 

    tableView.reloadData() 
} 

Il mio scopo

class Team: NSObject{ 
    var id: Int! 
    var name: NSString! 
    var shortname: NSString! 


    init(id: Int, name:NSString, shortname: NSString) { 
     self.id = id 
     self.name = name 
     self.shortname = shortname 

    } 

} 

risposta

124

In realtà, è necessario archiviare l'oggetto personalizzato in NSData quindi salvarlo predefinite dall'utente e recuperare da impostazioni predefinite dell'utente e annullarlo di nuovo. È possibile archiviare in questo modo

let teams = [Team(id: 1, name: "team1", shortname: "t1"), Team(id: 2, name: "team2", shortname: "t2")] 

var userDefaults = UserDefaults.standard 
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: teams) 
userDefaults.set(encodedData, forKey: "teams") 
userDefaults.synchronize() 

ed estrarlo come questo

let decoded = userDefaults.object(forKey: "teams") as! Data 
let decodedTeams = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Team] 

Ma se hai appena fatto che si otterrà

.Team encodeWithCoder:]: unrecognized selector sent to instance 

Si dovrà fare squadra conformi alle NSCoding proprio così

class Team: NSObject, NSCoding { 
    var id: Int 
    var name: String 
    var shortname: String 


    init(id: Int, name: String, shortname: String) { 
     self.id = id 
     self.name = name 
     self.shortname = shortname 

    } 

    required convenience init(coder aDecoder: NSCoder) { 
     let id = aDecoder.decodeInteger(forKey: "id") 
     let name = aDecoder.decodeObject(forKey: "name") as! String 
     let shortname = aDecoder.decodeObject(forKey: "shortname") as! String 
     self.init(id: id, name: name, shortname: shortname) 
    } 

    func encode(with aCoder: NSCoder) { 
     aCoder.encode(id, forKey: "id") 
     aCoder.encode(name, forKey: "name") 
     aCoder.encode(shortname, forKey: "shortname") 
    } 
} 
+3

Ottima spiegazione, grazie @ mohamede1945! – appsunited

+0

Grazie @appsunited :) – mohamede1945

+0

Grazie. Sei lo stesso ragazzo anche in TC? – khunshan

0

Si può provare NSKeyedUnarchiver, come qui di seguito

Qui ho conservato UIColor Oggetto in UserDefault che è possibile sostituire con il vostro oggetto

NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color]; 
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"]; 

e retrive che

NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"]; 
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData]; 

Nota : Si tratta di Objective C, ma spero che possibile convertire questo a Swift

Spero che questo risolve il problema

+0

a causa di eccezione non rilevata 'NSInvalidArgumentException', motivo: '- [Striky.Team encodeWithCoder:]: selettore non riconosciuto –

+0

Questo perché è necessario conformarsi al protocollo NSCoding – Steve

Problemi correlati