Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2 : OBJC_ASSOCIATION_RETAIN_NONATOMIC use of unresolved identifier

Tags:

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 )

like image 303
Matthieu Riegler Avatar asked Jun 20 '15 23:06

Matthieu Riegler


1 Answers

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)

like image 149
James Alvarez Avatar answered Nov 04 '22 02:11

James Alvarez