在我的应用程序中,我使用自创的点阵列来使用CGPath屏蔽图像。
所以看起来像这样
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))
我想要的是我可以将所有数组写入一个文件,然后在我的应用程序启动后,我将该文件加载到应用程序并使用这些数组。我想这样做是因为这些数组每4-5秒创建一次,我猜它不好。
答案 0 :(得分:3)
基本上你需要做的就是:
棘手的部分是你需要自动完成所有这些。
创建使用数组生成存档的Swift脚本。脚本的第一个参数将接受App主捆绑文件夹位置(它将在第二步传递)。将 CreatePointsArrayArchive.swift 文件添加到您的项目中,不将其添加到任何目标。
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.")
}
...所以在第一步之后,项目的根文件夹中有 CreatePointsArrayArchive.swift 文件;使用上面列出的代码。
您需要将其作为构建步骤执行。以下是您需要做的事情:
打开项目构建目标和运行脚本阶段:
在脚本文本区域中键入以下行:
xcrun swift -sdk $(xcrun --show-sdk-path --sdk macosx) CreatePointsArrayArchive.swift ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/
...截图:
要在iOS App中访问您的存档,请使用以下代码:
// 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