Skip to content

Instantly share code, notes, and snippets.

@correia
Created November 4, 2017 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save correia/13b5bf11ee6650fa39f565ffa919edd3 to your computer and use it in GitHub Desktop.
Save correia/13b5bf11ee6650fa39f565ffa919edd3 to your computer and use it in GitHub Desktop.
Strongly typed NSCopying/NSMutableCopying for Swift
// Strongly typed NSCopying/NSMutableCopying for Swift
import Cocoa
protocol Clonable: NSCopying {
func clone() -> Self
}
extension Clonable {
func clone() -> Self {
return copy(with: nil) as! Self
}
}
protocol MutableCloning: NSMutableCopying {
associatedtype MutableSelf
func mutableClone() -> MutableSelf
}
extension MutableCloning {
func mutableClone() -> MutableSelf {
return mutableCopy(with: nil) as! MutableSelf
}
}
extension NSParagraphStyle: MutableCloning {
typealias MutableSelf = NSMutableParagraphStyle
}
let paragraphStyle = NSParagraphStyle.default.mutableClone()
paragraphStyle.lineBreakMode = .byTruncatingTail
print(paragraphStyle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment