How to present a dictionary in a set method swift? - swift

I'm trying to do a set and get method to this Data manager class, from a dictionary and I don't know how to insert the values of the dictionary' in the set and get method (i have changed it from an array to Dic) thanks
class DataManager {
private static var sharedInstance: DataManager?
private var recordsArray: [[String:String]] = []
private let defaults = UserDefaults.standard
let userRecord: String = "userRecord";
private init(){}
public static func getInstance()-> DataManager{
if DataManager.sharedInstance == nil {
DataManager.sharedInstance = DataManager()
}
return DataManager.sharedInstance!
}
//here is my problem - set and get methods
//I don't know how to move the parameters
public func setRecordsArray([_:String,path:String]) {
self.recordsArray.append(?);
defaults.set(self.recordsArray, forKey: self.userRecord)
}
// again the same problem
public func getRecordsArray() -> [String] {
let a = self.defaults.array(forKey: self.userRecord);
return a as! [String];
}
}

The key to answering your question is to know the type of variable you want to set and get.
In this case, the recordsArray variable is an array of dictionaries of string values and keys: [[String:String]]
So, one way to pass this parameter is to create a variable of the same type as it should be set:
public func setRecordsArray(array:[[String:String]]) {
self.recordsArray = array
defaults.set(self.recordsArray, forKey: self.userRecord)
}
It simply updates the value of the self.recordsArray variable and sets the value in user defaults.
The get method works similar, however it returns a variable with the same type that should be returned.
A ? was added in the return because if there is no saved user defalts, the method returns nil:
public func getRecordsArray() -> [[String:String]]?{
if let array = defaults.object(forKey: self.userRecord) as? [[String:String]]{
self.recordsArray = array
return array
}
return nil
}
Also, you can make a set method for insert elements inside the array.
in this case, parameter type must be like [String:String], the element type of this array:
public func setRecord(record:[String:String]) {
if let array = defaults.object(forKey: self.userRecord) as? [[String:String]]{
self.recordsArray = array
self.recordsArray.append(record)
defaults.set(self.recordsArray, forKey: self.userRecord)
} else{
self.recordsArray.append(record)
defaults.set(self.recordsArray, forKey: self.userRecord)
}
}

Related

Swift objc_getAssociatedObject always nil

I am trying to associated a property to an Array Extension:
private var AssociatedObjectHandle: String = "BlaBLabla"
extension Array {
var emptyIndex:Int {
mutating get {
if let object = objc_getAssociatedObject(self, &AssociatedObjectHandle) {
return object as! Int
}
let index = self.searchEmptyIndex()
self.emptyIndex = index
return index
}
set {
let new = (newValue as NSInteger)
objc_setAssociatedObject(self, &AssociatedObjectHandle, new, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
func searchEmptyIndex() -> Int {
if let arr = self as? [Int] {
return arr.index(of: -1)!
}
return -1
}
}
The objc_getAssociatedObject call always returns nil!!
Anyone has any idea why?
I am banging my head for the last hour about this...
You cannot add associated objects to a Swift Array (or any Swift value type).
objc_setAssociatedObject() and objc_getAssociatedObject() are from the
Objective-C runtime, they expect an instance of NSObject as first
argument.
Your code compiles and runs only because any Swift value is automatically
bridged to an object if necessary.
When you call objc_setAssociatedObject(self, ...) then self
is bridged to a (temporary) instance of NSArray, and the association
is made on that object.
Later, when objc_getAssociatedObject(self, ...) is called,
another (temporary) instance of NSArray is created, and that
has no associated object.
That's why you get nil as the result.

Saving Array to Realm in Swift?

Is it possible to save an array of objects to Realm? Anytime I make a change to the array it should be saved to Realm.
My current solution is to save object for object with a for loop. For append/modifying objects calling save() will do the job, but not when I remove an object from it.
class CustomObject: Object {
dynamic var name = ""
dynamic var id = 0
override static func primaryKey() -> String? {
return "id"
}
}
struct RealmDatabase {
static var sharedInstance = RealmDatabase()
var realm: Realm!
let object0 = CustomObject()
let object1 = CustomObject()
var array = [object0, object1]
init() {
self.realm = try! Realm()
}
func save() {
for object in self.array {
try! self.realm.write {
self.realm.add(object, update: true)
}
}
}
}
To save lists of objects you have to use a Realm List, not a Swift Array.
let objects = List<CustomObject>()
Then, you can add elements:
objects.append(object1)
Take a look at to many relationships and Collections sections of the official docs.
Swift 3
func saveRealmArray(_ objects: [Object]) {
let realm = try! Realm()
try! realm.write {
realm.add(objects)
}
}
And then call the function passing an array of realm 'Object's:
saveRealmArray(myArray)
Note: realm.add(objects) has the same syntax of the add function for a single object, but if you check with the autocompletion you'll see that there is: add(objects: Sequence)

Set new value with computed properties

i have a model object and i want to set new value to the object.
bellow i have mentioned the object variable and initialization
private var _data: NSMutableDictionary
// MARK:- Init
init(data: [String: AnyObject])
{
_data = NSMutableDictionary(dictionary: data)
}
How can i set a new value with setter. bellow i have mentioned the computed variable
var name: String? {
get {
if let nameObject = _data.objectForKey("network")?.objectForKey("name") {
return (nameObject as! String)
} else {
return nil
}
}
set {
}
}
need to complete the setter
Edit
I could found the answer for this question.
I need to change the name of the _data object. So i have to update name without change other variables in side the _data. so i keep a mutable copy of the _data object and changed the new value there. then updated the _data object.
Something like
set {
_data.objectForKey("network")?.setObject(newValue, forKey: "name")
}
How about this:
var name: String? {
get {
return _data["network"]?["name"] as? String
}
set {
if let network = _data["network"] as? Dictionary {
network["name"] = newValue
} else {
let network: [String:AnyObject] = ["name": newValue]
data["network"] = network
}
}
}
The setter also takes care of the situation the network key doesn't exist. If also _data can be nil then an extra if-let can be added to address this situation too.
BTW, I also added a shorter version of the getter.
Not really sure what you are trying to do here but can't you just write this:
set {
_data.objectForKey("network")?.objectForKey("name") = newValue
}
Edit: Wrong function, should have been:
set {
_data.objectForKey("network")?.setObject(newValue, forKey: "name")
}

Mutating property by its name

With the help of Reflection API I'm getting the properties list for my types.
func inspectedProperties(ignored: [String] = []) -> [Property] {
var properties = [String]()
for child in self.children() {
guard let label = child.label else {
continue
}
properties += [label]
}
return properties.filter { !ignored.contains($0) }
}
This function returns me the names for all properties.
Now I want to mutate a certain property just by knowing its name.
class Fruit {
private dynamic var name = "Apple"
}
If I call Fruit().inspectedProperties() I'll get the following array ["name"].
But is it possible to mutate the variable named "name"?
OK, I found a very simple solution but it is not flexible. Actually, you can use KVO to mutate your data types. For this purpose your models should be subclasses of NSObject to enable KVO features and variables marked as dynamic.
P.S.
typealias Property = String
class Fruit: NSObject {
private dynamic var name = "Apple"
}
Then there is the mutating function.
func mutateProperty<T>(property: Property) -> T -> () {
return { value in
let filtered = self.children().filter { label, value in
if let label = label where label == property {
return true
}
return false
}
guard let child = filtered.first else {
return
}
if let object = self as? NSObject where child.value is T {
object.setValue(value as? AnyObject, forKey: property)
}
}
}
Then try out:
let fruit = Fruit()
fruit.mutateProperty("name")("Google")
print(fruit.name) // "Google"
It works, but if you want to work with value types rather than with reference ones it won't work. There might be some low level solution but I'm not familiar with one. If anyone knows how to, please leave your answer here! :)

Is there a way to set associated objects in Swift?

Coming from Objective-C you can call function objc_setAssociatedObject between 2 objects to have them maintain a reference, which can be handy if at runtime you don't want an object to be destroyed until its reference is removed also. Does Swift have anything similar to this?
Here is a simple but complete example derived from jckarter's answer.
It shows how to add a new property to an existing class. It does it by defining a computed property in an extension block. The computed property is stored as an associated object:
import ObjectiveC
// Declare a global var to produce a unique address as the assoc object handle
private var AssociatedObjectHandle: UInt8 = 0
extension MyClass {
var stringProperty:String {
get {
return objc_getAssociatedObject(self, &AssociatedObjectHandle) as! String
}
set {
objc_setAssociatedObject(self, &AssociatedObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
EDIT:
If you need to support getting the value of an uninitialized property and to avoid getting the error unexpectedly found nil while unwrapping an Optional value, you can modify the getter like this:
get {
return objc_getAssociatedObject(self, &AssociatedObjectHandle) as? String ?? ""
}
The solution supports all the value types as well, and not only those that are automagically bridged, such as String, Int, Double, etc.
Wrappers
import ObjectiveC
final class Lifted<T> {
let value: T
init(_ x: T) {
value = x
}
}
private func lift<T>(x: T) -> Lifted<T> {
return Lifted(x)
}
func setAssociatedObject<T>(object: AnyObject, value: T, associativeKey: UnsafePointer<Void>, policy: objc_AssociationPolicy) {
if let v: AnyObject = value as? AnyObject {
objc_setAssociatedObject(object, associativeKey, v, policy)
}
else {
objc_setAssociatedObject(object, associativeKey, lift(value), policy)
}
}
func getAssociatedObject<T>(object: AnyObject, associativeKey: UnsafePointer<Void>) -> T? {
if let v = objc_getAssociatedObject(object, associativeKey) as? T {
return v
}
else if let v = objc_getAssociatedObject(object, associativeKey) as? Lifted<T> {
return v.value
}
else {
return nil
}
}
A possible
Class extension (Example of usage)
extension UIView {
private struct AssociatedKey {
static var viewExtension = "viewExtension"
}
var referenceTransform: CGAffineTransform? {
get {
return getAssociatedObject(self, associativeKey: &AssociatedKey.viewExtension)
}
set {
if let value = newValue {
setAssociatedObject(self, value: value, associativeKey: &AssociatedKey.viewExtension, policy: objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
}
I wrote a modern wrapper available at https://github.com/b9swift/AssociatedObject
You may be surprised that it even supports Swift structures for free.
Obviously, this only works with Objective-C objects. After fiddling around with this a bit, here's how to make the calls in Swift:
import ObjectiveC
// Define a variable whose address we'll use as key.
// "let" doesn't work here.
var kSomeKey = "s"
…
func someFunc() {
objc_setAssociatedObject(target, &kSomeKey, value, UInt(OBJC_ASSOCIATION_RETAIN))
let value : AnyObject! = objc_getAssociatedObject(target, &kSomeKey)
}
Update in Swift 3.0
For example this is a UITextField
import Foundation
import UIKit
import ObjectiveC
// Declare a global var to produce a unique address as the assoc object handle
var AssociatedObjectHandle: UInt8 = 0
extension UITextField
{
var nextTextField:UITextField {
get {
return objc_getAssociatedObject(self, &AssociatedObjectHandle) as! UITextField
}
set {
objc_setAssociatedObject(self, &AssociatedObjectHandle, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
Klaas answer just for Swift 2.1:
import ObjectiveC
let value = NSUUID().UUIDString
var associationKey: UInt8 = 0
objc_setAssociatedObject(parentObject, &associationKey, value, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
let fetchedValue = objc_getAssociatedObject(parentObject, &associationKey) as! String
Just add #import <objc/runtime.h> on your brindging header file to access objc_setAssociatedObject under swift code
The above friend has answered your question, but if it is related to closure properties, please note:
```
import UIKit
public extension UICollectionView {
typealias XYRearrangeNewDataBlock = (_ newData: [Any]) -> Void
typealias XYRearrangeOriginaDataBlock = () -> [Any]
// MARK:- associat key
private struct xy_associatedKeys {
static var originalDataBlockKey = "xy_originalDataBlockKey"
static var newDataBlockKey = "xy_newDataBlockKey"
}
private class BlockContainer {
var rearrangeNewDataBlock: XYRearrangeNewDataBlock?
var rearrangeOriginaDataBlock: XYRearrangeOriginaDataBlock?
}
private var newDataBlock: BlockContainer? {
get {
if let newDataBlock = objc_getAssociatedObject(self, &xy_associatedKeys.newDataBlockKey) as? BlockContainer {
return newDataBlock
}
return nil
}
set(newValue) {
objc_setAssociatedObject(self, xy_associatedKeys.newDataBlockKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
}
convenience init(collectionVewFlowLayout : UICollectionViewFlowLayout, originalDataBlock: #escaping XYRearrangeOriginaDataBlock, newDataBlock: #escaping XYRearrangeNewDataBlock) {
self.init()
let blockContainer: BlockContainer = BlockContainer()
blockContainer.rearrangeNewDataBlock = newDataBlock
blockContainer.rearrangeOriginaDataBlock = originalDataBlock
self.newDataBlock = blockContainer
}
```
For 2022, now very simple:
// Utils-tags.swift
// Just a "dumb Swift trick" to add a string tag to a view controller.
// For example, with UIDocumentPickerViewController you need to know
// "which button was clicked to launch a picker"
import UIKit
private var _docPicAssociationKey: UInt8 = 0
extension UIDocumentPickerViewController {
public var tag: String {
get {
return objc_getAssociatedObject(self, &_docPicAssociationKey)
as? String ?? ""
}
set(newValue) {
objc_setAssociatedObject(self, &_docPicAssociationKey,
newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}