Change coder signatures

This commit is contained in:
Daniel Saidi 2020-04-29 22:56:24 +02:00
parent 06e576418c
commit 53b3413131
6 changed files with 10 additions and 10 deletions

View File

@ -18,7 +18,7 @@ public class Base64StringCoder: StringCoder {
/** /**
Decode a base64 encoded string. Decode a base64 encoded string.
*/ */
public func decode(string: String) -> String? { public func decode(_ string: String) -> String? {
guard let data = Data(base64Encoded: string, options: .ignoreUnknownCharacters) else { return nil } guard let data = Data(base64Encoded: string, options: .ignoreUnknownCharacters) else { return nil }
return String(data: data, encoding: .utf8) return String(data: data, encoding: .utf8)
} }
@ -26,7 +26,7 @@ public class Base64StringCoder: StringCoder {
/** /**
Encode a string to base64. Encode a string to base64.
*/ */
public func encode(string: String) -> String? { public func encode(_ string: String) -> String? {
let data = string.data(using: .utf8) let data = string.data(using: .utf8)
let encoded = data?.base64EncodedData(options: .endLineWithLineFeed) let encoded = data?.base64EncodedData(options: .endLineWithLineFeed)
guard let encodedData = encoded else { return nil } guard let encodedData = encoded else { return nil }

View File

@ -14,5 +14,5 @@ import Foundation
*/ */
public protocol StringDecoder: AnyObject { public protocol StringDecoder: AnyObject {
func decode(string: String) -> String? func decode(_ string: String) -> String?
} }

View File

@ -14,5 +14,5 @@ import Foundation
*/ */
public protocol StringEncoder: AnyObject { public protocol StringEncoder: AnyObject {
func encode(string: String) -> String? func encode(_ string: String) -> String?
} }

View File

@ -48,13 +48,13 @@ extension CodingScreen {
func base64Decode() { func base64Decode() {
let string = coder.encode(string: "This is a string")! let string = coder.encode(string: "This is a string")!
let result = coder.decode(string: string)! let result = coder.decode(string)!
resultText = "\"\(string)\" was decoded to \"\(result)\"" resultText = "\"\(string)\" was decoded to \"\(result)\""
} }
func base64Encode() { func base64Encode() {
let string = "This is a string" let string = "This is a string"
let result = coder.encode(string: string)! let result = coder.encode(string)!
resultText = "\"\(string)\" was encoded to \"\(result)\"" resultText = "\"\(string)\" was encoded to \"\(result)\""
} }
} }

View File

@ -27,7 +27,7 @@ class Base64StringEncoderTests: QuickSpec {
foo foo
bar bar
""" """
let encoded = coder.encode(string: string) let encoded = coder.encode(string)
expect(encoded).to(equal("Zm9vCmJhcg==")) expect(encoded).to(equal("Zm9vCmJhcg=="))
} }
} }
@ -36,7 +36,7 @@ bar
it("fails for non-encoded string") { it("fails for non-encoded string") {
let string = "test" let string = "test"
let decoded = coder.decode(string: string) let decoded = coder.decode(string)
expect(decoded).to(beNil()) expect(decoded).to(beNil())
} }
@ -45,8 +45,8 @@ bar
foo foo
bar bar
""" """
let encoded = coder.encode(string: string)! let encoded = coder.encode(string)!
let decoded = coder.decode(string: encoded) let decoded = coder.decode(encoded)
expect(decoded).to(equal(string)) expect(decoded).to(equal(string))
} }
} }