Types In Swift

Bekir Yeşilkaya
4 min readDec 19, 2021

--

Photo by Alex Bachor on Unsplash

When you start programming with the Swift programming language, first of all, questions about types such as what are primitive types, what is a class, what is a structure, what is an enumeration, what is a protocol, etc. come to mind. So what types are there in swift? There are 2 kinds of types in Swift:Named types and Compound types.

Named Types

“A named type is a type that can be given a particular name when it’s defined. ”(Swift.org)For instance, when we create an instance from named of SomeClass, it has SomeClass type.I’ll explain in detail below with examples. So what are the named types? Primitive types, classes, structures, enums, protocols, arrays , dictionaries are the named types. See basic type examples in below.

Each variable has a named type when creates an instance above.

Compound Types

“A compound type is a type without a name, defined in the Swift language itself.”(Swift.org).There are two compound types: function types and tuple types. Compound types can contain named types, compound types or both of them.For instance, the tuple type as (Double,(String,Int)) contains 2 elements . First one is named type as Double, second one is another compound type so tuple.Now I will explain Function and Tuple types below.

Function Type

“A function type represents the type of a function, method, or closure and consists of a parameter and return type separated by an arrow (->)”(Swift.org). I gave some examples below to understand function type.

So when a variable create from function how it’s gonna be? See examples below.

I created some void functions and a variable as a named “someFunc”. Then I assign each function to “someFunc” variable. Some questions may come to your mind like “How it’s gonna be when I give a function which has different names or argument names?”. The answer is in the TYPE.When I declared “someFunc” varible, I defined it as “firstFunction” which has a type as “(Int,Int)->()”.Then I assigned “secondFunction” and “thirdFunction” to it. And “secondFunction” and “thirdFunction” have same type. But “secondFunction” and “thirdFunction” have different names or argument names. The answer is “argument names in functions and methods aren’t part of the corresponding function type.”. So “someFunc” variable expect same type.When I give some different arguments and argument types , I will get compiler error.See examples below.

As you see, I got some errors.Because “fourthFunction” and “fifthFunction” have different types. “fourthFunction” has a type as “(Int,String)->()” and “fifthFunction” has a type as “(Int,Int,Int)->()”.

For Closure,same things apply here.See examples below.

As a result, when we define or assign a variable with function, we should pay attention to its type.

Tuple Type

“A tuple type is a comma-separated list of types, enclosed in parentheses.”(Swift.org). Types mean a tuple can include named types and compound types.See examples below to understand clearly tuples.

⚠️ “When an element of a tuple type has a name, that name is part of the type.”(Swift.org).So this means you can’t different name to an element of tuple.See example.

As you see each element represent different type.

⚠️ “All tuple types contain two or more types, except for Void which is a type alias for the empty tuple type, ().”(Swift.org)

As a result, I tried to tell you some types. In my next article, I will tell you about Opaque and Metatype Types. For more, check out swift.org.

This is my first post. I may have some mistakes. I hope you will receive it with understanding.

Best regards.

Reference

https://docs.swift.org/swift-book/ReferenceManual/Types.html

--

--