The LogStore will store the log in an array of strings. During package creation, Xcode added a Sources folder for the code. That folder contains a subfolder with one source code file, LogStore.swift. Replace the contents of that file with the following code:
| import Foundation |
| |
| struct LogStore { |
| static var log: [String] = [] |
| } |
Note the keyword static. Properties with this keyword are stored in the type itself. Such properties are called type properties. This means we don’t need to create an instance of LogStore to access the log array. Instead, we can access it with LogStore.log.
Usually we print log output using the global print function. We want a similar function that prints to the debug console and also adds the string to the log array. This means the function needs to be defined in the global scope and not within the LogStore or any other type. Add the printLog function to LogStore.swift as shown in the following code. The added lines are highlighted.
| import Foundation |
| |
| struct LogStore { |
| static var log: [String] = [] |
| } |
| |
» | public func printLog(_ string: String) { |
» | print(string) |
» | |
» | LogStore.log.append(string) |
» | } |
This is all we need to store the log for later access from within the app. With this, the first part of our little library is finished and we can move on to the second part—presenting the log in the host app.