簡體   English   中英

訪問協議類型的成員在Swift中不起作用

[英]Accessing members of protocol type doesn't work in Swift

以下是問題,如果類型是protocol.type,我無法調用類方法。 是否有可以實現這一目標的替代模式。

真實情況是我有arr:[SomeProtocol.Type],我想執行let字符串:[String] = arr.map {$ 0.str}

protocol SomeProtocol: class {
   static var str: String { get }

   static func value() -> Int
}

class SomeClass: SomeProtocol {
   static var str: String = "SomeClass"

   static func value() -> Int {
      return 1
   }
}

let protocolType: SomeProtocol.Type = SomeClass.self
println(protocolType.str) // compile error
println(protocolType.value()) // compile error

在此輸入圖像描述

正如您所看到的,swift的這一部分未實現。 (Xcode 6.3 beta 4)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM