Matthieu Riegler
Matthieu Riegler

Reputation: 55297

Swift 2 : OBJC_ASSOCIATION_RETAIN_NONATOMIC use of unresolved identifier

I just updated my project to Swift 2.

I one of my swift class I use ObjC association.

I have the following :

objc_AssociationPolicy( OBJC_ASSOCIATION_RETAIN_NONATOMIC)

Since the update, the compiler returns Use of unresolved identifier 'OBJC_ASSOCIATION_RETAIN_NONATOMIC'.

Any idea why ?


Edit: For those who have the same problem, a temporary fix would be to replace the constant with its value ie. 1 : objc_AssociationPolicy( rawValue: 1 )

Upvotes: 23

Views: 5743

Answers (2)

dhara ahuja
dhara ahuja

Reputation: 11

Try this:

import ObjectiveC.runtime

func setOverlay(view: UIView)
{
    objc_setAssociatedObject(self, &AssociatedKeys.overlayKey, view, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}

Upvotes: 0

James Alvarez
James Alvarez

Reputation: 7219

If you look at the obj c runtime swift header, it appears this construct has become an enum:

/**
 * Policies related to associative references.
 * These are options to objc_setAssociatedObject()
 */
enum objc_AssociationPolicy : UInt {

    case OBJC_ASSOCIATION_ASSIGN
    case OBJC_ASSOCIATION_RETAIN_NONATOMIC

    case OBJC_ASSOCIATION_COPY_NONATOMIC

    case OBJC_ASSOCIATION_RETAIN

    case OBJC_ASSOCIATION_COPY
}

So you can replace with: objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC.

If you need the token as a UInt, you can always use .rawValue.

(In the previous version objc_AssociationPolicy was just a typealias for UInt - with the effect of casting 'OBJC_ASSOCIATION_RETAIN_NONATOMIC', an Int)

Upvotes: 44

Related Questions