How to initialize a Dictionary with structure components in Swift -
i have following structure:
struct song { var songnum: int = 0 var name = "not defined" var lyrics_wo_chords = nsmutableattributedstring() var lyrics_w_chords = nsmutableattributedstring() var favorite = false } and i'm trying make dictionary var songs = [string: [song]]()
where the string name of songbook , [song] array of structures called song hold individual structure members
i've tried songs["songbook name"] = song.self as? [song] add new key dictionary. otherwise, have no idea how initialize it.
also, when append array of key:
songs["songbook name"]?.append(song( songnum: 1, name: "name", lyrics_o_chords: nsmutableattributedstring(string:"no chords"), lyrics_w_chords: nsmutableattributedstring(string: "with chords"), favorite: false))` the dictionary returned nil
any appreciated, thank you
your value in dictionary array of song (that [song]), need put [ ] around value make array of song:
songs["songbook name"] = [song( songnum: 1, name: "name", lyrics_wo_chords: nsmutableattributedstring(string:"no chords"), lyrics_w_chords: nsmutableattributedstring(string: "with chords"), favorite: false) ] structure , class names should capitalized, use song instead of song when defining structure.
Comments
Post a Comment