iosswiftxlpagertabstrip

Variable 'self' was written to, but never read warning message when XLPagerTabStrip is used


I am using the cocoapod XLPagerTabStrip to implement PagerTabStrip in my iOS app. I get the below warning message while building or running the app

Variable 'self' was written to, but never read

This warning occurs from the following closure code that is used to change the item text color on swipe.

changeCurrentIndexProgressive = { [weak self] (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
    guard changeCurrentIndex == true else { return }
    oldCell?.label.textColor = .lightGray
    newCell?.label.textColor = .black
}

Can someone please help me to get rid of this warning message?


Solution

  • You can safely remove [weak self] from the closure capture list since you are not referring to it in the closure itself, therefore the compiler warns you that there is no need for it to be captured.

    The closure should look like this

    changeCurrentIndexProgressive = { (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
        guard changeCurrentIndex == true else { return }
        oldCell?.label.textColor = .lightGray
        newCell?.label.textColor = .black
    }
    

    Read more about Closures and capture lists here