Processing one-time immutable data created by the application

I am rewriting my application to enable SceneKit to display molecules. About 60% of my code is dedicated to creating these molecules as SCNNodearrays stored in a dictionary. Another 20% create a dictionary Stringfor displaying text. Ideally, these dictionaries should be created only once.

I am currently creating these call dictionaries viewDidLoadin my main (and initial) VC. Then I archive the dictionaries:

NSKeyedArchiver.archiveRootObject(moleculeDictionary, toFile: filePath)

Then I read the archived dictionary back to the local dictionary used by the application:

moleculeDictionary = Molecules.readFile() as! [String: [SCNNode]]

// and in Molecules, the readFile() function:
let dictionary = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? NSDictionary ?? NSDictionary()
return dictionary

I read it from the archive if the local version no longer exists. I still need to add a dictionary check.

: ? , . - . , , -. , CoreData, , .

0
1

/ node node , .

, art.scnassets Xcode

        let scene = SCNScene(named: "art.scnassets/oneMolecule.scn")!

, , , , .

node

let documentDirURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
if let gizmoNode = gameView.scene?.rootNode.childNodeWithName("Gizmo", recursively:true) {
    let gizmoData = NSKeyedArchiver.archivedDataWithRootObject(gizmoNode)
    let gizmoURL = documentDirURL.URLByAppendingPathComponent("Gizmo").URLByAppendingPathExtension("plist")
    print("gizmo path:", gizmoURL.path)
    if (!gizmoData.writeToURL(gizmoURL, atomically: true)) {
        return false
    }
}

iOS , "" . " " , , , , .

node, , PNG.

let path = NSBundle.mainBundle().pathForResource("Gizmo", ofType: "plist")
print(path)
let gizmoNode = NSKeyedUnarchiver.unarchiveObjectWithFile(path!) as! SCNNode
print (gizmoNode)
scene.rootNode.addChildNode(gizmoNode)

: SCNScenes SCNNodes SCNView. node Mac. node, . , , . /copy/build iOS.

+2

Source: https://habr.com/ru/post/1610621/


All Articles