Swift / iOS:如何在AnyObject / Any或Pointers的函数中使用inout参数 [英] Swift/iOS: How to use inout parameters in functions with AnyObject/Any or Pointers

查看:138
本文介绍了Swift / iOS:如何在AnyObject / Any或Pointers的函数中使用inout参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个带有变量指针和描述符/键的函数,并为变量设置一个新值。理想情况下,指针应该是对象或基元,但我也可以使用单独的函数(或其他参数)。
在我的代码中,我也使用密钥从数据库中检索新值,但在下面的示例中,我使用虚拟值对其进行了简化,以便可以在操场中轻松使用:

I'm trying to write a function that takes a variable pointer and a descriptor/key and sets a new value for the variable. Ideally the pointer should be either of an object or a primitive, but I could also live with separate functions (or an additional parameter). In my code I retrieve the new value from a database also using the key, but in the following example I simplified it with dummy values so that it can be used easily in a playground:

import UIKit

func setValue(inout object: AnyObject, key: String) {
    switch key {
    case "String":
        object = "A String"
    case "UIColor":
        object = UIColor.whiteColor()
    case "Bool":
        object = true
    default:
        println("Unhandled key: \(key)")
    }
}

var string: String = "Default String"
var color: UIColor = UIColor.blackColor()
var bool: Bool = false

setValue(&string, "String")
setValue(&color, "UIColor")
setValue(&bool, "Bool")

我收到以下错误:


无法使用类型'的参数列表调用'setValue'(inout
String,key字符串)'

"Cannot invoke 'setValue' with an argument list of type '(inout String, key String)'"

我知道我在这里混合了对象和基元。我也尝试将它分解并将它们分成两个函数,但即使失败了:

I understand that I'm mixing Objects and Primitives here. I also tried to break it down and separate these into two functions, but even that fails:

func setValue(inout object: AnyObject, key: String) {
    switch key {
    case "UIColor":
        object = UIColor.whiteColor()
    default:
        println("Unhandled key: \(key)")
    }
}

var color: UIColor = UIColor.blackColor()
setValue(&color, "UIColor")

这也会出现同样的错误:

This also gives the same error:


不能使用类型'的参数列表调用'setValue'(inout
UIColor,key String)'

"Cannot invoke 'setValue' with an argument list of type '(inout UIColor, key String)'"

如果我将'AnyObject'更改为'UIColor'它可以工作,但函数的重点是,它需要任何变量类型或至少任何对象类型(我使用Any编写第二个函数)然后或者添加另一个参数)

If I change 'AnyObject' to 'UIColor' it works, but the point of the function is, that it takes any variable type or at least any object type (I'd write a second function using "Any" for primitives then or add another parameter)

在Objective-C中我使用指针,转移方法到Swift也不起作用,结果相同:

In Objective-C I was using pointers, transferring the approach to Swift also doesn't work, same result:

func setValue(object: UnsafeMutablePointer<AnyObject>, key: String) {
    switch key {
    case "String":
        object.memory = "A String"
    case "UIColor":
        object.memory = UIColor.whiteColor()
    case "Bool":
        object.memory = true
    default:
        println("Unhandled key: \(key)")
    }
}

有人知道我在这里缺少什么吗?非常感谢任何帮助!

Does anybody have an idea what I'm missing here? Any help is much appreciated!

谢谢!

推荐答案

更好的你可以创建如下通用方法:

Better you can create a generic method like below:

func setValue<T>(inout object:T, key: String) {
        switch key {
        case "String":
            object = ("A String" as? T)!
        case "UIColor":
            object = (UIColor.whiteColor() as? T)!
        case "Bool":
            object = (true as? T)!
        default:
            println("Unhandled key: \(key)")
        }
    }

并且呼叫将是这样的:

setValue(&string, key: "String")
setValue(&color, key: "UIColor")
setValue(&bool, key: "Bool")

希望有所帮助!

这篇关于Swift / iOS:如何在AnyObject / Any或Pointers的函数中使用inout参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆