2017-02-03 1 views
-1

I'am, welcher Parameter über die Funktion vonWie Parameter von ANYOBJECT passieren ... funktionieren müssen ANYOBJECT

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

Und ich möchte eine allgemeine Funktion anpassen diese Funktion oben zu rufen. Also muss ich Parameter senden

arguments: AnyObject...

Aber dieser Parameter werden [ANYOBJECT] in meiner Funktion anpassen. Wie man es repariert?

aktualisieren:

Wenn ich Code verwenden Gebrüll:

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

Es Fehler geben wird:

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

Update: (Temp Lösung)

I hol dir eine te mp Lösung, die hässlich ist, aber gerade genug für mich funktioniert.

// 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]) 
     } 
    } 
+0

Mögliches Duplikat von [Übergeben eines Arrays an eine Funktion mit variabler Anzahl von Argumenten in Swift] (http: // stac koverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift) – Andreas

+0

Ich weiß, wie es mit Int zu tun, aber weiß nicht wie um dies mit AnyObject zu tun – JerryZhou

+0

Oh, ist der Status von https://bugs.swift.org/browse/SR-128 nicht auf dem neuesten Stand? Wie machst du es mit Int? – Andreas

Antwort

1

Wenn Sie die initializer verweisen, sollten Sie NSAttributedString.init(attributes:format:_:)

Der vollständige Aufruf unsafeBitCast(NSAttributedString.init(attributes:format:_:), to: Function.self)

wird Ich habe gerade etwas Ähnliches versucht, verwenden in Swift Spielplätze auf dem iPad:

Playgrounds screenshot

+0

Aha, Ausgezeichnet! – JerryZhou

+0

Wo hast du das gefunden? Ich habe im Dokument nichts gefunden, vielleicht habe ich ohne korrekte Schlüssel gesucht. Vielen Dank. – JerryZhou

+0

Ich erinnere mich vage daran, dass es einige Beispiele wie '[1, 2, 3] .map (String.init)' gibt, die zeigen, dass der Initialisierer auf diese Weise referenziert werden kann. –

Verwandte Themen