RealmSwift ComboView
up vote
0
down vote
favorite
I have a comboBox in my cocoa app. I need it to display all of the string data inside the realm file. Each string is a name. Currently only the last entered name will display and I cant find the code to display all names.
ComboBox
name1
name2
name3
name4
etc.
here is the code
@IBOutlet weak var comboBox: NSComboBox!
var realm : Realm?
var theData : Results<NewData>?
override func viewDidLoad() {
super.viewDidLoad()
let config = SyncUser.current!.configuration(realmURL: Constants.REALM_URL, fullSynchronization: true, enableSSLValidation: true, urlPrefix: nil)
self.realm = try! Realm(configuration: config)
comboBox.reloadData()
comboBox.usesDataSource = true
comboBox.dataSource = self
}
extension PublisherViewController: NSComboBoxDataSource {
func numberOfItems(in comboBox: NSComboBox) -> Int {
return theData?.count ?? 0
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
comboBox.stringValue = (theData?.last?.addName) ?? ""
return true
}
}
swift macos cocoa realm
add a comment |
up vote
0
down vote
favorite
I have a comboBox in my cocoa app. I need it to display all of the string data inside the realm file. Each string is a name. Currently only the last entered name will display and I cant find the code to display all names.
ComboBox
name1
name2
name3
name4
etc.
here is the code
@IBOutlet weak var comboBox: NSComboBox!
var realm : Realm?
var theData : Results<NewData>?
override func viewDidLoad() {
super.viewDidLoad()
let config = SyncUser.current!.configuration(realmURL: Constants.REALM_URL, fullSynchronization: true, enableSSLValidation: true, urlPrefix: nil)
self.realm = try! Realm(configuration: config)
comboBox.reloadData()
comboBox.usesDataSource = true
comboBox.dataSource = self
}
extension PublisherViewController: NSComboBoxDataSource {
func numberOfItems(in comboBox: NSComboBox) -> Int {
return theData?.count ?? 0
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
comboBox.stringValue = (theData?.last?.addName) ?? ""
return true
}
}
swift macos cocoa realm
Well, right off the bat, if you're trying to use theData as your dataSource, it's empty so nothing would be displayed. You need to write the code to populate that array/Realm results from Realm. Second thing is the objectForValueAt is passed an int so you know which item in the array you need to return but it's unused and you're returning true, where it should be... a string.. and your setting the stringValue of the comboBox to the last element of the array each time. Take a look at the NSComboBox and datasource documentation.
– Jay
Nov 17 at 14:41
So I have it working and I have tried to figure out returning all strings from a property in the realmFile with no success. all I can return is either the first string or the last string. return theData?.last?.addName or return theData?.first?.addName
– Cartisim
2 days ago
The comboBox datasource is typically an array of objects. It could be strings, ints or something else. Looking at the NSComboBox docs, you'll see that the function func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) is passed an index Int of the item for it to display. for example when the box opens it's menu, that function is passed 0, then 1, then 2 then 3 etc. Those are the indexes of what's stored in the array. In this case you can use your Realm Results object (theData) as it behaves like an array. So you would need to return theData[index] in that function, not true.
– Jay
2 days ago
The problem is that when I return theData[index] I get this error [General] -[RLM:Managed 4 NewData copyWithZone:]: unrecognized selector sent to instance 0x600002626820
– Cartisim
2 days ago
That's because it needs to populate the Menu with strings. I slightly mis-spoke in my above comment. You would need to get your Realm object, get the string you want from it - like a name or email address and return that string. Again, the docs cover that, and there are a number of examples here on SO on how to populate that menu.
– Jay
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a comboBox in my cocoa app. I need it to display all of the string data inside the realm file. Each string is a name. Currently only the last entered name will display and I cant find the code to display all names.
ComboBox
name1
name2
name3
name4
etc.
here is the code
@IBOutlet weak var comboBox: NSComboBox!
var realm : Realm?
var theData : Results<NewData>?
override func viewDidLoad() {
super.viewDidLoad()
let config = SyncUser.current!.configuration(realmURL: Constants.REALM_URL, fullSynchronization: true, enableSSLValidation: true, urlPrefix: nil)
self.realm = try! Realm(configuration: config)
comboBox.reloadData()
comboBox.usesDataSource = true
comboBox.dataSource = self
}
extension PublisherViewController: NSComboBoxDataSource {
func numberOfItems(in comboBox: NSComboBox) -> Int {
return theData?.count ?? 0
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
comboBox.stringValue = (theData?.last?.addName) ?? ""
return true
}
}
swift macos cocoa realm
I have a comboBox in my cocoa app. I need it to display all of the string data inside the realm file. Each string is a name. Currently only the last entered name will display and I cant find the code to display all names.
ComboBox
name1
name2
name3
name4
etc.
here is the code
@IBOutlet weak var comboBox: NSComboBox!
var realm : Realm?
var theData : Results<NewData>?
override func viewDidLoad() {
super.viewDidLoad()
let config = SyncUser.current!.configuration(realmURL: Constants.REALM_URL, fullSynchronization: true, enableSSLValidation: true, urlPrefix: nil)
self.realm = try! Realm(configuration: config)
comboBox.reloadData()
comboBox.usesDataSource = true
comboBox.dataSource = self
}
extension PublisherViewController: NSComboBoxDataSource {
func numberOfItems(in comboBox: NSComboBox) -> Int {
return theData?.count ?? 0
}
func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
comboBox.stringValue = (theData?.last?.addName) ?? ""
return true
}
}
swift macos cocoa realm
swift macos cocoa realm
edited Nov 17 at 14:31
Jay
17.9k42848
17.9k42848
asked Nov 15 at 13:24
Cartisim
86
86
Well, right off the bat, if you're trying to use theData as your dataSource, it's empty so nothing would be displayed. You need to write the code to populate that array/Realm results from Realm. Second thing is the objectForValueAt is passed an int so you know which item in the array you need to return but it's unused and you're returning true, where it should be... a string.. and your setting the stringValue of the comboBox to the last element of the array each time. Take a look at the NSComboBox and datasource documentation.
– Jay
Nov 17 at 14:41
So I have it working and I have tried to figure out returning all strings from a property in the realmFile with no success. all I can return is either the first string or the last string. return theData?.last?.addName or return theData?.first?.addName
– Cartisim
2 days ago
The comboBox datasource is typically an array of objects. It could be strings, ints or something else. Looking at the NSComboBox docs, you'll see that the function func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) is passed an index Int of the item for it to display. for example when the box opens it's menu, that function is passed 0, then 1, then 2 then 3 etc. Those are the indexes of what's stored in the array. In this case you can use your Realm Results object (theData) as it behaves like an array. So you would need to return theData[index] in that function, not true.
– Jay
2 days ago
The problem is that when I return theData[index] I get this error [General] -[RLM:Managed 4 NewData copyWithZone:]: unrecognized selector sent to instance 0x600002626820
– Cartisim
2 days ago
That's because it needs to populate the Menu with strings. I slightly mis-spoke in my above comment. You would need to get your Realm object, get the string you want from it - like a name or email address and return that string. Again, the docs cover that, and there are a number of examples here on SO on how to populate that menu.
– Jay
2 days ago
add a comment |
Well, right off the bat, if you're trying to use theData as your dataSource, it's empty so nothing would be displayed. You need to write the code to populate that array/Realm results from Realm. Second thing is the objectForValueAt is passed an int so you know which item in the array you need to return but it's unused and you're returning true, where it should be... a string.. and your setting the stringValue of the comboBox to the last element of the array each time. Take a look at the NSComboBox and datasource documentation.
– Jay
Nov 17 at 14:41
So I have it working and I have tried to figure out returning all strings from a property in the realmFile with no success. all I can return is either the first string or the last string. return theData?.last?.addName or return theData?.first?.addName
– Cartisim
2 days ago
The comboBox datasource is typically an array of objects. It could be strings, ints or something else. Looking at the NSComboBox docs, you'll see that the function func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) is passed an index Int of the item for it to display. for example when the box opens it's menu, that function is passed 0, then 1, then 2 then 3 etc. Those are the indexes of what's stored in the array. In this case you can use your Realm Results object (theData) as it behaves like an array. So you would need to return theData[index] in that function, not true.
– Jay
2 days ago
The problem is that when I return theData[index] I get this error [General] -[RLM:Managed 4 NewData copyWithZone:]: unrecognized selector sent to instance 0x600002626820
– Cartisim
2 days ago
That's because it needs to populate the Menu with strings. I slightly mis-spoke in my above comment. You would need to get your Realm object, get the string you want from it - like a name or email address and return that string. Again, the docs cover that, and there are a number of examples here on SO on how to populate that menu.
– Jay
2 days ago
Well, right off the bat, if you're trying to use theData as your dataSource, it's empty so nothing would be displayed. You need to write the code to populate that array/Realm results from Realm. Second thing is the objectForValueAt is passed an int so you know which item in the array you need to return but it's unused and you're returning true, where it should be... a string.. and your setting the stringValue of the comboBox to the last element of the array each time. Take a look at the NSComboBox and datasource documentation.
– Jay
Nov 17 at 14:41
Well, right off the bat, if you're trying to use theData as your dataSource, it's empty so nothing would be displayed. You need to write the code to populate that array/Realm results from Realm. Second thing is the objectForValueAt is passed an int so you know which item in the array you need to return but it's unused and you're returning true, where it should be... a string.. and your setting the stringValue of the comboBox to the last element of the array each time. Take a look at the NSComboBox and datasource documentation.
– Jay
Nov 17 at 14:41
So I have it working and I have tried to figure out returning all strings from a property in the realmFile with no success. all I can return is either the first string or the last string. return theData?.last?.addName or return theData?.first?.addName
– Cartisim
2 days ago
So I have it working and I have tried to figure out returning all strings from a property in the realmFile with no success. all I can return is either the first string or the last string. return theData?.last?.addName or return theData?.first?.addName
– Cartisim
2 days ago
The comboBox datasource is typically an array of objects. It could be strings, ints or something else. Looking at the NSComboBox docs, you'll see that the function func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) is passed an index Int of the item for it to display. for example when the box opens it's menu, that function is passed 0, then 1, then 2 then 3 etc. Those are the indexes of what's stored in the array. In this case you can use your Realm Results object (theData) as it behaves like an array. So you would need to return theData[index] in that function, not true.
– Jay
2 days ago
The comboBox datasource is typically an array of objects. It could be strings, ints or something else. Looking at the NSComboBox docs, you'll see that the function func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) is passed an index Int of the item for it to display. for example when the box opens it's menu, that function is passed 0, then 1, then 2 then 3 etc. Those are the indexes of what's stored in the array. In this case you can use your Realm Results object (theData) as it behaves like an array. So you would need to return theData[index] in that function, not true.
– Jay
2 days ago
The problem is that when I return theData[index] I get this error [General] -[RLM:Managed 4 NewData copyWithZone:]: unrecognized selector sent to instance 0x600002626820
– Cartisim
2 days ago
The problem is that when I return theData[index] I get this error [General] -[RLM:Managed 4 NewData copyWithZone:]: unrecognized selector sent to instance 0x600002626820
– Cartisim
2 days ago
That's because it needs to populate the Menu with strings. I slightly mis-spoke in my above comment. You would need to get your Realm object, get the string you want from it - like a name or email address and return that string. Again, the docs cover that, and there are a number of examples here on SO on how to populate that menu.
– Jay
2 days ago
That's because it needs to populate the Menu with strings. I slightly mis-spoke in my above comment. You would need to get your Realm object, get the string you want from it - like a name or email address and return that string. Again, the docs cover that, and there are a number of examples here on SO on how to populate that menu.
– Jay
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320497%2frealmswift-comboview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Well, right off the bat, if you're trying to use theData as your dataSource, it's empty so nothing would be displayed. You need to write the code to populate that array/Realm results from Realm. Second thing is the objectForValueAt is passed an int so you know which item in the array you need to return but it's unused and you're returning true, where it should be... a string.. and your setting the stringValue of the comboBox to the last element of the array each time. Take a look at the NSComboBox and datasource documentation.
– Jay
Nov 17 at 14:41
So I have it working and I have tried to figure out returning all strings from a property in the realmFile with no success. all I can return is either the first string or the last string. return theData?.last?.addName or return theData?.first?.addName
– Cartisim
2 days ago
The comboBox datasource is typically an array of objects. It could be strings, ints or something else. Looking at the NSComboBox docs, you'll see that the function func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) is passed an index Int of the item for it to display. for example when the box opens it's menu, that function is passed 0, then 1, then 2 then 3 etc. Those are the indexes of what's stored in the array. In this case you can use your Realm Results object (theData) as it behaves like an array. So you would need to return theData[index] in that function, not true.
– Jay
2 days ago
The problem is that when I return theData[index] I get this error [General] -[RLM:Managed 4 NewData copyWithZone:]: unrecognized selector sent to instance 0x600002626820
– Cartisim
2 days ago
That's because it needs to populate the Menu with strings. I slightly mis-spoke in my above comment. You would need to get your Realm object, get the string you want from it - like a name or email address and return that string. Again, the docs cover that, and there are a number of examples here on SO on how to populate that menu.
– Jay
2 days ago