0

I integrate autocomplete event in my app. when I configure textfield I got this error

Cannot subscript a value of type '[String : AnyObject]' with an index of type 'NSAttributedStringKey'

Please check below code:

fileprivate func configureTextField(){
    print("i am here 4")
    autocompleteTextfield.autoCompleteTextColor = UIColor(red: 128.0/255.0, green: 128.0/255.0, blue: 128.0/255.0, alpha: 1.0)
    autocompleteTextfield.autoCompleteTextFont = UIFont(name: "HelveticaNeue-Light", size: 12.0)!
    autocompleteTextfield.autoCompleteCellHeight = 35.0
    autocompleteTextfield.maximumAutoCompleteCount = 20
    autocompleteTextfield.hidesWhenSelected = true
    autocompleteTextfield.hidesWhenEmpty = true
    autocompleteTextfield.enableAttributedText = true
    ***var attributes = [String:AnyObject]()
    attributes[NSForegroundColorAttributeName] = UIColor.black
    attributes[NSFontAttributeName] = UIFont(name: "HelveticaNeue-Bold", size: 12.0)
    autocompleteTextfield.autoCompleteAttributes = attributes***           
}

I got error on NSForegroundColorAttributeName,NSFontAttributeName and attributes and for autocomplete I import autocomplete UITextfield file. I got above error in below method:

> open var autoCompleteAttributes:[String:AnyObject]?
  fileprivate func commonInit(){
      hidesWhenEmpty = true
      autoCompleteAttributes = [NSForegroundColorAttributeName.rawValue:UIColor.black]
      autoCompleteAttributes![NSFontAttributeName] = UIFont.boldSystemFont(ofSize: 12)
      self.clearButtonMode = .always
      self.addTarget(self, action: #selector(AutoCompleteTextField.textFieldDidChange), for: .editingChanged)
      self.addTarget(self, action: #selector(AutoCompleteTextField.textFieldDidEndEditing), for: .editingDidEnd)
  }

I got error autoCompleteAttributes![NSFontAttributeName] this line. I do not what happen please check code...

Amit
  • 4,837
  • 5
  • 31
  • 46
Faster
  • 41
  • 2
  • 11
  • Possible duplicate of [Cannot convert value of type '\[String : AnyObject\]?' to expected argument type '\[NSAttributedStringKey : Any\]?'](https://stackoverflow.com/questions/45695286/cannot-convert-value-of-type-string-anyobject-to-expected-argument-type) – Tamás Sengel Oct 25 '18 at 11:16
  • You need to change your dictionary type to `[NSAttributedStringKey: Any]` – Leo Dabus Oct 25 '18 at 12:46

0 Answers0