Say I have a Framework called SwiftKit, which has a UIView\'s extension class method named someClassMethod and a property named someProperty within it:
// Sw
If it's an extension of an ObjC NSObject
object e.g. UIView
, then yes the extension method requires a prefix.
However, those who find the underscores unsightly can try this alternative that uses Swift protocols to replace the UIColor.red.my_toImage()
with UIColor.red.my.toImage()
here: Better way to manage swift extensions in your project
frameworks SwiftFoundation and SwiftKit has the same names of the properties and functions
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
}
}
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() }
}
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()
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.