• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Writing, then reading JSON
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Writing, then reading JSON


  • Subject: Writing, then reading JSON
  • From: Charles Jenkins <email@hidden>
  • Date: Fri, 19 Sep 2014 11:45:30 -0400

My document structure is a file wrapper containing a bunch of RTF documents and a file called structure.json which describes how they relate to one another.

I write out the structure file like this:

    let structureDict = theProject.getStructureDictionary()
    let jsonData = NSJSONSerialization.dataWithJSONObject( structureDict, options: nil, error: outError )

    writeFileToWrapper(
      parentWrapper: theFileWrapper!,
      filename: structureFileName,
      data: jsonData,
      err: outError
    );


I’m not including the bodies of getStructureDictionary() or writeFileToWrapper() because they seem to work just fine. The structure.json file appears in my output package, and if I open it using TextWrangler, I see exactly the JSON content I expect, stored in UTF8 encoding.

The thing is, my app can’t read it back in. Here’s the function that’s not working:

  override func readFromFileWrapper(
    parentWrapper: NSFileWrapper!,
    ofType typeName: String!,
    error outError: NSErrorPointer
  ) -> Bool
  {
    if let fw = parentWrapper.fileWrappers[ structureFileName ] as? NSFileWrapper {
      if let data = fw.regularFileContents? {
        let debug: String = NSString( data: data, encoding: NSUTF8StringEncoding )
        let obj: AnyObject? = NSJSONSerialization.JSONObjectWithData( data, options: nil, error: outError )
        if let structureDict = obj as? NSDictionary {
          var proj = DocumentNode.readFromWrapperViaStructureDictionary( parentWrapper: parentWrapper, dictionary: structureDict )
          theProject = proj
          theFileWrapper = parentWrapper
          return true
        }
      }
    }
    return false;
  }


I expect I’ll find bugs in readFromWrapperViaStructureDictionary() if I ever call it, but I never get that far.

With Swift and Xcode, stepping line-by-line through code it a bit confusing because the current line indicator bounces all around, sometimes appearing on lines of code already executed. But to be best of my belief, my problem is that obj can’t be converted to an NSDictionary. I inserted the debug: String to see what’s read from the file, and it comes back as garbage.

Is calling regularFileContents the wrong way to read up my JSON file?

—

Charles Jenkins

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Writing, then reading JSON
      • From: Charles Jenkins <email@hidden>
    • Re: Writing, then reading JSON
      • From: Alex Zavatone <email@hidden>
  • Prev by Date: Re: FYI: docs in Xcode 5 are working again. (End)
  • Next by Date: Defer opening documents until app is fully inited
  • Previous by thread: Re: FYI: docs in Xcode 5 are working again. (End)
  • Next by thread: Re: Writing, then reading JSON
  • Index(es):
    • Date
    • Thread