ios - Type Issues When Writing a Nested dictionary to a plist (Swift) -


i'm trying write dictionary multiple layers of nesting progress list (plist) in swift. however, reason 1 of dictionaries being treated array when inspect plist leading bunch of type issues in project.

here original nested dictionary:

i've attached screenshot showing plist structure , showing original data structure, know aren't meant show photos of code text editor isn't formatting , mistreating angle brackets

original data structure

screenshot showing plist

updated

ok added 1 more key value pair @ topmost level , being written dictionary correctly. problem i'm having can write plist fine when try read nil.

func initializeprogress() { let directories = nssearchpathfordirectoriesindomains(.documentdirectory, nssearchpathdomainmask.userdomainmask, true)  if let documents = directories.first {     if let urldocuments = nsurl(string: documents) {         let urlprogress = urldocuments.urlbyappendingpathcomponent("progress.plist")         let progressdict = nsdictionary(dictionary: ["ap biology": ["misc": ["correct": 0, "incorrect": 0, "totalinsection": 0, "percentdone": 0],             "basics": ["correct": 0, "incorrect": 0, "totalinsection": 0, "percentdone": 0],             "essential chemistry": ["correct": 0, "incorrect": 0, "totalinsection": 0, "percentdone": 0],             "molecules of life": ["correct": 0, "incorrect": 0, "totalinsection": 0, "percentdone": 0]]])         progressdict.writetofile(urlprogress.path!, atomically: true)      }  } 

}

func retrieveandsetprogress() { let directories = nssearchpathfordirectoriesindomains(.documentdirectory, nssearchpathdomainmask.userdomainmask, true)  let documents = directories.first let urldocuments = nsurl(string: documents!) let urlprogress = urldocuments!.urlbyappendingpathcomponent("progress.plist") print(nsdictionary(contentsofurl: urlprogress)) let data = nsdata(contentsofurl: urlprogress, options: nsdatareadingoptions())     print(data) progress = nsdictionary(contentsofurl: urlprogress)! as! dictionary<string, dictionary<string, dictionary<string, int>>>  } 

progress global variable , last line of second method produces error: fatal error: unexpectedly found nil while unwrapping optional value

change

print(nsdictionary(contentsofurl: urlprogress)) 

to

print(nsdictionary(contentsoffile: urlprogress.absolutestring)) 

it can read then. tested on playground


Comments