An advanced Ethereum/EVM mobile wallet
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
alpha-wallet-ios/Trust/Extensions/String.swift

68 lines
1.8 KiB

// Copyright SIX DAY LLC. All rights reserved.
import Foundation
extension String {
var hex: String {
let data = self.data(using: .utf8)!
return data.map { String(format: "%02x", $0) }.joined()
}
var hexEncoded: String {
let data = self.data(using: .utf8)!
return data.hexEncoded
}
var doubleValue: Double {
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.decimalSeparator = "."
if let result = formatter.number(from: self) {
return result.doubleValue
} else {
formatter.decimalSeparator = ","
if let result = formatter.number(from: self) {
return result.doubleValue
}
}
return 0
}
var trimmed: String {
return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
var asDictionary: [String: Any]? {
if let data = self.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
return [:]
}
}
return [:]
}
}
extension String {
func index(from: Int) -> Index {
return self.index(startIndex, offsetBy: from)
}
func substring(from: Int) -> String {
let fromIndex = index(from: from)
return substring(from: fromIndex)
}
func substring(to: Int) -> String {
let toIndex = index(from: to)
return substring(to: toIndex)
}
func substring(with r: Range<Int>) -> String {
let startIndex = index(from: r.lowerBound)
let endIndex = index(from: r.upperBound)
return substring(with: startIndex..<endIndex)
}
}