【问题标题】:Variable 'self' was written to, but never read warning message when XLPagerTabStrip is used变量“self”已写入,但在使用 XLPagerTabStrip 时从未读取警告消息
【发布时间】:2021-04-05 05:58:25
【问题描述】:

我正在使用 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
}
  • Swift 版本:5
  • Xcode 版本:12.1
  • XLPagerTabStrip:9.0.0

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

【问题讨论】:

  • 尝试删除 [weak self],因为您没有在闭包内访问 self。

标签: ios swift xlpagertabstrip


【解决方案1】:

您可以安全地从闭包捕获列表中删除[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
}

阅读更多关于闭包和捕获列表here

【讨论】:

    猜你喜欢
    • 2016-10-24
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多