变量“self”已写入,但使用 XLPagerTabStrip 时从未读取警告消息

JoB*_*oy7 5 ios swift xlpagertabstrip

我正在使用 cocoapod XLPagerTabStrip在我的 iOS 应用程序中实现 PagerTabStrip。我在构建或运行应用程序时收到以下警告消息

变量“self”已写入,但从未读取

此警告来自以下关闭代码,该代码用于更改滑动时的项目文本颜色。

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
}
Run Code Online (Sandbox Code Playgroud)
  • 斯威夫特版本:5
  • Xcode 版本:12.1
  • XLPagerTabStrip:9.0.0

有人可以帮我摆脱这个警告消息吗?

Wit*_*ski 10

您可以安全地从闭包捕获列表中删除[weak self],因为您没有在闭包本身中引用它,因此编译器会警告您不需要捕获它。

闭包应该是这样的

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
}
Run Code Online (Sandbox Code Playgroud)

在此处阅读有关闭包和捕获列表的更多信息