Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.80 KB | None | 0 0
  1.     func testAssociation() {
  2.         class Holder {}
  3.         class Association {}
  4.        
  5.         var associationHolder: Holder? = Holder()
  6.         var strongReferenceAssociation: Association? = Association()
  7.         weak var weakReferenceAssociation: Association? = strongReferenceAssociation
  8.         var associationKey = "key"
  9.        
  10.         objc_setAssociatedObject(associationHolder!, &associationKey, strongReferenceAssociation!, .OBJC_ASSOCIATION_RETAIN)
  11.  
  12.         weak var retrievedAssociation = objc_getAssociatedObject(associationHolder, &associationKey) as? Association
  13.         XCTAssertTrue(strongReferenceAssociation! === retrievedAssociation!)
  14.        
  15.         strongReferenceAssociation = nil
  16.         associationHolder = nil
  17.  
  18.         XCTAssertTrue(weakReferenceAssociation === .none)
  19.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement