Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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
Swift 如何传递任何对象的参数。。。要运行哪个参数需要AnyObject_Swift_Arguments_Nsattributedstring - Fatal编程技术网

Swift 如何传递任何对象的参数。。。要运行哪个参数需要AnyObject

Swift 如何传递任何对象的参数。。。要运行哪个参数需要AnyObject,swift,arguments,nsattributedstring,Swift,Arguments,Nsattributedstring,我正在使用 NSAttributedString(attributes: [String : AnyObject]?, format: String, arguments: AnyObject...) 我想定制一个通用函数来调用上面的函数。所以我需要发送参数 参数:任何对象 但是这个参数将成为我的自定义函数中的[AnyObject]。如何修复它 更新: 当我使用下面的代码时: typealias Function = ([String : AnyObject]?, String, [AnyOb

我正在使用

NSAttributedString(attributes: [String : AnyObject]?, format: String, arguments: AnyObject...)
我想定制一个通用函数来调用上面的函数。所以我需要发送参数

参数:任何对象

但是这个参数将成为我的自定义函数中的[AnyObject]。如何修复它

更新

当我使用下面的代码时:

typealias Function = ([String : AnyObject]?, String, [AnyObject]) -> NSAttributedString
let myAttributedString = unsafeBitCast(NSAttributedString(attributes:format:_:) , Function.self)
它将给出错误:

使用未解析标识符“NSAttributedString(属性:格式:\)”

更新:(临时解决方案)

我得到了一个临时解决方案,虽然很难看,但对我来说已经足够了

// 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])
         }  
     }
//注意:这里的参数可以是String或NSAttributedString
private func getAttributedString(withFormat格式:String,u参数:AnyObject…,withUnderLine:Bool=false)->NSAttributedString{
var attributes=[NSFontAttributeName:#someFont,
NSForegroundColorAttributeName:#someColor]
如果使用下划线{
属性[NSStrikethrowStyleAttributeName]=NSUnderlineStyle.StyleSingle.rawValue
}
切换参数.count{
案例0:
返回NSAttribute字符串(属性:属性,格式:格式)
案例1:
返回NSAttributedString(属性:属性,格式:格式,参数[0])
案例2:
返回NSAttributedString(属性:属性,格式:格式,参数[0],参数[1])
案例3:
返回NSAttributedString(属性:属性,格式:格式,参数[0],参数[1],参数[2])
案例4:
返回NSAttribute字符串(属性:属性,格式:格式,参数[0],参数[1],参数[2],参数[3])
违约:

assert(arguments.count如果引用初始值设定项,则应使用
nsattributestring.init(属性:格式:)

完整调用变为
unsafeBitCast(NSAttributedString.init(属性:format:\)、to:Function.self)

我刚刚在iPad上的Swift操场上尝试过类似的东西:


我知道如何使用Int,但不知道如何使用anyobject。哦,状态是否最新?如何使用Int?CC Dog的回答:谢谢。你为什么不使用该解决方案或发布尝试时出现的错误?啊哈,太好了!你在哪里发现的?我在do中没有发现任何有关的信息文档,可能我搜索时没有正确的键。谢谢。我模糊地记得有一些示例,如
[1,2,3].map(String.init)
,它表明可以用这种方式引用初始值设定项。