Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用与枚举关联的对象?_Ios_Swift_Associated Object - Fatal编程技术网

Ios 如何使用与枚举关联的对象?

Ios 如何使用与枚举关联的对象?,ios,swift,associated-object,Ios,Swift,Associated Object,我有一个ViewController,我使用关联对象向其添加了两个新属性:枚举和字符串(字符串版本取自) 下面是我的示例代码: extension UIViewController { private struct AssociatedKeys { static var handle = "handle" } enum CustomStringEnum: String { case One = "One" case Tw

我有一个ViewController,我使用关联对象向其添加了两个新属性:枚举和字符串(字符串版本取自)

下面是我的示例代码:

extension UIViewController {

    private struct AssociatedKeys {
        static var handle = "handle"
    }

    enum CustomStringEnum: String {
        case One = "One"
        case Two = "Two"
        case Three = "Three"
    }

    var customEnum: CustomStringEnum {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.handle) as? CustomStringEnum ?? .One
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.handle, newValue.rawValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    var descriptiveName: String {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.handle) as! String
        }

        set {
            objc_setAssociatedObject(
                self,
                &AssociatedKeys.handle,
                newValue as NSString?,
                .OBJC_ASSOCIATION_RETAIN_NONATOMIC
            )
        }
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let vc = UIViewController()
        vc.customEnum = .Three
        vc.descriptiveName = "Three"

        print(vc.customEnum.rawValue) // -> This prints "One"
        print(vc.descriptiveName)     // -> This prints "Three"
    }
}
字符串版本工作正常,但枚举版本工作不正常。我不确定问题出在哪里


这是
objc\u getAssociated对象
objc\u setAssociated对象
的问题。get版本似乎始终为零,因此返回默认值1。

您在
get
函数中硬编码了
。One

根据您的评论,您的复杂化与相关值没有任何意义,因此您应该简化:

enum Numbers: String {
    case One = "One"
    case Two = "Two"
    case Three = "Three"

    // default
    init() { self = .One }

    static let germanNumbers = [One: "Eins", Two: "Zwei", Three: "Drei"]
    var germanString: String { return Numbers.germanNumbers[self]! }
}

let num = Numbers.Three

print(num)              // "Three"
print(num.rawValue)     // "Three"

let defaultNum = Numbers()
print(defaultNum)       // "One"

print(num.germanString)        // "Drei"
print(defaultNum.germanString) // "Eins"

将您的代码更改为此

extension UIViewController {

private struct AssociatedKeys {
    static var handle = "handle"
    static var enumContext = "enumContext"
}

enum CustomStringEnum: String {
    case One = "One"
    case Two = "Two"
    case Three = "Three"
}

var customEnum: CustomStringEnum {
    get {
        let rawvalue = objc_getAssociatedObject(self, &AssociatedKeys.enumContext)
        if rawvalue == nil{
            return .One
        }else{
            return CustomStringEnum(rawValue: rawvalue as! String)!;
        }
    }
    set {
        objc_setAssociatedObject(self, &AssociatedKeys.enumContext, newValue.rawValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }
}

var descriptiveName: String {
    get {
        return objc_getAssociatedObject(self, &AssociatedKeys.handle) as! String
    }

    set {
        objc_setAssociatedObject(
            self,
            &AssociatedKeys.handle,
            newValue as NSString?,
            .OBJC_ASSOCIATION_RETAIN_NONATOMIC
        )
    }
}
}

然后它将工作

我这样做是为了返回。如果没有值,则返回一个。请澄清使用关联值要达到的目的。我要设置枚举值。在我的示例中,我将其设置为.3,但它打印.1(默认值)。因此,当尝试读取它发现为nil的值时,这就是它打印默认值的原因。我不知道为什么这些值设置不正确。