Convert a String into an Int, Float, Boolean, and Data in Swift

Working with String is a common task in macOS and iOS apps. This post presents code examples for converting a String into an Int, Double, Float, Bool, and Data, including Unicode, in Swift.

  1. Convert a String into an Int, Int8, Int16, Int32, and Int64
  2. Convert a String into an UInt, UInt8, UInt16, UInt32, and UInt64
  3. Convert a String into a Double
  4. Convert a String into a Float and Float80
  5. Convert a String into a Bool
  6. Convert a String into Data (Unicode, UTF-8, UTF-16, UTF-32)
  7. Convert a String into NSData

FREE all Swift loading animations.

Loading Animation Example

Follow AdvancedSwift and immediately get access to commented code for these animations that you can use in your projects.

String Transformation For NSString

The examples in this post also apply to NSString. An NSString can be cast to a String using as. Example:

let nsString: NSString = "NS"
let string = nsString as String

Convert a String into an Int, Int8, Int16, Int32, and Int64

The Swift type Int, and related Int8, Int16, Int32, and Int64, contain a constructor that takes in a String argument and return an optional parsed value.

// Convert String to Int
let encodedInt = "3"
let int = Int(encodedInt)

// Examples for other Int types
let int8 = Int8(encodedInt)
let int16 = Int16(encodedInt)
let int32 = Int32(encodedInt)
let int64 = Int64(encodedInt)

Convert a String into a UInt, UInt8, UInt16, UInt32, and UInt64

Just like Int, the Swift type UInt, and related UInt8, UInt16, UInt32, and UInt64, contain a constructor that takes in a String argument and return an optional parsed value.

// Convert String to UInt
let encodedUInt = "3"
let uint = UInt(encodedInt)

// Examples for other UInt types
let uint8 = UInt8(encodedInt)
let uint16 = UInt16(encodedInt)
let uint32 = UInt32(encodedInt)
let uint64 = UInt64(encodedInt)

Convert a String into a Double

One constructor of the Swift type Double takes in a String argument and returns an optional parsed value.

// Convert a String to Double
let encodedDouble = "3.14"
let double = Double(encodedDouble)

Convert a String into a Float and Float80

One constructor of the Swift type Float, and similarly Float80, takes in a String argument and returns an optional parsed value.

// Convert a String to Float
let encodedFloat = "3.14"
let float = Float(encodedFloat)
let float80 = Float80(encodedFloat)

Convert a String into a Bool

Although the Bool type does not have a dedicated constructor that takes a String, comparison can be used to convert a String to a Bool.

// Convert a String to Bool
let encodedBoolean = "true"
let boolean = (encodedBoolean == "true")

Convert a String into Unicode (UTF-8, UTF-16, UTF-32)

There are three different types of Unicode character encodings: UTF-8, UTF-16 and UTF-32. The String method data(using:) takes in an enum specifying how the String should be encoded and returns an optional Data, with enum options like .unicode, .utf8, .utf16, and .utf32.

// Convert a String to Unicode
let string = "hello"
let unicode = string.data(using: .unicode)

// Convert a String to UTF-8, UTF-16, and UTF-32
let utc8 = string.data(using: .utf8)
let utf8 = string.data(using: .utf16)
let utf8 = string.data(using: .utf32)

Convert a String into NSData

The data(using:) method on a Swift String returns Data, which can be cast using as to NSData.

// Encode a String as Data
let string = "hello"
let unicode = string.data(using: .unicode)

// Cast Data to NSData
let nsData = unicode as? NSData

Transform a String into an Int, Double, Float, Bool, and Data in Swift

That's it! Using the different transformation methods presented in the code examples you can transform String into different Swift data types in your macOS and iOS apps.