Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

swift - How to pass parameter of AnyObject... to function which parameter need AnyObject

I'am using function of

NSAttributedString(attributes: [String : AnyObject]?, format: String, arguments: AnyObject...)

And I want to customize an general function to call this function above. So I need to send parameter

arguments: AnyObject...

But this parameter will become [AnyObject] in my customize function. How to fix it?

Update:

When I use code bellow:

typealias Function = ([String : AnyObject]?, String, [AnyObject]) -> NSAttributedString
let myAttributedString = unsafeBitCast(NSAttributedString(attributes:format:_:) , Function.self)

It will give error:

Use of unresolved identifier 'NSAttributedString(attributes:format:_:)'

Update:(Temp Solution)

I get an temp solution which is ugly but works just enough for me.

// 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])
         }  
     }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are referencing the initializer, you should use NSAttributedString.init(attributes:format:_:)

The full call becomes unsafeBitCast(NSAttributedString.init(attributes:format:_:), to: Function.self)

I've just tried something similar in Swift Playgrounds on iPad:

Playgrounds screenshot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...