2

In my app I'm using selfcreated arrays of points to mask images with CGPath.

So it looks like this

let pnt1 = CGPointMake(0, 33)
    let pnt2 = CGPointMake(33, 66)
    let pnt3 = CGPointMake(47, 71)
    let pnt4 = CGPointMake(66, 65)
    let pnt5 = CGPointMake(79, 69)
    let pnt6 = CGPointMake(90, 67)
    let pnt7 = CGPointMake(116, 36)
    let pnt8 = CGPointMake(93, 8)
    let pnt9 = CGPointMake(59, 0)
    let pnt10 = CGPointMake(37, 0)
    var pntz = NSMutableArray()
    pntz.addObject(NSValue(CGPoint: pnt1))
    pntz.addObject(NSValue(CGPoint: pnt2))
    pntz.addObject(NSValue(CGPoint: pnt3))
    pntz.addObject(NSValue(CGPoint: pnt4))
    pntz.addObject(NSValue(CGPoint: pnt5))
    pntz.addObject(NSValue(CGPoint: pnt6))
    pntz.addObject(NSValue(CGPoint: pnt7))
    pntz.addObject(NSValue(CGPoint: pnt8))
    pntz.addObject(NSValue(CGPoint: pnt9))
    pntz.addObject(NSValue(CGPoint: pnt10))

What i want is that i could write all my arrays to one file and then after my app has launched i'd load that file to app and use these arrays. I want to do it because these arrays are being created every 4-5 secs and i guess it's not good.

Keenle
  • 12,010
  • 3
  • 37
  • 46
s1ddok
  • 4,615
  • 1
  • 18
  • 31
  • Top of my head, you could wrap all arrays in one master object (container) and apply NSCoding protocol to serialize that object to disk. – Woodstock Aug 08 '14 at 08:59
  • @JohnWoods but how i get the file? it writes to folder on ios device, but not the app-project folder. – s1ddok Aug 08 '14 at 09:01

1 Answers1

3

Basically here is what you need to do:

  1. Archive your array to file on MAC; and do it in Swift code!
  2. Add this file to your app's main bundle on MAC
  3. Unarchive file from the app's main bundle on iOS

The tricky part is that you need to do all this automatically.

1. Archive your array to file on MAC and do it in Swift code!

Create Swift script that generates archive with array. First parameter of the script will accept App main bundle folder location(it will be passed at the second step). Add CreatePointsArrayArchive.swift file to your project, and do not add it to any targets.

Contents of CreatePointsArrayArchive.swift:

import Foundation
import Cocoa

println("MY APP: Generating points array archive...")

let archiveFileName = "MyPointsArray.archive"
let resourcesFolderPath = Process.arguments[1] // access first element because 0 contains this script file name

// YOUR CODE:
let pnt1 = CGPointMake(0, 33)
let pnt2 = CGPointMake(33, 66)
let pnt3 = CGPointMake(47, 71)
let pnt4 = CGPointMake(66, 65)
let pnt5 = CGPointMake(79, 69)
let pnt6 = CGPointMake(90, 67)
let pnt7 = CGPointMake(116, 36)
let pnt8 = CGPointMake(93, 8)
let pnt9 = CGPointMake(59, 0)
let pnt10 = CGPointMake(37, 0)
var pntz = NSMutableArray()
pntz.addObject(NSValue(point: pnt1))
pntz.addObject(NSValue(point: pnt2))
pntz.addObject(NSValue(point: pnt3))
pntz.addObject(NSValue(point: pnt4))
pntz.addObject(NSValue(point: pnt5))
pntz.addObject(NSValue(point: pnt6))
pntz.addObject(NSValue(point: pnt7))
pntz.addObject(NSValue(point: pnt8))
pntz.addObject(NSValue(point: pnt9))
pntz.addObject(NSValue(point: pnt10))


// Archiving array to file
if NSKeyedArchiver.archiveRootObject(pntz, toFile: archiveFileName) {
    println("    Array points archive generated SUCCESSFULY wit hfile name '\(archiveFileName)'.")
} else {
    println("    FAILED to generate array points archive.")
}

// Copy file to app budle
println("MY APP: Copying '\(archiveFileName)' to app main bundle at path '\(resourcesFolderPath)'...")
var err: NSError?
if NSFileManager.defaultManager().copyItemAtPath(archiveFileName, toPath: resourcesFolderPath + archiveFileName, error: &err) {
    println("    '\(archiveFileName)' file added to app main bundle.")
} else {
    println("    FAILED to add '\(archiveFileName)' to app main bundle.")
}

... so afteer 1-st step there is CreatePointsArrayArchive.swift file at the root folder of the project; with the code that listed above.

2. Add this file to your app's main bundle on MAC

You need to perform this as a build step. Here is what you need to do:

  1. Open you project build target and Run Script phase:

    enter image description here

  2. Type following line in script text area:

    xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) CreatePointsArrayArchive.swift ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/
    

    ... screenshot:

    enter image description here

3. Unarchive file from the app's main bundle on iOS

To access you archive in iOS App use code bellow:

// NOTE: MyPointsArray.archive is name of the file that was defined in CreatePointsArrayArchive.swift file
let pointsArchivePath = NSBundle.mainBundle().pathForResource("MyPointsArray", ofType: "archive")
let pointsArr = NSKeyedUnarchiver.unarchiveObjectWithFile(archivePath) as NSArray
Keenle
  • 12,010
  • 3
  • 37
  • 46
  • wow! i'll let you know if that worked for me! the question is can i delete file CreatePointsArrayArchive.swift after i ran script? or it is now a part of app and will be just calculated every time user enters app? – s1ddok Aug 08 '14 at 12:43
  • CreatePointsArrayArchive.swift executed only at compile time. And it does not get copied and executed at client. So you can just keep CreatePointsArrayArchive.swift file in you project. Should you need to alternate your array, do it in the script file (CreatePointsArrayArchive.swift) and rebuild the app. – Keenle Aug 08 '14 at 13:20
  • Basically CreatePointsArrayArchive.swift can be substituted with any other scripting file. I just wanted to keep it in swift since you already have code done in swift. – Keenle Aug 08 '14 at 13:22