简体   繁体   English

Swift 中的协议

[英]Protocols in Swift

I want to create a FileManager using protocols in Swift.我想使用 Swift 中的协议创建一个 FileManager。

It is simply a struct它只是一个结构

struct AppFile: AppFileManipulation {

    let fileName: String

    init(fileName: String)
    {
        self.fileName = fileName
    }

}

which is created in the code这是在代码中创建的

let file = AppFile(fileName: "testfile.txt")

now I want to separate out different functions into different protocols现在我想将不同的功能分成不同的协议

One of these gives the status for the file, here it is with file exists其中之一给出了文件的状态,这里是文件存在

protocol AppFileStatus
{
    func exists(file at: URL) -> Bool
}

extension AppFileStatus
{
    func exists(file at: URL) -> Bool
    {
        return FileManager.default.fileExists(atPath: at.path)
    }
}

and I want to have a separate protocol that has file operations我想有一个具有文件操作的单独协议

protocol AppFileManipulation : AppDirectoryNames, AppFileStatus
{
    func writeStringsToFile(containing: String, to path: FileManager.SearchPathDirectory, withName name: String) -> Bool
}

extension AppFileManipulation
{
        func writeStringsToFile(containing: String, to path: FileManager.SearchPathDirectory, withName name: String) -> Bool{

}
    }

Now for various reasons (to check if the file already exists, actually) I want the body of writeStringsToFile to access exists(file at: URL).现在由于各种原因(实际上是检查文件是否已经存在),我希望 writeStringsToFile 的主体能够访问存在(文件位于:URL)。

One solution would be to put everything into the same protocol, but really I wanted to split the functionality out.一种解决方案是将所有内容放入同一个协议中,但实际上我想将功能分开。 Another option would be to repeat code.另一种选择是重复代码。

How should I structure this so I can access the AppFileStatus methods from within the AppFileManipulation protocol?我应该如何构建它以便我可以从 AppFileManipulation 协议中访问 AppFileStatus 方法?

Here is a sample project;这是一个示例项目; https://github.com/stevencurtis/ProtocolFileHandling https://github.com/stevencurtis/ProtocolFileHandling

You might be looking for the where Self option for your extension:您可能正在为您的扩展寻找 where Self 选项:

struct AppFile: AppFileStatus, AppFileManipulation {

    let fileName: String

    init(fileName: String)
    {
        self.fileName = fileName
    }
}


protocol AppFileStatus
{
    func exists(file at: URL) -> Bool
}


extension AppFileStatus
{
    func exists(file at: URL) -> Bool
    {
        return FileManager.default.fileExists(atPath: at.path)
    }
}


protocol AppFileManipulation
{
    func writeStringsToFile(containing: String, to path: FileManager.SearchPathDirectory, withName name: String) -> Bool
}


extension AppFileManipulation where Self: AppFileStatus
{
    func writeStringsToFile(containing: String, to path: FileManager.SearchPathDirectory, withName name: String) -> Bool{
            let exists = self.exists(file: URL(fileURLWithPath: ""))

            return true
    }
}

and now you can call:现在您可以致电:

let file = AppFile(fileName: "testfile.txt")
file.writeStringsToFile(containing: ..., to: ..., withName: ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM