我正在尝试为Lerp-able(线性插值)类型创建一个协议.我声明它与Equatable定义方式类似:
protocol Lerpable {
func lerp(from: Self, to: Self, alpha: Double) -> Self
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我试图实现Lerpable的Double:
func lerp(from: Double, to: Double, alpha: Double) -> Double {
return from + alpha * (to - from)
}
extension Double: Lerpable {}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:Type 'Double' does not conform to protocol 'Lerpable'.
我认为这将非常简单,但也许我只是不明白Equatable的工作原理.或者它是Swift中的特例?有什么想法吗?
更新:正确答案如下,这是代码的最终版本,供其他人参考:
protocol Lerpable {
func lerp(to: Self, alpha: Double) -> Self
}
extension Double: Lerpable {
func lerp(to: Double, alpha: Double) -> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一些协议组合用于依赖注入,但我遇到了一个问题,我怀疑可能没有我想要的解决方案,但我无法看到其逻辑原因:
protocol DM1 {
func sayHi() -> Void
}
protocol DM2 {
func sayHello() -> Void
}
protocol VM1 {
typealias T: DM1
var dm: T { get }
}
protocol VM2 {
typealias T: DM2
var dm: T { get }
}
protocol RT: VM1, VM2 {
}
class James {
let rt: RT
init(rt: RT) {
self.rt = rt
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码导致错误" 协议'RT'只能用作通用约束,因为它对rt实例变量和实例化参数具有自我或相关类型要求 " James.我真的不明白为什么我不能在James课堂上使用这个通用要求.
我最初做过类似的事情:
protocol DM1 {
func sayHi() -> …Run Code Online (Sandbox Code Playgroud) 很长一段时间以来,我一直在努力使用Swift协议和关联类型.我再次使用基本功能来真正理解出现了什么问题,我在Rob Napier的Swift Protocols中使用了相关类型要求的TypeErasure 文章,但我仍然没有运气.
找到下面的代码
// An Animal can eat
protocol Animal {
associatedtype Food
func feed(food: Food) -> Void
}
struct AnyAnimal<Food>: Animal {
private let _feed: (Food) -> Void
init<Base: Animal where Food == Base.Food>(_ base: Base) {
_feed = base.feed
}
func feed(food: Food) { _feed(food) }
}
// Kinds of Food
struct Grass {}
struct Cow: Animal {
func feed(food: Grass) { print("moo") }
}
struct Goat: Animal {
func feed(food: Grass) { print("bah") } …Run Code Online (Sandbox Code Playgroud) 让我们假设:
enum MyEnum: String { case value }
let possibleEnum: Any = MyEnum.value
if let str = stringFromPossibleEnum(possibleEnum: possibleEnum)
Run Code Online (Sandbox Code Playgroud)
在stringFromPossibleEnum不知道枚举类型名称的情况下实现的最佳选择是什么?
func stringFromPossibleEnum(possibleEnum: Any) -> String? {
// how should this be implemented without knowing enum type name?
}
Run Code Online (Sandbox Code Playgroud)
UPD:好的,越来越好,有了这个我可以判断是否possibleEnum是一个枚举:
if Mirror(reflecting: possibleEnum).displayStyle == .enum { print("yes!") }
Run Code Online (Sandbox Code Playgroud)
但是如何判断这是否是String基于枚举的?
UPD: 这条推文表明你可以rawValue从 Enum 中获得Any 。然后你可以检查它是否rawValue是String. 但如何rawValue从Mirror?
我正在尝试这样做:
protocol Fly {
}
class Bird: Fly {
}
func fetch<T: Fly>(model: T) {
print("Done")
}
let bird: Fly = Bird()
fetch(model: bird)
Run Code Online (Sandbox Code Playgroud)
但是我收到这个错误:
无法使用类型为“(模型:Fly)”的参数列表调用“获取”
我设置let bird: Fly = Bird()为 type Fly,它不应该工作,因为该函数fetch采用任何符合该协议的对象吗?
有什么想法吗?
我建立了Class在Swift那类及其协议。我正在使用Obj-C启动项目,但我得到以下错误,而编译我的项目。
找不到“SpeechRecognizerDelegate”的协议声明;你的意思是“SFSpeechRecognizerDelegate”?
谁能指导我如何在我的 Obj-C 类中使用 Swift 类协议。
这是我的 Swift 代码:
protocol SpeechRecognizerDelegate : class {
func speechRecognitionFinished(_ transcription:String)
func speechRecognitionError(_ error:Error)
}
class SpeechRecognizer: NSObject, SFSpeechRecognizerDelegate {
open weak var delegate: SpeechRecognizerDelegate?
}
Run Code Online (Sandbox Code Playgroud)
Obj-C 中的协议使用:
#import "ARBot-Swift.h"
@interface ChatScreenViewController : JSQMessagesViewController <SpeechRecognizerDelegate>
Run Code Online (Sandbox Code Playgroud)
如果需要更多信息,请告诉我。
提前致谢。
我正在尝试编写符合集合协议的协议,它有一个associatedType - Object和一个属性对象.
protocol DDCDataSource: Collection
{
associatedtype Object
var object: Object {get set}
}
Run Code Online (Sandbox Code Playgroud)
我想为Object也符合Collection协议的情况添加一些默认功能,即直接返回Object对这些必需Collection属性和函数的实现.除了Collection对下标的要求外,它似乎都有效.
无法使用类型为"Self.Object.Index"的索引下标"Self.Object"类型的值
extension DDCDataSource where Object: Collection
{
typealias Index = Object.Index
var startIndex: Object.Index {
get {
return object.startIndex
}
}
var endIndex: Object.Index {
get {
return object.endIndex
}
}
subscript(position: Object.Index) -> Element
{
return object[position]
}
func index(after i: Object.Index) -> Object.Index {
return object.index(after: i)
}
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个函数类:Bar,Bar使用属于它的委托做特定的事情,这个委托符合协议FooDelegate,类似的东西:
protocol FooDelegate{
associatedtype Item
func invoke(_ item:Item)
}
class SomeFoo:FooDelegate{
typealias Item = Int
func invoke(_ item: Int) {
//do something...
}
}
class Bar{
//In Bar instance runtime ,it will call delegate to do something...
var delegate:FooDelegate!
}
Run Code Online (Sandbox Code Playgroud)
但是在课堂上Bar:var delegate:FooDelegate!我收到了一个错误:
协议'FooDelegate'只能用作通用约束,因为它具有Self或相关类型要求
我怎么能解决这个问题?
使用XCode 10.1/Swift 4.2.
我正在尝试将符合Swift协议的对象分配给Objective-C指针.以下代码是一个最小的示例,编译和按预期工作,但它给了我以下警告:
如果分配给局部变量: Incompatible pointer types initializing 'NSObject<Animal> *__strong' with an expression of type 'id<Animal> _Nullable'
如果分配给存储的属性:
Incompatible pointer types assigning to 'NSObject<Animal> *' from 'id<Animal> _Nullable'
关于如何解决这个警告而不仅仅是沉默它的任何想法?
SWIFT代码:
@objc protocol Animal {
var name: String { get }
}
@objc class Pig: NSObject, Animal {
var name: String = "pig"
}
@objc class Cow: NSObject, Animal {
var name: String = "cow"
}
@objc class Farm: NSObject {
static func getAnimal(name: String) -> Animal? {
// return …Run Code Online (Sandbox Code Playgroud) objective-c compiler-warnings pointer-conversion swift swift-protocols
我了解keyPaths的基本概念,但不了解其用例。如果您已经知道实例的类型,则可以轻松访问其属性。如果您不这样做,那么协议已经支持只读,读写属性。有人可以向我解释我所缺少的吗?除了keyPath或keypath比协议更好时,我们无法使用协议做任何事情?