Last active
August 29, 2015 14:08
-
-
Save tjw/151bdebb6a8eb029a3ba to your computer and use it in GitHub Desktop.
Can't access members of protocol types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol MeasurementUnit { | |
// If we have one of this unit, how many millimeters is it? | |
class var asMillimeters: Double { get } | |
} | |
public class Inch : MeasurementUnit { | |
public class var asMillimeters: Double { | |
get { | |
return 25.4 | |
} | |
} | |
} | |
let unit: MeasurementUnit.Type = Inch.self | |
println("unit to mm = \(unit.asMillimeters)") | |
/* Yields: | |
protocol-type-member.swift:15:25: error: accessing members of protocol type value 'MeasurementUnit.Type' is unimplemented | |
*/ |
+1
"@objc protocol meas {
class var asm: Double{get}
}
class inc : meas {
class var asm: Double {
get {
return 25
}
}
}
let a = inc.self as meas.Type
let b:AnyClass = a as Any as AnyClass
b.asm"
Can you try this .I have made protocol to objc type, and did some downcasting. Please let me know if this worked
It seems you can access the static method as long as you declare the protocol using @objc
and then cast the variable to AnyClass
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd also like to know how this works...