结构静态方法与Swift中的类级方法

时间:2016-11-26 03:43:40

标签: swift

我喜欢将实用程序方法组织为Swift中的结构体中的静态方法。例如,我可能会创建类似下面的内容

struct ImageUtility {
    static func someUtility(_ image: UIImage) -> UIImage? { /* ... */ }
    static func otherUtility(_ image: UIImage, croppedTo rect: CGRect) -> UIImage? { /* ... */ }
}

似乎我可以通过在类上使用类方法来完成同样的事情,如下所示:

class ImageUtility {
    class func someUtility(_ image: UIImage) -> UIImage? { /* ... */ }
    class func otherUtility(_ image: UIImage, croppedTo rect: CGRect) -> UIImage? { /* ... */ }

}

从结构或类中调用这些方法看起来是相同的,例如let newImage = ImageUtility.someUtility(image)。这些方法之间有什么重大差异吗?

将这些全部放入现有课程的扩展中会有什么好处吗?就像在这种情况下,将ImageUtility方法放入UIImage扩展名会更好吗?

0 个答案:

没有答案
相关问题