Skip to content

Instantly share code, notes, and snippets.

@ZevEisenberg
Last active August 18, 2021 20:37
Show Gist options
  • Save ZevEisenberg/1a8e489751b4fdd4c46ce99e2de6e3b2 to your computer and use it in GitHub Desktop.
Save ZevEisenberg/1a8e489751b4fdd4c46ce99e2de6e3b2 to your computer and use it in GitHub Desktop.
Adds typed mutable copying to NSMutableCopying conformers
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
@ZevEisenberg
Copy link
Author

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