You cannot refer to yourself from lazy property in a quick

I recently stumbled upon something strange in my code, I have been programming in one project for several days, but suddenly I tried to create a lazy property using this code:

import UIKit class CategoryViewController: UICollectionViewController { lazy var searchController: UISearchController = { let searchController = UISearchController(searchResultsController: nil) searchController.searchBar.frame = CGRect(x: 0, y: 0, width: CGRectGetWidth(self.collectionView.frame), height: 44) return searchController }() } 

Now every time I try to refer to myself using in a lazy block, it produces this:

 self.(<#NSObject#>) 

and cannot access any of the other properties.

If I try to hard code them, it produces:

 UICollectionView? does not have a member named 'frame' 

And the above code shows:

 Extra argument 'width' in call 

I already tried to rewrite the code and exclude derived data, build again, but the error persists. Any ideas as to why this could happen?

Thanks in advance

Update autocomplete field:

enter image description here

+5
source share
1 answer

UICollectionView? doesn't have a member named 'frame'

See the question mark? This means that it is optional. You cannot send any messages to Optional until you expand it. Expand it: self.collectionView!.frame

EDIT . The reason this worked before, and then your code broke, is because the Swift API has changed. Apple does this a lot, so you need to throw fists. You should be prepared to break the code every time you upgrade Xcode. They changed their minds three times about whether the UICollectionViewController collectionView should be optional or not. And I am not at all convinced that their answer is right now.

Further editing . Indicates that autocomplete works:

enter image description here

+1
source

Source: https://habr.com/ru/post/1209242/


All Articles