Created
November 4, 2017 21:26
-
-
Save correia/13b5bf11ee6650fa39f565ffa919edd3 to your computer and use it in GitHub Desktop.
Strongly typed NSCopying/NSMutableCopying for Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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