Swift: Reading a plist from MainBundle and writing directly to Documents fails

后端 未结 1 501
夕颜
夕颜 2020-12-22 07:16

Take the following file, named Permissions.plist:




        
相关标签:
1条回答
  • 2020-12-22 07:54

    This is wrong:

    guard let docsURL = NSURL(string: docsString) else { ... }
    

    To create an NSURL from a file path, use

    let docsURL = NSURL(fileURLWithPath: docsString)
    

    Conversely, you cannot use string interpolation to create a file path from an NSURL:

    let plistString = "\(plistURL)"
    

    Use the .path method instead:

    let plistPath = plistURL.path!
    

    Or better, use the writeToURL() method to write your dictionary, then you don't have to convert the URL to a path.

    0 讨论(0)
提交回复
热议问题