Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Sources/InfoKit/Plist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,35 @@ public class Plist<T: Codable> {

}

/// Encode the data into plist file
///
/// - Parameter data: class to encode
/// - Returns: whether encode and write to tht plist file success
public func encode(data: T) -> Bool {
guard let resource = self.resource else {
return false
}

guard let path = self.bundle.path(forResource: resource, ofType: "plist", inDirectory: nil) else {
#if DEBUG
print("Resource: \(resource) not found in bundle: \(self.bundle)")
#endif
return false
}

do {
let encoder = PropertyListEncoder()
let data = try encoder.encode(data)
try data.write(to: URL(fileURLWithPath: path))
return true
} catch {
#if DEBUG
print("Error Encoding \(error)")
#endif
return false
}
}

/// Returns the value associated with the default bundled Info.plist file.
///
/// - Parameter plist: The Plist provider
Expand Down
20 changes: 20 additions & 0 deletions Tests/PlistTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,24 @@ class PlistTests: XCTestCase {

}

func testEncodeUserProvidedPlist() {

// Mocks
struct Products: Codable {
let foo: String
let bar: String
}

// Given
let bundle = Bundle(for: PlistTests.self)
let plist = Plist<Products>("Products", in: bundle)

let newProduct = Products(foo: "new foo", bar: "new foo bar")

let result = plist.encode(data: newProduct)

XCTAssertEqual(result, true)

}

}