From 53fe3278b7d014a17f758de969dbf4846c66eb18 Mon Sep 17 00:00:00 2001 From: Peter Baumgartner Date: Wed, 7 Aug 2019 14:18:54 +0200 Subject: [PATCH] Added numeric extension to String --- BXSwiftUtils.xcodeproj/project.pbxproj | 4 ++ BXSwiftUtils/Strings/String+Numeric.swift | 48 +++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 BXSwiftUtils/Strings/String+Numeric.swift diff --git a/BXSwiftUtils.xcodeproj/project.pbxproj b/BXSwiftUtils.xcodeproj/project.pbxproj index 3114663..1ea57ae 100644 --- a/BXSwiftUtils.xcodeproj/project.pbxproj +++ b/BXSwiftUtils.xcodeproj/project.pbxproj @@ -116,6 +116,7 @@ D0DA3333213DA04A00994E89 /* DispatchGroup+Once.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0DA3331213DA04900994E89 /* DispatchGroup+Once.swift */; }; D0E9E01E22D5EA5E00ADB450 /* Collection+Assign.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0E9E01D22D5EA5D00ADB450 /* Collection+Assign.swift */; }; D0EA52DB20F8992300A0EF6D /* MTKTextureLoader+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0EA52D920F8992300A0EF6D /* MTKTextureLoader+Extensions.swift */; }; + D0EA873E22FAF3A60078AC7B /* String+Numeric.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0EA873D22FAF3A60078AC7B /* String+Numeric.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -238,6 +239,7 @@ D0DA3331213DA04900994E89 /* DispatchGroup+Once.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DispatchGroup+Once.swift"; sourceTree = ""; }; D0E9E01D22D5EA5D00ADB450 /* Collection+Assign.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Collection+Assign.swift"; sourceTree = ""; }; D0EA52D920F8992300A0EF6D /* MTKTextureLoader+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MTKTextureLoader+Extensions.swift"; sourceTree = ""; }; + D0EA873D22FAF3A60078AC7B /* String+Numeric.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Numeric.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -517,6 +519,7 @@ 48AD6C9C2147FCC400225D9C /* String+UniqueByIncrementing.swift */, D046DF87216BAB59004331C3 /* String+DataDetection.swift */, D08F44DF21708CD90060FBAE /* String+Regex.swift */, + D0EA873D22FAF3A60078AC7B /* String+Numeric.swift */, D05D0C0721C6687100FAF3CC /* String+Paths.swift */, 48C2480F2088CE9400DC9317 /* NSAttributedString+Codable.swift */, ); @@ -811,6 +814,7 @@ buildActionMask = 2147483647; files = ( D034B97A221C1F5900E94722 /* Mirror+IterateProperties.swift in Sources */, + D0EA873E22FAF3A60078AC7B /* String+Numeric.swift in Sources */, D08AC6402285AFF700DFBD66 /* BXKeychain.swift in Sources */, D0C8448B21D5FB3500229319 /* NotificationCenter+AutoRemoving.swift in Sources */, D0157ED0227DB41F00E34D65 /* Bundle+Localizations.swift in Sources */, diff --git a/BXSwiftUtils/Strings/String+Numeric.swift b/BXSwiftUtils/Strings/String+Numeric.swift new file mode 100644 index 0000000..0645bc5 --- /dev/null +++ b/BXSwiftUtils/Strings/String+Numeric.swift @@ -0,0 +1,48 @@ +//********************************************************************************************************************** +// +// String+Numeric.swift +// Helper function for numeric parsing +// Copyright ©2019 Peter Baumgartner. All rights reserved. +// +//********************************************************************************************************************** + + +import Foundation + + +//---------------------------------------------------------------------------------------------------------------------- + + +// MARK: - + +public extension String +{ + + /// Strips all non numeric charactef from a string. This includes leading and trailing whitespaces, as well + /// as any letters that make up units. En example would be "100%" -> "100". The resulting string can be easily + /// converted to an Int or Double. + + func strippingNonNumericCharacters() -> String + { + let numbericCharacters = Set("0123456789.,") + return self.filter { numbericCharacters.contains($0) } + } + + var intValue : Int? + { + return Int(self.strippingNonNumericCharacters()) + } + + var floatValue : Float? + { + return Float(self.strippingNonNumericCharacters()) + } + + var doubleValue : Double? + { + return Double(self.strippingNonNumericCharacters()) + } +} + + +//----------------------------------------------------------------------------------------------------------------------