Declare “NSMapTable StrongObject” in Swift 3

2019-02-26 11:20发布

How can I declare an NSMapTable in Swift 3? Why doesn't this example from Apple work?

let activeLines = NSMapTable.strongToStrongObjectsMapTable()

Xcode suggested change to:

let activeLines = NSMapTable.strongToStrongObjects()

but it still does not work.

I need convert this example to Swift 3

https://developer.apple.com/library/content/samplecode/TouchCanvas/Listings/TouchCanvas_CanvasView_swift.html

1条回答
▲ chillily
2楼-- · 2019-02-26 11:53

NSMapTable.strongToStrongObjects() will fail with the error:

error: generic parameter 'KeyType' could not be inferred
NSMapTable.strongToStrongObjects()
           ^

The class declaration of NSMapTable is:

open class NSMapTable<KeyType : AnyObject, ObjectType : AnyObject> : NSObject, NSCopying, NSCoding, NSFastEnumeration

You need to specify the key and object types of your map table in its declaration:

NSMapTable<NSObject, NSObject>.strongToStrongObjects()
查看更多
登录 后发表回答