Unexpected results from code in extension viewWillAppear

Using Xcode 12 and for a macOS program.

I created a test project to run down this error. The test app simply has a window controller/view controller that I added a search tool bar item to. While running searchFieldToolbarItem.view line of code crashes due to nil value. The class hierarchy from the debugger has the baseNSObject at the third level where as I was expecting this a the first level. I have this same code in another app and in debugger it shows baseNSObject at the top level. What am I missing and any ideas how to fix this?

Code Block
import Cocoa
var custSearchField: NSSearchField = NSSearchField()
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
override func viewWillAppear() {
guard let toolbar = self.view.window?.toolbar else {
return
}
guard let searchFieldToolbarItem = toolbar.items.last else {
return
}
custSearchField = (searchFieldToolbarItem.view as? NSSearchField)!
}
}

Answered by BigEagle in 644844022
Finally got back to this last night. Problem solved. I had the wrong object in the toolbar for the search item. For this to work I needed a search field in the toolbar instead of a search field tool bar item.
So, I tested by adding a Toolbar and a search item inside, at the rightmost position.

And it works.

So check the search item is effectively the last item (rightmost) in the toolbar.
Or explore the toolbar items to get it.

Code Block
override func viewWillAppear() {
guard let toolbar = self.view.window?.toolbar else { return }
for item in toolbar.items where item.view is NSSearchField {
let custSearchField = item.view
}
}


Thanks Claude for your answer, the search field is last but I changed the the code to the following: This doesn't not crash but it does not add the menu in the toolbar search item. Also, as I mentioned I have basically the same code that works fine in another customer view controller. So, I don't see why this new code seems to behave differently.

What I am wanting to do is the following:
  1. Add a search field item to the window controller toolbar

  2. Expose the search field from the toolbar to the view controller with the viewWillAppear override

  3. From the view controller code add my search options (In this case ALL, Number, Label and Amount) as well as the search function itself.

Code Block
var itemSearchField: NSSearchField = NSSearchField() // Global variable
  // ToolBar Search
    override func viewWillAppear() {
         guard let toolbar = self.view.window?.toolbar else {
            return
        }
        for item in toolbar.items where item.view is NSSearchField {
                    itemSearchField = (item.view as? NSSearchField)! // Point itemSearchField to toolbar search item
              }
        createMenuForSearchField() // Create Menu for Search Field
        itemSearchField.target = self
        itemSearchField.action = #selector (itemSearch (sender:))
    }

Also, I tried a few variations of you code but no success.

Please advise
Accepted Answer
Finally got back to this last night. Problem solved. I had the wrong object in the toolbar for the search item. For this to work I needed a search field in the toolbar instead of a search field tool bar item.
Unexpected results from code in extension viewWillAppear
 
 
Q