This is the code causing the warning:
private override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
let distance = CGRectGetMidX(attributes!.frame) - self.midX;
var transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, -distance, 0, -self.width);
attributes!.transform3D = CATransform3DIdentity;
return attributes
}
The console also prints:
This is likely occurring because the flow layout "xyz" is modifying attributes returned by UICollectionViewFlowLayout without copying them.
How do I fix this warning?
It's not the answer to original question, but can help for layoutAttributesForElements(in rect: CGRect) (Swift 3.0):
Adding to @Georgi answer
<NSCopying>
must be conformed and add copy message call tolayoutAttributesForItemAtIndexPath
I have subclassed
UICollectionViewFlowLayout
. InsidelayoutAttributesForElementsInRect()
I did this change:change from
change to
And sure enough, that's just what you are doing:
I expect that if you simply say:
or similar, the problem will go away.
In addition to the great answer above.
I know the example code is written in swift, but I thought that it can be helpful to have the Objective-C version.
For Objective-C, this won't work, because the copy function does only a shallow copy. You will have to do this:
I have added a temp variable for readability.
Updated response for Swift 3!
for func layoutAttributesForElements
for func layoutAttributesForItem
If you override both function, you have to call copy on both function!
Good coding!