Skip to content

Commit

Permalink
Added numeric extension to String
Browse files Browse the repository at this point in the history
  • Loading branch information
peterb180369 committed Aug 7, 2019
1 parent 2e10d03 commit 53fe327
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions BXSwiftUtils.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -238,6 +239,7 @@
D0DA3331213DA04900994E89 /* DispatchGroup+Once.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DispatchGroup+Once.swift"; sourceTree = "<group>"; };
D0E9E01D22D5EA5D00ADB450 /* Collection+Assign.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Collection+Assign.swift"; sourceTree = "<group>"; };
D0EA52D920F8992300A0EF6D /* MTKTextureLoader+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MTKTextureLoader+Extensions.swift"; sourceTree = "<group>"; };
D0EA873D22FAF3A60078AC7B /* String+Numeric.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Numeric.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -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 */,
);
Expand Down Expand Up @@ -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 */,
Expand Down
48 changes: 48 additions & 0 deletions BXSwiftUtils/Strings/String+Numeric.swift
Original file line number Diff line number Diff line change
@@ -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())
}
}


//----------------------------------------------------------------------------------------------------------------------

0 comments on commit 53fe327

Please sign in to comment.