ios :: data_store
app::data_store
using NSUserDefaults
Serializable::inspect
to show more useful information
serialization :: serialization_core
Abstract services to serialize Nit objects to different formatsdeserialize_json
and JsonDeserializer
serialize_to_json
and JsonSerializer
core :: union_find
union–find algorithm using an efficient disjoint-set data structurecocoa :: foundation
The Foundation Kit provides basic Objective-C classes and structures
# Implements `app::data_store` using `NSUserDefaults`
module data_store
import app::data_store
import cocoa::foundation
private import json
redef class DataStore
# The `NSUserDefaults` used to implement `DataStore`
var user_defaults = new NSUserDefaults.standard_user_defaults is lazy
redef fun [](key)
do
var nsstr = user_defaults.string_for_key(key.to_nsstring)
if nsstr.address_is_null then return null
# TODO report errors
var deserializer = new JsonDeserializer(nsstr.to_s)
var deserialized = deserializer.deserialize
var errors = deserializer.errors
if errors.not_empty then
# An update may have broken the versioning compatibility
print_error "{class_name} error at deserialization: {errors.join(", ")}"
return null # Let's be safe
end
return deserialized
end
redef fun []=(key, value)
do
var nsobject: NSString
if value == null then
nsobject = new NSString.nil
else
var serialized_string = new StringWriter
var serializer = new JsonSerializer(serialized_string)
serializer.serialize(value)
# TODO report errors
nsobject = serialized_string.to_s.to_nsstring
end
user_defaults.set_object(nsobject, key.to_nsstring)
end
end
lib/ios/data_store.nit:15,1--64,3