使用Swift 3.0(我可以使用Swift 4.0,如果这对我有帮助......但我认为不会)我想要Erase两级.我要键入什么来擦除具有相关类型的协议,该协议符合协议本身又具有相关类型的协议.所以可以说我想键入擦除嵌套关联类型.
下面的代码是我的代码的极其简化的版本,但它更清楚.所以我真正想要的是这样的:
protocol Motor {
var power: Int { get }
}
protocol Vehicle {
associatedType Engine: Motor
var engine: Engine { get }
}
protocol Transportation {
associatedType Transport: Vehicle
var transport: Transport { get }
}
Run Code Online (Sandbox Code Playgroud)
然后我想输入erase Transportation并能够存储一个阵列,AnyTransportation其中任何Vehicle一个可以拥有任何东西Motor.
所以这是一个包含3个协议的场景,其中2个具有(嵌套)关联类型.
我不知道该怎么做.实际上,我甚至不知道如何解决更简单的场景:
我们可以将上面的原始场景简化为我们有2个协议的版本,其中只有1个协议具有关联类型:
protocol Vehicle {
var speed: Int { get }
}
protocol Transportation {
associatedtype Transport: Vehicle
var transport: …Run Code Online (Sandbox Code Playgroud) 我在下面定义的协议存在问题.我有两个要求:
Peer用作其他类中的类型,同时保持具体类的私有性.为了满足第二点,我需要使协议符合Equatable协议.但是当我这样做时,我不能再将其Peer用作类型,因为它需要被视为通用类型.这意味着我不能再具有私有的具体实现,并且要求1被打破.
想知道是否有其他人遇到过这个问题并以某种方式绕过它.也许我误解了我所得到的错误indexOf......
Group.swift
import Foundation
class Group {
var peers = [Peer]()
init() {
peers.append(PeerFactory.buildPeer("Buddy"))
}
func findPeer(peer: Peer) -> Bool {
if let index = peers.indexOf(peer) {
return true
}
return false
}
}
Run Code Online (Sandbox Code Playgroud)
Peer.swift
import Foundation
protocol Peer {
var name: String { get }
}
class PeerFactory {
static func buildPeer(name: String) -> Peer {
return SimplePeer(name: name)
}
}
private class SimplePeer: Peer {
let …Run Code Online (Sandbox Code Playgroud) 我正在尝试这样做:
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采用任何符合该协议的对象吗?
有什么想法吗?
我有2个协议,Filters并且Parameters都扩展了Encodable
protocol Filters: Encodable {
var page: Int { get }
}
protocol Parameters: Encodable {
var type: String { get }
var filters: Filters { get }
}
Run Code Online (Sandbox Code Playgroud)
因此,我创建了符合这些协议的结构。
struct BankAccountFilters: Filters {
var page: Int
var isWithdrawal: Bool
}
struct BankAccountParamters: Parameters {
let type: String = "Bank"
var filters: Filters
}
let baf = BankAccountFilters(page: 1, isWithdrawal: true)
let bap = BankAccountParamters(filters: baf)
Run Code Online (Sandbox Code Playgroud)
失败是因为…
错误:类型“ BankAccountParamters”不符合协议“可编码”
注意:无法自动合成“可编码”,因为“过滤器”不符合“可编码”
Filters显然确实符合Encodable …
我正在尝试使用Swift 4的Encodable + JSONEncoder将结构序列化为String.该对象可以保存异类值,如String,Array,Date,Int等.
使用的方法可以正常工作,但Date除外.JSONEncoder的dateEncodingStrategy属性没有任何影响.
这是一个片段,它重现了Playground中的行为:
struct EncodableValue:Encodable {
var value: Encodable
init(_ value: Encodable) {
self.value = value
}
func encode(to encoder: Encoder) throws {
try value.encode(to: encoder)
}
}
struct Bar: Encodable, CustomStringConvertible {
let key: String?
let value: EncodableValue?
var description: String {
let encoder = JSONEncoder()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E, d MMM yyyy"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
encoder.dateEncodingStrategy = .formatted(dateFormatter)
let jsonData = try? encoder.encode(self)
return String(data: jsonData!, encoding: .utf8)!
} …Run Code Online (Sandbox Code Playgroud) 我不断看到如何扩展数组的不同语法。这是我看到的两个。有人能解释一下有什么区别吗?
extension Array where Element == StringProtocol {
func foo(){}
}
extension Array where Element:StringProtocol {
func foo(){}
}
Run Code Online (Sandbox Code Playgroud)
那么有什么区别呢?
奖金:
我正在尝试编写一个[String]与 和一起使用的扩展[Substring],并且建议我将其基于StringProtocol,因此如上所述。但是,如果我尝试做如下的事情......
func foo(separator:Character)
for line in self{
let components = line.split(separator: separator, maxSplits: 1, omittingEmptySubsequences: false)
let prefix = components[0].replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression) // Only trim the end of the prefix
let suffix = components.count > 1
? components[1].trimmingCharacters(in: .whitespaces) // Perform full trim on the suffix
: nil
... …Run Code Online (Sandbox Code Playgroud)