Does any one have or know of an example to add core data to sprite kit?
Thanks!
Ok, I'll just quickly run through NSUserDefaults, NSCoding and .plist approaches.
NSUserDefaults: Simple, singular data that is very easily saved and loaded. No data structure really, just a key-value pair where the key is a string by which you save and load the value. Read more: https://developer.apple.com/reference/foundation/userdefaults
// SAVE
let saveData = UserDefaults.standard
saveData.set(true, forKey: "isThisDataSimple")
saveData.set(5, forKey: "amountOfSecondsNeededToUnderstandThisData")
saveData.synchronize()
// LOAD
let loadData = UserDefaults.standard
print("This data is simple: \(loadData.bool(forKey: "isThisDataSimple"))")
print("And it took me only \(loadData.integer(forKey: "amountOfSecondsNeededToUnderstandThisData")) seconds to understand it!")NSCoding: A straightforward and independent way of saving data with a little bit more complexity. Probably wouldn't be good enough to handle table relationships and other database-related functionalities, but definitely a nice way of getting very far with little hassle. This would for example allow you to save multiple data objects that are defined in a class. Read more: https://developer.apple.com/reference/foundation/nscoding
// CLASS
import SpriteKit
class MyData: NSObject, NSCoding {
let isThisDataSimple: Bool
let amountOfSecondsNeededToUnderstandThisData: Int
init(dataIsThisDataSimple: Bool, dataAmountOfSecondsNeededToUnderstandThisData: Int) {
isThisDataSimple = dataIsThisDataSimple
amountOfSecondsNeededToUnderstandThisData = dataAmountOfSecondsNeededToUnderstandThisData
}
required convenience init(coder decoder: NSCoder) {
let isThisDataSimple = decoder.decodeBool(forKey: "isThisDataSimple")
let amountOfSecondsNeededToUnderstandThisData = decoder.decodeInteger(forKey: "amountOfSecondsNeededToUnderstandThisData")
self.init(dataIsThisDataSimple: isThisDataSimple, dataAmountOfSecondsNeededToUnderstandThisData: amountOfSecondsNeededToUnderstandThisData)
}
func encode(with coder: NSCoder) {
coder.encode(isThisDataSimple, forKey: "isThisDataSimple")
coder.encode(amountOfSecondsNeededToUnderstandThisData, forKey: "amountOfSecondsNeededToUnderstandThisData")
}
}
// PATH
let myDataPathFileName = "MyData.plist"
let myDataPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(myDataPathFileName)
// SAVE
let myDataToSave = MyData(dataIsThisDataSimple: true, dataAmountOfSecondsNeededToUnderstandThisData: 5)
NSKeyedArchiver.archiveRootObject(myDataToSave, toFile: myDataPath)
// LOAD
if let myData = NSKeyedUnarchiver.unarchiveObject(withFile: myDataPath) as? MyData {
print("This data is simple: \(myData.isThisDataSimple)")
print("And it took me only \(myData.amountOfSecondsNeededToUnderstandThisData) seconds to understand it!")
}Property List: Just a standard way of laying out organized data. You can save into property lists (like above), but if you want to work with static data, you can just visually write your data into a .plist file in Xcode and just simply fetch it in code when needed. Now there are so many varieties of how to fetch that data, which depend on how it is constructed anyway, so maybe I won't present any code on that. Instead you should probably look into it yourself. Read more: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/UnderstandXMLPlist/UnderstandXMLPlist.html
Hope that helped 😀!