在NSUserDefaults

时间:2016-01-18 21:46:16

标签: swift dictionary nsuserdefaults

所以我有一个字典var songs = [String: [Song]](),其中Song是一个有五个成员的结构。

我希望像这样存储这个词典:NSUserDefaults.standardUserDefaults().setObject(songs, forKey: "Data")

但是,我收到的错误是无法转换为AnyObject类型?

编辑:

如何将此类SongManager转换为NSCoding,以便我可以稍后将其作为NSData传递给NSUserDefaults?

import UIKit

var SongMgr: SongManager = SongManager()

struct song {
    var songnum: Int = 0
    var name = "not defined"
    var lyrics_wo_chords = NSMutableAttributedString()
    var lyrics_w_chords = NSMutableAttributedString()
    var favorite = false
}

class SongManager: NSObject {
    var songs = [String: [song]]()
    func addnewsong(songbook: String, songnum: Int, name: String, lyrics_wo_chords: NSMutableAttributedString, lyrics_w_chords: NSMutableAttributedString, favorite: Bool){

        songs[songbook]?.append(song(songnum: songnum, name: name, lyrics_wo_chords: lyrics_wo_chords, lyrics_w_chords: lyrics_w_chords, favorite: favorite))
    }
}

0 个答案:

没有答案