Skip to content

Commit 9f0b137

Browse files
author
Jimmy Arts
committed
Fixed optional issue
1 parent 7d8317b commit 9f0b137

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Sources/Base64.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import Foundation
44
/// URI Safe base64 encode
55
func base64encode(_ input:Data) -> String {
66
let data = input.base64EncodedData(options: NSData.Base64EncodingOptions(rawValue: 0))
7-
let string = String(describing: NSString(data: data, encoding: String.Encoding.utf8.rawValue))
7+
guard let cocoaString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else {
8+
return ""
9+
}
10+
let string = String(describing: cocoaString)
811
return string
912
.replacingOccurrences(of: "+", with: "-", options: NSString.CompareOptions(rawValue: 0), range: nil)
1013
.replacingOccurrences(of: "/", with: "_", options: NSString.CompareOptions(rawValue: 0), range: nil)
@@ -14,15 +17,15 @@ func base64encode(_ input:Data) -> String {
1417
/// URI Safe base64 decode
1518
func base64decode(_ input:String) -> Data? {
1619
let rem = input.characters.count % 4
17-
20+
1821
var ending = ""
1922
if rem > 0 {
2023
let amount = 4 - rem
2124
ending = String(repeating: "=", count: amount)
2225
}
23-
26+
2427
let base64 = input.replacingOccurrences(of: "-", with: "+", options: NSString.CompareOptions(rawValue: 0), range: nil)
2528
.replacingOccurrences(of: "_", with: "/", options: NSString.CompareOptions(rawValue: 0), range: nil) + ending
26-
29+
2730
return Data(base64Encoded: base64, options: NSData.Base64DecodingOptions(rawValue: 0))
2831
}

0 commit comments

Comments
 (0)