I have been coding out things the long way for quite some time and as my apps grow, the code gets sloppier and sloppier.
Anyways…
I am constantly needing to update multiple textfield at one time.
For instance I have a text field which I enter a sentence into.
I then process each word in that text fields string and pull out individual words based on length.
set theText to aTextFieldValue as string
if the length of (word 1 of theText) as string is greater than 1 then
set my tag1 to (word 1 of theText) as string
end if
if the length of (word 2 of theText) as string is greater than 1 then
set my tag2 to (word 2 of theText) as string
end if
… and so on.
This works, however I run into numerous situations where I have 20+ “tags”…
I want to do the following however I am missing something.
set theText to aTextFieldValue as string
set wordCount to count words of aTextFieldValue
repeat with i from 1 to wordCount
if the length of (word i of theText) is greater than 1 then
set my ("tag" & i) to (word 1 of theText) as string
end if
Obviously I am doing something wrong in the line :
set my (“tag” & i) to (word 1 of theText) as string
I always throw a “[<NSObject 0x7fff74eb8810> setValue:forUndefinedKey:]”
How do you go about changing multiple text fields in such a manner?
you can’t change variable names this way at runtime.
Alternatively put the tag variables into a list
set tagList to {my tag1, my tag2, my tag3}
set theText to aTextFieldValue as string
set wordCount to count words of aTextFieldValue
repeat with i from 1 to wordCount
if the length of (word i of theText) is greater than 1 then
set item i of tagList to (word i of theText) -- no coercion needed, theText is AS text anyway
end if
end repeat
assuming the tag. properties are references to the text fields, you have to call the setStringValue() method,
try this:
set tagList to {my tag1, my tag2, my tag3}
set theText to aTextFieldValue as string
set wordCount to count words of aTextFieldValue
repeat with i from 1 to wordCount
if the length of (word i of theText) is greater than 1 then
set currentTextField to item i of tagList
currentTextField's setStringValue(word i of theText)
end if
end repeat
uncheck the checkbox in the Bindings Inspector, select the target class on the left side and control-drag from the blue cube to the specific text field then select the appropriate property.
I figured it out!
I had it set up right, I just overlooked :
“currentTextField’s setStringValue(word i of theText)”
should be
currentTextField’s setStringValue:(word i of theText)