Last active
August 18, 2021 20:37
-
-
Save ZevEisenberg/1a8e489751b4fdd4c46ce99e2de6e3b2 to your computer and use it in GitHub Desktop.
Adds typed mutable copying to NSMutableCopying conformers
This file contains 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
import Foundation | |
protocol TypedMutableCopying { | |
associatedtype ConstantVersion | |
associatedtype MutableVersion | |
var typedCopy: ConstantVersion { get } | |
var typedMutableCopy: MutableVersion { get } | |
} | |
extension TypedMutableCopying where Self: NSCopying, ConstantVersion: NSCopying { | |
var typedCopy: ConstantVersion { | |
copy() as! ConstantVersion | |
} | |
} | |
extension TypedMutableCopying where Self: NSMutableCopying, MutableVersion: NSMutableCopying { | |
var typedMutableCopy: MutableVersion { | |
mutableCopy() as! MutableVersion | |
} | |
} | |
protocol TypedMutableCopying { | |
associatedtype ConstantVersion = Self | |
associatedtype MutableVersion | |
var typedCopy: ConstantVersion { get } | |
var typedMutableCopy: MutableVersion { get } | |
} | |
extension TypedMutableCopying where Self: NSCopying, ConstantVersion: NSCopying { | |
var typedCopy: ConstantVersion { | |
copy() as! ConstantVersion | |
} | |
} | |
extension TypedMutableCopying where Self: NSMutableCopying, MutableVersion: NSMutableCopying { | |
var typedMutableCopy: MutableVersion { | |
mutableCopy() as! MutableVersion | |
} | |
} | |
extension NSArray: TypedMutableCopying { | |
typealias MutableVersion = NSMutableArray | |
} | |
extension NSAttributedString: TypedMutableCopying { | |
typealias MutableVersion = NSMutableAttributedString | |
} | |
extension NSSet: TypedMutableCopying { | |
typealias MutableVersion = NSMutableSet | |
} | |
extension NSData: TypedMutableCopying { | |
typealias MutableVersion = NSMutableData | |
} | |
extension NSString: TypedMutableCopying { | |
typealias MutableVersion = NSMutableString | |
} | |
extension NSDictionary: TypedMutableCopying { | |
typealias MutableVersion = NSMutableDictionary | |
} | |
extension NSIndexSet: TypedMutableCopying { | |
typealias MutableVersion = NSMutableIndexSet | |
} | |
extension NSOrderedSet: TypedMutableCopying { | |
typealias MutableVersion = NSMutableOrderedSet | |
} | |
extension NSURLRequest: TypedMutableCopying { | |
typealias MutableVersion = NSMutableURLRequest | |
} | |
extension NSCharacterSet: TypedMutableCopying { | |
typealias MutableVersion = NSMutableCharacterSet | |
} | |
extension NSParagraphStyle: TypedMutableCopying { | |
typealias MutableVersion = NSMutableParagraphStyle | |
} | |
#if canImport(UserNotifications) | |
import UserNotifications | |
extension UNNotificationContent: TypedMutableCopying { | |
typealias MutableVersion = UNMutableNotificationContent | |
} | |
#endif |
#if canImport(UIKit)
should this be UserNotifications?
Yep. Thanks. I'm sure I've made that mistake before and I'll make it again 😆
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: