osx - Custom NSFormatter returning nil in swift -


I have an NSFormatter in Swift, which is linked to an NSTextField. It prevents illegal characters from being entered, but when I try to reach the value of the next area, it returns zero value.

The class below is:

  class PSEntryNameFormatter: NSFormat {Override function string forebbank value (OBJ: Oabbed?) - & gt; String? {If obj == blue {println ("stringForObjectValue: obj is zero, do not return zero") return blue} If O = obj in the form of? String {println ( "stringForObjectValue: obj string, returning \ (o)") return o}: nil} override getObjectValue (obj function return println ( "obj is not zero return, string stringForObjectValue") :? AutoreleasingUnsafeMutablePointer & LT ; AnyObject & gt; For String String: String, Error Detection Error: AutoreleasingUnsafeMutablePointer & lt; NSString? & Gt;) - & gt; BULL {println ("getObjectValue: \ (string)") obj = string returns true} overdraided funparential stringwalid (partial string: string ?, newEditingString newString: AutoreleasingUnsafeMutablePointer & lt; NSString? & Gt ;, ErrorConversion Error: AutoreleasingUnsafeMutablePointer & lt; NSString ? & Gt;) - & gt; Bool {if f = partial string {var illegal: String = sum ( "", s.componentsSeparatedByCharactersInSet (PSEntryNameFormatterCharacterSet)) various objects = s.componentsSeparatedByCharactersInSet (NSCharacterSet (charactersInString: illegal)) The new string: NSString = join ( "", Materials) If the string (newString) == s:} println ( "isPartialStringValid: partial string right mind" {println ( "isPartialStringValid partial string") return true} return) Unreal}}  

And how I try to use here:

  Funk control (Control: NSC control, T Steldandting field from: Anstiid) - & gt; Bool {println (should end "text editing") if (control == nameTextField) {var name string as = nameTextField.objectValue setObjectName (name)} return true}  

Debugging I println statement to add, and what happens when you set the text fields 'blueprint' here and then I remove the two characters:

  stringForObjectValue: obj string, the blueprint stringForObjectValue return: Obj string is, returning template isPartialStringValid: fine partial string getObjectValue: templat isPartialStringValid: partial string getObjectValue: templa  

Then I press Enter:

  getObjectValue: Templa text editing should finish getObjectValue: Templa stringForObjectValue: obj, nil getObjectValue return zero: stringForObjectValue : Obj, is not equal to stringForObjectValue Returning to zero: obj is not equal to, Serious error returns zero: found unexpectedly, while unwrapping an optional value zero  

and then it crashes On the line when I cast to the wire, obviously I can prevent the crash But first I have to find out why he did not return any help would be greatly appreciated!

The problem is in your getObjectValue function.

You should assign a value in this manner:

obj.memory = string

instead of

  Obj = string =  

Comments