2016-08-08 3 views
1

Swift에서 진행 목록 (plist)에 여러 개의 중첩 레이어가있는 사전을 쓰려고합니다. 그러나 어떤 이유에서든 사전 중 하나가 배열로 취급되어 프로젝트 프로젝트에서 형식 문제가 많은 plist를 검사합니다. 여기 유형 문제 plist에 중첩 된 사전을 쓸 때 (Swift)

은 원래의 중첩 된 사전입니다 :

나는 PLIST 구조를 보여주는 스크린 샷을 첨부했습니다 원래의 데이터 구조를 보여주는 또 다른, 난 당신이 코드의 사진을 보여주기 위해 의미되지 않습니다 알고 있지만이 텍스트 편집기 ISN '은 t가 적절히 포맷되고 각괄호

original data structure

screenshot showing plist

UPDATED 학대된다

,745,

Ok 최상위 레벨에 키 값 쌍을 한 개 더 추가했으며 사전에 올바르게 기록되었습니다. 제가 지금 가지고있는 문제는 plist에 잘 쓸 수 있다는 것입니다. 그러나 그것에서 읽으려고 할 때 나는 얻지 못하는 모든 것이 없습니다.

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 전역 변수 및 두 번째 방법의 마지막 라인 오류 발생 : fatal error: unexpectedly found nil while unwrapping an Optional value

+0

저에게 잘 작동합니다. plier를 쓰거나 읽는 코드를 게시하십시오. – dan

+0

사전의 최상위 레벨에 다른 키 값 쌍을 추가하여 올바르게 작동하게 만들었습니다. 제가 지금 가지고있는 문제는 프로젝트로 다시 읽어들이려고 할 때, 그것을 없애는 것입니까? 순간적으로 읽기/쓰기 코드를 게시합니다. 당신의 도움을 주셔서 감사합니다. –

+0

@ 단 질문을 업데이트하고 읽고 쓸 수있는 두 가지 방법을 추가했습니다. 도움을 주셔서 감사합니다. –

답변

0

변화

print(NSDictionary(contentsOfURL: urlProgress)) 

,174,
print(NSDictionary(contentsOfFile: urlProgress.absoluteString)) 

그러면 읽을 수 있습니다. 방금 놀이터에서 테스트했습니다

+0

감사합니다. –

관련 문제