問題描述
假設(shè)我有一個名為 SwiftKit 的框架,它有一個名為 someClassMethod 的 UIView 擴展類方法和一個名為 someProperty 的屬性:
Say I have a Framework called SwiftKit, which has a UIView's extension class method named someClassMethod and a property named someProperty within it:
// SwiftKit
public extension UIView {
class func someClassMethod() {
print("someClassMethod from Swift Kit")
}
var someProperty: Double {
print("someProperty from Swift Kit")
return 0
}
}
我還有一個叫 SwiftFoundation 的框架,它里面還有一個 UIView 的擴展類方法叫 someClassMethod 和一個叫 someProperty 的屬性:
And I also have a Framework called SwiftFoundation, which also has a UIView's extension class method named someClassMethod and a property named someProperty within it:
// SwiftFoundation
public extension UIView {
class func someClassMethod() {
print("someClassMethod from Swift Foundation")
}
var someProperty: Double {
print("someProperty from Swift Foundation")
return 0
}
}
然后我創(chuàng)建了一個項目來介紹這些框架,事情是,如果我將它們都導(dǎo)入同一個 swift 文件并訪問這些擴展名,即使我指定了,我也會收到Ambiguous use of someProperty/someClassMethod()"錯誤SwiftKit.UIView.someClassMethod() 形式的調(diào)用:
Then I created a project introduced these Frameworks, things is, if I import both of them in the same swift file and access those extensions, I got a "Ambiguous use of someProperty/someClassMethod()" error, even if I specified the call in the form of SwiftKit.UIView.someClassMethod() :
import UIKit
import SwiftKit
import SwiftFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.someProperty // error: Ambiguous use of 'somProperty'
SwiftKit.UIView.someClassMethod() // error: Ambiguous use of 'someClassMethod()'
}
}
如果我只導(dǎo)入其中一個,模棱兩可的錯誤就會消失,但會發(fā)生更奇怪的事情:
If I only import one of them, the ambiguous error goes away, but more strange thing happens:
import UIKit
import SwiftKit
//import SwiftFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.someProperty
SwiftKit.UIView.someClassMethod()
}
}
控制臺打印出來:
來自 Swift 基金會的 someProperty
someProperty from Swift Foundation
來自 Swift Foundation 的 someClassMethod
someClassMethod from Swift Foundation
我的問題是:我怎樣才能毫不含糊地調(diào)用這些擴展(類/實例方法、屬性)?如果我不能,是否意味著我們應(yīng)該像通常使用 Objective-C 一樣為擴展名添加前綴?
My question is: How can I call these extensions(both class/instance method, properties) without ambiguous? If I cannot, does it mean we should add prefix to extension names as we usually do with Objective-C?
推薦答案
詳情
- Swift 3、Xcode 8.1
- Swift 4、Xcode 9.1
- Swift 5.1、Xcode 11.2.1
框架 SwiftFoundation 和 SwiftKit 具有相同名稱的屬性和函數(shù)
frameworks SwiftFoundation and SwiftKit has the same names of the properties and functions
使用不同的屬性和函數(shù)名稱
Use different names of the properties and functions
// SwiftFoundation
public extension UIView {
public class func swiftFoundationSomeClassMethod() {
print("someClassMethod from Swift Foundation")
}
public var swiftFoundationSomeProperty: Double {
print("someProperty from Swift Foundation")
return 0
}
}
方式2
對屬性和函數(shù)進行分組
Way2
Group the properties and functions
// SwiftKit
public extension UIView {
public class SwiftKit {
public class func someClassMethod() {
print("someClassMethod from Swift Kit")
}
public var someProperty: Double {
print("someProperty from Swift Kit")
return 0
}
}
var SwiftKit: SwiftKit { return SwiftKit() }
}
結(jié)果
import UIKit
import SwiftKit
import SwiftFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
_ = view.SwiftKit.someProperty
UIView.SwiftKit.someClassMethod()
_ = view.swiftFoundationSomeProperty
UIView.swiftFoundationSomeClassMethod()
}
}
項目
SwiftFoundation.UIView.swiftFoundationSomeClassMethod()
您使用命名空間的變體不正確,因為這兩個框架的所有 UIView 擴展都包含在您的 UIView 類中.查看下圖,您可以在 SwiftFoundation 中看到 SwiftKit 類和 swiftFoundationSomeClassMethod().這可能會使其他開發(fā)人員感到困惑.
Your variant of using the namespaces is not correct because all UIView extensions from both frameworks are included in you UIView class. Look at image bellow, you can see SwiftKit class and swiftFoundationSomeClassMethod() inside SwiftFoundation. This can confuse other developers.
這篇關(guān)于Swift 擴展:兩個模塊中的相同擴展功能的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!