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 | |
*/ |
This is not fixed yet as of April 2015. So annoying. I need to implement the following:
protocol CloudStorable {
class var classUrlString: String { get }
}
class DownloadTask<T: CloudStorable> {
func execute() {
var destinationUrl = "http://localhost:1234/" + T.classUrlString
// this does not work, and I cannot simply make the property non-static
// because I do not have an instance of CloudStorable before downloading.
}
}
I'd also like to know how this works...
+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
It would seem swift does not yet support protocol type properties or methods yet. In the meantime the next best thing would be to make it an instance variable, I guess?
It's not great but it will work for me for now.