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
}
}









share|improve this question
























  • 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

















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
}
}









share|improve this question
























  • 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















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
}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin