Skip to content

periclesm/SwiftExtensions

Repository files navigation

SwiftExtensions

As the name suggests, this repository is a compilation of extensions used in specific types or objects in Swift.

Although the iOS SDK has a comprehensive set of functions to manipulate the values or objects, some are missing and ideally, it's good to be included somewhere centrally (as in an extension) rather than just computing/converting/generating the result inline inside a class.

The list is far from absolute; this is just a list of the extensions I have used in the recent years, updated for latest version. Needless to say that addidtion or deprecations might happen in the list in the future.

The Xcode project contains Unit Tests and a simple UI to display some of the extensions included in the repository (might add more examples in the future).

Contents:

Foundation:

Collection:
  • isNotEmpty: Provides a negation of .isEmpty that simply checkes if the collection (Array, Dictionary, Set) has content. (Shamelessly stoles from Kotlin)
Data:
  • Convert Data to a Base64 String. While it already exists in the SDK, this conversion takes into consideration URL Encoding so that the string can be added in a JSON payload and sent to a service. There is also a String extension that does the opposite, decoding a URL Encoded String back to Data.
Int:
  • Ordinal String: Takes the given Int and creates a String with ordinal numbering (ex. 1st, 2nd, 3rd etc). Supports localization (English, French, Gernam, Spanish and Greek already there). Also supports gender for masculine, feminine and neuter.
String:
  • isNotEmpty: Same as in collection (string is a collection of characters in swift), returns a boolean if the String is not empty (or not).

  • trim: Shortcode for trimming whitespaces and new lines at the beginning and end of the string.

  • urlEncode: Shortcode for adding Percent encodings to a string (url Query).

  • toBase64: Shortcode for creating a Base64 String.

  • removeSpaces: removes all spaces from the string.

  • removeCharacters: removes the given characters from the string.

  • randomString: generates a random string with a predefined length.

  • customUUID: generates a Universally Unique Identifier of specific length, segment size and separation character.

  • rearrangeString: Changes the order of characters in the string either by giving a specific order (if you know the string) or randomly (if you include numbers larger than the string count in the pattern, it will loop from start until the number is reached).

  • emoji: in a string that contains the emoji code (remove "U+" at start), it will return the corresponding emoji.

  • emoji (with codes array): renders and returns a multi scalar emoji.

  • countryFlag: given the country ISO Code (US, Uk, FR, De, GR), returns its flag.

TimeInterval:
  • timeFormattedString: given the duration of an audio or video segment (in TimeInterval aka Double -- see AV Foundation), it will return the duration as a string in hh:mm:ss
URL:

MapKit:

MKMapView:
  • currentLatitudinalMeters: returns the latitudinal size in meters (that is the distance shown in device or view width)

  • currentLatitudinalFeet: same as above in feet

  • currentLongitudinalMeters: returns the longtitudinal size in meters (as before but this time it is the distance shown in device or view height)

  • currentLongitudinalFeet: same as above but in feet

  • getMapDistance: compound function to show the latidudinal or longtitudinal distance (explicitly stated) in meters/kilometers or feet/miles

UIKit:

UIColor:

Provides initialization and variables to define your own colors along with the standard ones. Uses the hex color code with a parameter to include the P3 color profile or fallback to sRGB.

UIFont:

Provides functions to define your own fonts based on their postscript name and define the size.

UIImage:

Contains functions to get the Image format (jpeg, png, gif, webm, bitmap etc) from image data, check if a png image contains transparent pixels, shortcodes for png and jpeg conversions and convert an image to Base64 encoded string for uploading it to a service.

UIImageView:

Contains a basic image transition function (with animation) and a basic fetch from the internet function. While there are far more sophisticated and fancy methods to do both, these functions provide a quick way to fetch, animate and diplsay an image.

UILabel:

isTextWrapped: returns a boolean to indicate if the text is wrapped to the next line underneath. Note: this heavily depends on the device width (iPhone SE, Max or iPad) and requires that the text is already been set in the UILabel and the constraints have already been calculated.

UIApplication:

Has functions to get the key window and the current top controller.

UIViewController:

Contains functions to check if the current view controller is presented modally or is pushed as well as to determine it's parent.

About

A compilation of useful functions as Swift extensions.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages