作为R门客已指出;因为你没有这些实用程序类/结构的实例,所以它没有太大区别。
一个可能投票支持使用类在结构将是,你在其中明确限制在class
使用(protocol ...: class
)的协议声明你static
功能非常特殊的情况:对于结构对方(protocol ...: struct
)不可用。
/* only class types can conform to this protocol */
protocol MyClassProtocol: class {
static func split(str: String) -> [String]
}
extension MyClassProtocol {
static func split(str: String) -> [String]{
return str.characters.split{$0 == "."}.map(String.init)
}
}
/* Use custom implementation */
class BasicTools: MyClassProtocol {
class func split(str: String) -> [String]{
return str.characters.split{$0 == ","}.map(String.init)
}
}
/* Use default implementation */
class OtherBasicTools: MyClassProtocol { }
BasicTools.split("foo,bar") // ["foo", "bar"]
OtherBasicTools.split("foo.bar") // ["foo", "bar"]
在您的环境(的仅实用工具类/结构),上述:class
约束为MyClassProtocol
是不是真的有关。但是,如果MyClassProtocol
协议---除了包含静态工具---将被用作上下文中的委托,则相关的是对该委托的强有力的引用将创建保留周期。在这种情况下,我们需要将委托协议限制为只对引用类型可用(以符合),这允许我们对委托本身使用弱引用。在这种情况下,自然我们的工具箱必须在class
上下文中使用(而不是在结构中)。
例如,考虑下面的例子,我们有某些功能的静态工具箱我们希望提供给我们的所有不同类型的代表:
/* Delegate toolbox: static/class functions for use
with your delegates */
protocol MyBasicDelegateTools {
static func delegateCalled() ->()
}
extension MyBasicDelegateTools {
static func delegateCalled() ->() {
print("Logging: delegate was called")
}
}
一些代表在这里使用:
/* Delegate with access to your basic delegate tools */
protocol MyDelegate: class, MyBasicDelegateTools {
func arrUpdated(baz: [Int])
}
/* It's inherrent here that we let the reference to
the delegate be weak, to avoid a retain cycle. */
class Foo {
private var baz : [Int] = [] {
didSet {
if let _ = delegate?.arrUpdated(baz) {
delegate?.dynamicType.delegateCalled()
}
}
}
weak var delegate: MyDelegate?
}
/* Why? Since 'foo' in 'Bar' keeps a strong reference to the 'Foo' instance */
class Bar: MyDelegate {
var foo : Foo
var idx: Int = 0 {
didSet {
print("idx updated: \(idx)")
}
}
init(foo: Foo) {
self.foo = foo
self.foo.delegate = self
}
// MyDelegate
func arrUpdated(baz: [Int]) {
idx = baz.count
}
}
用法示例:
var a = Foo()
var b = Bar(foo: a)
a.baz.append(1)
// idx updated: 1 <-- from custom delegate
// Logging: delegate was called <-- from delegate toolbox
如果你只使用静态'functions'没有什么区别的。但是我发现第三方框架的一些函数不喜欢从'structs'内的'static functions'调用。例如,“解析”背景操作可能会以静默方式失败。从来没有真正花时间弄清楚为什么。 –