[swift-users] Any reason of "@objc is not supported within extensions of generic classes." ?

Jordan Rose jordan_rose at apple.com
Wed Aug 24 12:15:55 CDT 2016


The way @objc methods are added in an extension is by using Objective-C categories. With a generic class, though, there's no Class object to attach the category metadata to—it's all created dynamically at runtime.

We could potentially support this some day, but it would take some thought, and we'd have to be careful not to drastically slow down the cost of instantiating new concrete types from a heavily-extended generic class.

Jordan


> On Aug 24, 2016, at 7:23, Zhao Xin via swift-users <swift-users at swift.org> wrote:
> 
> I was trying to implement a final class to conform NSCopying by extension. List<T> was a class in Realm object database.
> 
> final class List<T> 
> 
> My implementation:
> 
> extension List:NSCopying {
>     public func copy(with zone: NSZone? = nil) -> Any {
>         ...
>     }
> }
> 
> I got an error called "@objc is not supported within extensions of generic classes.". Any idea why there is a limitation of this? Thanks.
> 
> Zhaoxin
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160824/8bccb7ea/attachment.html>


More information about the swift-users mailing list