How to access property or method from a variable? - ios

Is it possible to access a method or property using a variable as the name of the method or property in Swift?
In PHP you can use $object->{$variable}. For example
class Object {
public $first_name;
}
$object = new Object();
$object->first_name = 'John Doe';
$variable = 'first_name';
$first_name = $object->{$variable}; // Here we can encapsulate the variable in {} to get the value first_name
print($first_name);
// Outputs "John Doe"
EDIT:
Here is the actual code I'm working with:
class Punchlist {
var nid: String?
var title: String?
init(nid: String) {
let (result, err) = SD.executeQuery("SELECT * FROM punchlists WHERE nid = \(nid)")
if err != nil {
println("Error")
}
else {
let keys = self.getKeys() // Get a list of all the class properties (in this case only returns array containing "nid" and "title")
for row in result { // Loop through each row of the query
for field in keys { // Loop through each property ("nid" and "title")
// field = "nid" or "title"
if let value: String = row[field]?.asString() {
// value = value pulled from column "nid" or "title" for this row
self.field = value //<---!! Error: 'Punchlist' does not have a member named 'field'
}
}
}
}
}
// Returns array of all class properties
func getKeys() -> Array<String> {
let mirror = reflect(self)
var keys = [String]()
for i in 0..<mirror.count {
let (name,_) = mirror[i]
keys.append(name)
}
return keys
}
}

You can do it, but not using "pure" Swift. The whole point of Swift (as a language) is to prevent that sort of dangerous dynamic property access. You'd have to use Cocoa's Key-Value Coding feature:
self.setValue(value, forKey:field)
Very handy, and it crosses exactly the string-to-property-name bridge that you want to cross, but beware: here be dragons.
(But it would be better, if possible, to reimplement your architecture as a dictionary. A dictionary has arbitrary string keys and corresponding values, and thus there is no bridge to cross.)

Subscripting may help you.
let punch = Punchlist()
punch["nid"] = "123"
println(punch["nid"])
class Punchlist {
var nid: String?
var title: String?
subscript(key: String) -> String? {
get {
if key == "nid" {
return nid
} else if key == "title" {
return title
}
return nil
}
set {
if key == "nid" {
nid = newValue
} else if key == "title" {
title = newValue
}
}
}
}

Related

Why does the Realm Set object work at random?

The function to use a set of Realm objects is always random.
Primary keys must not be changed and they must be unique.
So I added another variable for compare.
And I override isEqual(:) function.
See below my code.
class Model: Object {
#objc dynamic var key = ""
#objc dynamic var id = ""
override static func primaryKey() -> String? {
return "key"
}
override func isEqual(_ object: Any?) -> Bool {
if let object = object as? Model {
return id == object.id
} else {
return false
}
}
}
let model1 = Model()
model1.key = UUID().uuidString
model1.id = "hi"
let model2 = Model()
model2.key = UUID().uuidString
model2.id = "hi"
let model1Array = [model1]
let model2Array = [model2]
let set1 = Set(model1Array)
let set2 = Set(model2Array)
let result = set1.intersection(set2)
print(result) // []
let result = set1.intersection(set2)
print(result) // [Model { key = 9E814B97-D0CC-4550-BF7B-19645C1DB746; id = hi; }]
let result = set1.intersection(set2)
print(result) // []
let result = set1.intersection(set2)
print(result) // []
let result = set1.intersection(set2)
print(result) // [Model { key = 8A399388-1FA2-4699-8258-5DA5DFCEC203; id = hi; }]
Every time I run, the values come out randomly.
What did I do wrong?
For Set to work correctly, your objects need to have a correct implementation of Hashable. The Realm Object already implements Hashable, and presumably, the == implementation calls isEqual.
However, the hash should be consistent with isEqual as well, but you haven't overridden hash yet. You should implement hash such that two equal objects (as determined by isEqual) have equal hashes.
One way is to do it like this:
override var hash: Int {
return id.hash
}

Realm model contains property but not found it at runtime

I am getting error as below
Invalid property name , reason: 'Property 'IsRecordDeleted' not found in object of type 'MyCustomModel'
Where as my Model is as Under
#objcMembers public class MyCustomModel : Object {
dynamic var Id : String = ""
dynamic var ProductId : String? = ""
dynamic var IsRecordDeleted : Bool? = false
dynamic var ProductBarcode : String? = ""
override public class func primaryKey() -> String? {
return "Id"
}
}
and I am making query like this :
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self).filter("IsRecordDeleted = false")
What could be problem here. I do not know why my app is crashing with the same error. But If I change the value like
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self).filter("ProductId = 0")
The app gets run, but crashed on when I use IsRecordDeleted in predicate.
Please tell me what could be problem
You can try
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self)
let filtered = mSavedItems.filter { $0.IsRecordDeleted == false }
and
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self)
let filtered = mSavedItems.filter { $0.ProductId == "0" }
For both
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self)
let filtered = mSavedItems.filter {
$0.IsRecordDeleted == false
&& $0.ProductId == "0"
}
//
let resultPredicate = NSPredicate(format: "ProductId == '0' AND IsRecordDeleted == false")
let filtered = mSavedItems.filter(resultPredicate)
I think you'll find that simply switching to filter blocks instead of string predicates might stop the crash, but will not produce the expected results.
This is because IsRecordDeleted never gets saved to the database. It is not a type that can be represented in Objective-C, therefore it cannot be dynamic, so Realm ignores it.
Take as an example the following class:
#objcMembers class MyObject: Object {
dynamic var id = ""
dynamic var testBool: Bool? = false
override static func primaryKey() -> String {
return "id"
}
}
And say we initialize them like this:
let obj1 = MyObject()
obj1.id = "1"
obj1.testBool = true
let obj2 = MyObject()
obj2.id = "2"
obj2.testBool = false
let realm = try? Realm()
try? realm?.write {
realm?.add(obj1, update: true)
realm?.add(obj2, update: true)
}
If we query Realm for these objects using realm.objects(MyObject.self), you'll get something like this
Results<MyObject> <0x7fe410c0ad90> (
[0] MyObject {
id = 1;
},
[1] MyObject {
id = 2;
}
)
And you'll see that in the database, there indeed is no property named testBool, which was our optional Bool.
You can easily see that the optional Bool may cause problems if you write it out like this instead:
class MyObject: Object {
#objc dynamic var id = ""
#objc dynamic var testBool: Bool? = false // This line will not compile.
override static func primaryKey() -> String {
return "id"
}
}
I'm curious why the IsRecordDeleted needs to be optional in the first place, since it seems to have a value from the get-go. If it doesn't, then something like this will work as expected:
#objcMembers public class MyCustomModel: Object {
dynamic var Id: String = ""
dynamic var ProductId: String? = ""
dynamic var IsRecordDeleted: Bool = false
dynamic var ProductBarcode: String? = ""
override public class func primaryKey() -> String? {
return "Id"
}
}
and you can query via string like you were trying to do in the first place.
If it has to be optional, then Realm provides a RealmOptional for this exact case, that you can look into here.

How to use multiple key for the same value in an Swift Dictionary?

How can I store user records into Swift Dictionary, but index them by two type of keys: userID and firstName? Is it possible?
If a value is removed both expression users[aUserID] and user[aFirstName] should return nil.
Dictionaries don't care about unique values, they only care about unique keys. So you can have as many identical values in a dictionary as you like, as long as their keys are all different.
The way I would approach your problem is by hiding all the necessary logic to keep the dictionary keys in sync in a wrapper class:
struct User {
let userId: Int
let firstName: String
// more user properties
}
class UserDict {
private var dict = [String: User]()
subscript(key: String) -> User? {
return dict[key]
}
subscript(key: Int) -> User? {
return dict["\(key)"]
}
func add(_ user: User) {
dict[user.firstName] = user
dict["\(user.userId)"] = user
}
func remove(forKey key: String) {
if let user = dict[key] {
dict.removeValue(forKey: user.firstName)
dict.removeValue(forKey: "\(user.userId)")
}
}
func remove(forKey key: Int) {
remove(forKey: "\(key)")
}
var count: Int {
return dict.count / 2
}
}
You can have multiple keys for the same value, consider this example:
class Value {
var name = "John"
}
var dictionary: [String: Value] = [:]
var value1 = Value()
dictionary["key1"] = value1
dictionary["key2"] = value1
value1.name = "Theresa"
dictionary["key1"]?.name // "Theresa"
dictionary["key2"]?.name // "Theresa"
For the second part of your question:
If value is removed both expression users[aUserID] and user[aFirstName] should return nil.
When you do this dict["key1"] = nil, the value for the key2 is still there and would be value1, so you could create a custom subscript that would handle that by finding identical values in the dictionary.

Create an Array of Object from an Array of Dictionaries with Swift

I'm receiving a JSON dictionary from a web service and I need to map the return values to existing values. Here's essentially what I'm trying to do:
class Contract {
var contractID: String?
var ebState: String?
var ibState: String?
var importerState: String?
var exportersBankRefNo: String?
var importersBankRefNo: String?
}
let contract1 = Contract()
contract1.contractID = "001"
let contract2 = Contract()
contract2.contractID = "002"
// This is the JSON return dictionary
let exportAppnStatusList: [[String: String]] = [["contractID":"001",
"ExporterBankRefNo":"ExporterBankRefNo001",
"ExporterBankState":"ACCEPTED",
"ImporterBankRefNo":"",
"ImporterBankState":"UNKNOWN",
"ImporterState":"UNKNOWN" ],
["contractID":"002",
"ExporterBankRefNo":"ExporterBankRefNo002",
"ExporterBankState":"ACCEPTED",
"ImporterBankRefNo":"ImporterBankRefNo002",
"ImporterBankState":"ACCEPTED",
"ImporterState":"UNKNOWN" ]]
I need to take the exportAppnStatusList and fill in the associated values in the existing contract1 and contract2, mapping by the contractID
This fills the contracts with available information, it ignores contracts where the id could not be found:
for contract in [contract1, contract2] {
if let contractDict = exportAppnStatusList.filter({$0["contractID"] == contract.contractID}).first {
contract.exportersBankRefNo = contractDict["ExporterBankRefNo"]
contract.ebState = contractDict["ExporterBankState"]
contract.importersBankRefNo = contractDict["ImporterBankRefNo"]
contract.ibState = contractDict["ImporterBankState"]
contract.importerState = contractDict["ImporterState"]
}
}
Why not generate the contract object by mapping over the array of dictionaries like this? You'll need to write a custom initializer that takes all these params
exportAppnStatusList.map { (dict:[Stirng:String]) -> Contract in
return Contract(contractID:dict["contractID"],
ebState:dict["ExporterBankState"],
ibState:dict["ImporterBankState"],
importerState:dict["ImporterState"],
exportersBankRefNo:dict["ExporterBankRefNo"],
importersBankRefNo:dict["ImporterBankRefNo"]
}
Try using this init (your class must inherit from NSObject):
init(jsonDict: [String: String]) {
super.init()
for (key, value) in jsonDict {
if class_respondsToSelector(Contract.self, NSSelectorFromString(key)) {
setValue(value, forKey: key)
}
}
}
Then you can do this:
exportAppnStatusList.forEach {
print(Contract(jsonDict: $0))
}

Two similar classes show different initialization errors

I found an error when I test some codes from Github.
class Profile {
let text: String
let date: String
let id: String?
init?(data: NSDictionary?) {
if let text = data?.valueForKeyPath(Test.Text) as? String {
self.text = text
if let date = data?.valueForKeyPath(Test.Created) as? String {
self.date = date
id = data?.valueForKeyPath(Test.ID) as? String
}
}else{
return nil
}
}
struct Test {
static let Text = "text"
static let Created = "created"
static let ID = "id"
}
}
The line of init? shows the error "constants self.data used before being initialized."
And I create a similar class of it, like
class Context {
let words: String
init?(text:String?) {
if let words = text {
self.words = words
}else{
return nil
}
}
}
This time it shows " all stored properties of class instance must be initialized before returing nil from an initializer."
For the first one , there is a workaround that I can delete the else block and give each properties an empty value would fix the error. However it would have me change the properties mutable.
( I don't want it to be mutable)
And for the second example, I just insert self.word = ""before the line of return nil could also fix the error.
But I really wonder why these similar cases show the different errors and realize the logic of Swift, and how can I fix it correctly?
Thank you for helping me.
Try this version of the code.
Code 1:
class Profile {
var text: String = ""
var date: String = ""
var id: String? = ""
init?(data: NSDictionary?) {
if let text = data?.valueForKeyPath(Test.Text) as? String {
self.text = text
if let date = data?.valueForKeyPath(Test.Created) as? String {
self.date = date
id = data?.valueForKeyPath(Test.ID) as? String
}
}else{
return nil
}
}
struct Test {
static let Text = "text"
static let Created = "created"
static let ID = "id"
}
}
Code 2:
class Context {
var words: String = ""
init?(text:String?) {
if let words = text {
self.words = words
} else {
return nil
}
}
}
York initializers are incomplete and that's why you get the message. Swift is type save, which means that all non optional properties must be initialized before the class is returned. Even when the class returns a nil. secondly, you can't call self (which is the class itself) if you haven't initialized the class. However, that should not be a problem in your case, since you've defined a root class. For your first class, please, implement the code like this and it should work.
class Profile {
struct Test {
static let Text = "text"
static let Created = "created"
static let ID = "id"
}
let text: String
let date: String
let id: String?
init?(data: NSDictionary?) {
guard let tempText = data?.valueForKeyPath(Test.Text) as? String else {
text = ""
date = ""
id = nil
return nil
}
text = tempText
if let tempDate = data?.valueForKeyPath(Test.Created) as? String {
date = tempDate
id = data?.valueForKeyPath(Test.ID) as? String
} else {
date = ""
id = nil
}
}
}
For the second class you need to do a similar thing, which means in the else statement give words a value and it should be okay.

Resources