In Swift, what type of object are basic data types?


Posted On: Feb 22, 2018


Swift utilizes the standard set of basic data types for different purposes such as numbers, Boolean values, and strings.

1.Int: Int is used to store the integer value. On 32-bit devices, it is always 32 bits deep and on 64-bit devices, it has 64 bits deep. You are able to access the maximum and minimum values the type enable store with the help of the min and max static properties:

//output on 32-bit device: -2147483648
//output on 64-bit device: -9223372036854775808
//output on 32-bit device: 2147483647
//output on a 64-bit device: 9223372036854775807

2. Double and Float: The use of Float and Double in Swift is considered when you are used to working with the decimal number. Double will be always 64-bit whereas float is always a 32- bit value. It does not depend upon the architecture of the device. The compiler utilizes the Double type instead of a Float in case of using decimal literal values. So, if you don't need the value of a 64-bit value, then it is definitely that you declare

Float variable.

var distance = 0.0 //distance is a Double var seconds: Float = 0.0 // second is a Float

3. Bool: The Bool basic type is used to store the Boolean value. The main difference between the Objective-C and Swift is it uses true and false instead of YES and NO as in Objective-C

It is unable to compare the value to 0 or nil in Swift. The expression that can definitely retort a Bool cam come into used to describe a boot value. Let's take an example

var someInteger = 0
var hasSome:Bool = (someInteger != 0)
//hasSome: false
someInteger = 100
hasSome = (someInteger != 0)
//hasSome = true

4. Strings: String literals are the text that is enclosed by double quotes in Swift. var greetingString = "I am Rich"

A string is the factory of the characters and the character pictures a Unicode character, has symbols, one of more than 100 scripts and 110,000 characters. There are a large number of character encoding method to implement the characters like UTF-8 or UTF-16. These encoding methods store every character with the help of a variable number of bytes in memory.

5. Arrays: In Swift, arrays are the collection types. It is an arranged list of the items of the equal type. At the time of declaring an array in Swift, you need to specify what type it will contain. When it is done, it is able to contain only that type. This whole process is to help you in ensuring the expected guaranteed type when you pull an item out of the array. You enclosed a list of elements with square brackets to create an array literal. For example var dogs = ["Harlow", "Cliff", "Rusty", "Mia", "Bailey"]

You need to assure that all the should be of equal types or else you will get a compile-time error.

It consists of two ways to represent that is long form and short form. Both the ways are comparable can be used mutually

Long form: Array Short form: [ValueType]

6. Dictionaries: A dictionary is an unordered factory of items of a particular type and every type is connected with a unique key. Like the arrays, it also contains the two ways to represent the type: the short form and the long form. Both of these types are comparable and can be used mutually.

Long form: Dictionary<KeyType, ValueType>
Short form: [KeyType: ValueType]
Here is an example of syntax to declare and initialize a dictionary with the use of short form.
var people: [String:SomePersonClass] = [:] //explicit type
//or, alternately
var people = [String:SomePersonClass]() //implicit type

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Swift Interview Questions

    What is IOS Swift?

    iOS swift is an intuitive and stalwart language for the iOS. It is joyful to deal with the writing stuff in Swift. It is very advanced and its syntax is also concise and expressive too...

    Swift Interview Questions

    What is Dictionary in Swift?

    It enables you to store the key-value pairs and access the value by providing the key.It is similar to that of the hash..

    Swift Interview Questions

    Explain Enum in Swift?

    Basically, it is a type that contains a group of various related values under the same umbrella...