更新到
xcode7-beta我经历了一种新的警告.这是我的代码
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { var attributes: [UICollectionViewLayoutAttributes]? = super.layoutAttributesForElementsInRect(rect) if let layoutInfo = self.layoutInfo { attributes?.append(layoutInfo) } return attributes }
警告信息是
变量“属性”从未被突变,考虑改变为“让”常量
为什么xcode说变量“属性”从未被突变?
问题更新
当我将代码更改为此时,警告消失了
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { var attributes: [UICollectionViewLayoutAttributes]? = super.layoutAttributesForElementsInRect(rect) if let layoutInfo = self.layoutInfo { attributes!.append(layoutInfo) } return attributes }
所以强制解开可以把它拿走.但这可能不是一件好事吗?
他们在WWDC的视频和发行说明中谈到了这一点.
一直以来,如果你可以使用let而不是var,你可以获得更多更好的性能(更快的速度,更小的空间).这告诉编译器这个东西是一个常量,而不是一个变量,这个事实允许编译器优化各种各样的事情.
但编译器无法做到这一点,除非你尽可能地使用let.它不会改变一个变化为一个让你为你.
因此,在Swift 2中,编译器在构建时进行更智能的分析,并且在使用可以使用let的var时警告您.最终这个功能可以正常工作,那么你应该把编译器的建议