iosswift3copywithzone

copyWithZone in Swift3


I am trying to convert one function to newer Swift3 version but getting error. The action is happening in UICollectionViewLayoutAttributes class which is called CircularCollectionViewLayoutAttributes and class starts like:

class CircularCollectionViewLayoutAttributes: UICollectionViewLayoutAttributes { ...

function code:

override func copy(with zone: NSZone? = nil) -> Any {
let copiedAttributes: CircularCollectionViewLayoutAttributes = super.copy(zone) as! CircularCollectionViewLayoutAttributes
copiedAttributes.anchorPoint = self.anchorPoint
copiedAttributes.angle = self.angle
return copiedAttributes

}

I almost managed to transform it, but still one error on line:

let copiedAttributes: CircularCollectionViewLayoutAttributes = super.copy(zone) as! CircularCollectionViewLayoutAttributes

says that: "Argument passed to call that takes no arguments" and underlines "zone".

Your help is very appreciated.


Solution

  • One of the changes that occurred when bridging APIs from Objective-C to Swift is that the prepositions in a lot of the method names have been turned into argument labels. The noun after the preposition or the object of the verb is often omitted and used as the internal argument name as well. copyWithZone is one of them.

    Here is the declaration in Swift:

    func copy(with zone: NSZone? = nil) -> Any
    

    Other examples include prepare(for:) from prepareForSegue, present(_:animated:completion:) from presentViewConroller etc.