I tried to use the method setString to change the text in a textfield and it didn’t work. Then I changed it to setStringValue and it worked. So what is setString used for?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
NSTextFielddoes not have asetString:method.NSTextFieldis a type ofNSControl, andNSControlhas asetStringValue:method.NSTextand its more famous subclassNSTextViewhave asetString:method. @John Boker is correct that the field editor is anNSText, but you still can’t sendsetString:to anNSTextField, even in edit mode. You’d need to get the field editor from the window and then callsetString:on that (not that you really should do that).While it is confusing to newcomers, there is a good rationale behind the different methods.
NSControlhas a “value”, and that value can take different types (setDoubleValue:,setObjectValue:, etc.) This allows me to set the value as a double, but then retrieve in as a string. For the broad range of possible controls, this makes good sense and is very flexible.NSTextis not a control; note how it doesn’t havesetAction:orsetTarget:either. Its purpose is to display and edit (attributed) strings. When you callstring, you are actually getting the real backing text storage, not just the value of the object in string form (asstringValuedoes forNSControl).That said, when you say “it didn’t work,” I hope you mean that you got a compiler warning about this that told you that
NSTextFieldmay not respond tosetString:. If you’re ignoring compiler warnings, then you’re going to have a lot of problems in ObjC.