Reputation: 1832
In my Application we are using Realm Storage for storing values locally. I can Able To Store my Array Values As Sting. But Not Able To Store and Retrive Values As Array. Is It Possible To Store NSArray Values To Realm Object and Retrieve It As NSArray. Here Is The Code I Have Used To Store String Values:
class PracticeView: Object
{
dynamic var PracticeArray = ""
}
And Usage:
let realm:Realm = try! Realm()
let PracticeDetails = PracticeView()
PracticeDetails.PracticeArray = "Test String Values"
try! realm.write
{
realm.add(PracticeDetails)
}
print("Values In Realm Object: \(PracticeDetails.PracticeArray)")
//Result Will Be
Values In Realm Object: Test String Values
Upvotes: 3
Views: 1362
Reputation: 15991
No, Realm cannot store native arrays (Whether Objective-C NSArray
objects or Swift arrays) as properties of the Object
model classes.
In Realm Swift, there is an object called List
that lets you store an array of Realm Object
instances as children. These still can't be strings though, so it's necessary to encapsulate the strings in another Realm Object
subclass.
class Practice: Object {
dynamic var practice = ""
}
class PracticeView: Object {
let practiceList = List<Practice>()
}
let newPracticeView = PracticeView()
let newPractice = Practice()
newPractice.practice = "Test String Value"
newPracticeView.practiceList.append(newPractice)
let realm = try! Realm()
try! realm.write {
realm.add(newPracticeView)
}
For more information, I recommend checking out the 'To-Many Relationships' section of the Realm Swift documentation. :)
Upvotes: 5