StdTypeHelper.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. extension Bool {
  22. // Create a boolean from an optional string
  23. public init?(_ value: String?) {
  24. guard let value = value else {
  25. return nil
  26. }
  27. self.init(value)
  28. }
  29. }
  30. extension Double {
  31. // Create a double from an optional string
  32. public init?(_ value: String?) {
  33. guard let value = value else {
  34. return nil
  35. }
  36. self.init(value)
  37. }
  38. }
  39. extension UInt64 {
  40. // Create a uint64 from an optional string
  41. public init?(_ value: String?) {
  42. guard let value = value else {
  43. return nil
  44. }
  45. self.init(value)
  46. }
  47. }
  48. extension String {
  49. // Extract a character from a string
  50. // Example: "foo"[1] == "o"
  51. subscript (i: Int) -> Character {
  52. return self[index(startIndex, offsetBy: i)]
  53. }
  54. }