如何将 AnyObject 的参数...传递给需要 AnyObject 的参数
How to pass parameter of AnyObject... to function which parameter need AnyObject
我正在使用
的功能
NSAttributedString(attributes: [String : AnyObject]?, format: String, arguments: AnyObject...)
而且我想自定义一个通用的函数来调用上面这个函数。所以我需要发送参数
arguments: AnyObject...
但是这个参数在我的自定义函数中会变成[AnyObject]。如何解决?
更新:
当我使用下面的代码时:
typealias Function = ([String : AnyObject]?, String, [AnyObject]) -> NSAttributedString
let myAttributedString = unsafeBitCast(NSAttributedString(attributes:format:_:) , Function.self)
会报错:
Use of unresolved identifier 'NSAttributedString(attributes:format:_:)'
更新:(临时解决方案)
我得到了一个临时解决方案,它很丑但对我来说足够用了。
// NOTE: here arguments may be String or NSAttributedString
private func getAttributedString(withFormat format: String, _ arguments: AnyObject..., withUnderLine: Bool = false) -> NSAttributedString {
var attributes = [NSFontAttributeName: #someFont,
NSForegroundColorAttributeName: #someColor]
if withUnderLine {
attributes[NSStrikethroughStyleAttributeName] = NSUnderlineStyle.StyleSingle.rawValue
}
switch arguments.count {
case 0:
return NSAttributedString(attributes: attributes, format: format)
case 1:
return NSAttributedString(attributes: attributes, format: format, arguments[0])
case 2:
return NSAttributedString(attributes: attributes, format: format, arguments[0], arguments[1])
case 3:
return NSAttributedString(attributes: attributes, format: format, arguments[0], arguments[1], arguments[2])
case 4:
return NSAttributedString(attributes: attributes, format: format, arguments[0], arguments[1], arguments[2], arguments[3])
default:
assert(arguments.count <= 4)
return NSAttributedString(attributes: attributes, format: format, arguments[0], arguments[1], arguments[2], arguments[3])
}
}
如果要引用初始化程序,则应使用 NSAttributedString.init(attributes:format:_:)
完整调用变为unsafeBitCast(NSAttributedString.init(attributes:format:_:), to: Function.self)
我刚刚在 iPad 的 Swift Playgrounds 中尝试了类似的东西:
我正在使用
的功能NSAttributedString(attributes: [String : AnyObject]?, format: String, arguments: AnyObject...)
而且我想自定义一个通用的函数来调用上面这个函数。所以我需要发送参数
arguments: AnyObject...
但是这个参数在我的自定义函数中会变成[AnyObject]。如何解决?
更新:
当我使用下面的代码时:
typealias Function = ([String : AnyObject]?, String, [AnyObject]) -> NSAttributedString
let myAttributedString = unsafeBitCast(NSAttributedString(attributes:format:_:) , Function.self)
会报错:
Use of unresolved identifier 'NSAttributedString(attributes:format:_:)'
更新:(临时解决方案)
我得到了一个临时解决方案,它很丑但对我来说足够用了。
// NOTE: here arguments may be String or NSAttributedString
private func getAttributedString(withFormat format: String, _ arguments: AnyObject..., withUnderLine: Bool = false) -> NSAttributedString {
var attributes = [NSFontAttributeName: #someFont,
NSForegroundColorAttributeName: #someColor]
if withUnderLine {
attributes[NSStrikethroughStyleAttributeName] = NSUnderlineStyle.StyleSingle.rawValue
}
switch arguments.count {
case 0:
return NSAttributedString(attributes: attributes, format: format)
case 1:
return NSAttributedString(attributes: attributes, format: format, arguments[0])
case 2:
return NSAttributedString(attributes: attributes, format: format, arguments[0], arguments[1])
case 3:
return NSAttributedString(attributes: attributes, format: format, arguments[0], arguments[1], arguments[2])
case 4:
return NSAttributedString(attributes: attributes, format: format, arguments[0], arguments[1], arguments[2], arguments[3])
default:
assert(arguments.count <= 4)
return NSAttributedString(attributes: attributes, format: format, arguments[0], arguments[1], arguments[2], arguments[3])
}
}
如果要引用初始化程序,则应使用 NSAttributedString.init(attributes:format:_:)
完整调用变为unsafeBitCast(NSAttributedString.init(attributes:format:_:), to: Function.self)
我刚刚在 iPad 的 Swift Playgrounds 中尝试了类似的东西: