Problems with NSTokenView (again) - using objectvalue

swift macos cocoa

145 просмотра

1 ответ

16 Репутация автора

In my Swift Application for Mac OS X, I'm using a NSTokenField. I'm using, among others, the delegate methods tokenField(tokenField: NSTokenField, representedObjectForEditingString editingString: String) -> AnyObject and tokenField(tokenField: NSTokenField, displayStringForRepresentedObject representedObject: AnyObject) -> String? to work with represented objects. My represented objects are instances of a custom class.

From the Apple Documentation, I know that objectValue can be used to access the represented objects of a NSTokenView as a NSArray:

To retrieve the objects represented by the tokens in a token field, send the token field an objectValue message. Although this method is declared by NSControl, NSTokenField implements it to return an array of represented objects. If the token field simply contains a series of strings, objectValue returns an array of strings. To set the represented objects of a token field, use the setObjectValue: method, passing in an array of represented objects. If these objects aren’t strings, NSTokenField then queries its delegate for the display strings to use for each token.

However, this doesn't seem to work for me. let tokenArray = self.tokenField.objectValue! as! NSArray does return a NSArray, but it is empty, even though the delegate method required to return a represented object has been called the appropriate amount of times before.

NSTokenView doesn't seem like a particularly strong tool to work with tokenization, but, lacking an alternative, I hope that you guys can help me making my implementation work.

Thanks in advance!

Автор: Jonah Mevert Источник Размещён: 18.07.2016 05:20

Ответы (1)


0 плюса

460 Репутация автора

This works for me:

let tokenStrings = tokenField.objectValue as! [String]

where tokenField is the NSTokenField that you want to extract the strings from.

Автор: closetCoder Размещён: 08.10.2016 09:45
Вопросы из категории :
32x32