1

I recently came across something weird that i can't wrap my head around.

i was using a SearchBar as a UI element. (note: it is the UIView subclass and not UIViewController subclass that apple handily gives out.

But i wasn't able to assign the the delegate in the computed property but was able to in viewDidload().

@usesAutoLayout var imageSearchbar:UISearchBar = {
    let bar = UISearchBar()
    bar.delegate = self
    bar.searchBarStyle = .minimal
    bar.searchTextField.backgroundColor = .white
    return bar
}()

bar.delegate = self shows a compile time error


Cannot assign value of type '(CollectionViewController) -> () -> CollectionViewController' to type 'UISearchBarDelegate?'


I want to gain knowledge as to what is it that i'm missing. Help appreciated.

Ashutosh Mane
  • 11
  • 2
  • 6
  • 1
    It's a closure, not a computed property. https://stackoverflow.com/questions/31515805/difference-between-computed-property-and-property-set-with-closure And it looks like CollectionViewController needs UISearchBarDelegate protocol conformance. – cora Aug 27 '22 at 17:17

0 Answers0