5

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
}
  • Swift version: 5
  • Xcode version: 12.1
  • XLPagerTabStrip: 9.0.0

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

Paul Beusterien
  • 27,542
  • 6
  • 83
  • 139
JoBoy7
  • 53
  • 1
  • 4

1 Answers1

10

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

Witek Bobrowski
  • 3,749
  • 1
  • 20
  • 34