avatarBytefer

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4351

Abstract

"hljs-type">String</span> <span class="hljs-operator">=</span> <span class="hljs-string">"Hello, Swift!"</span></pre></div><p id="7608"><b>TypeScript Code</b></p><div id="3cf1"><pre><span class="hljs-keyword">let</span> <span class="hljs-attr">greeting</span>: <span class="hljs-built_in">string</span> = <span class="hljs-string">"Hello, TypeScript!"</span>;</pre></div><h2 id="707e">2. Int type</h2><p id="2dbc">The Int type is a special type with the same length as the native word length of the current platform:</p><ul><li>On 32-bit platforms, Int and Int32 are the same length.</li><li>On 64-bit platforms, Int and Int64 are the same length.</li></ul><p id="e8d5"><b>Swift Code</b></p><div id="427b"><pre><span class="hljs-keyword">var</span> age: <span class="hljs-type">Int</span> <span class="hljs-operator">=</span> <span class="hljs-number">25</span></pre></div><p id="4038"><b>TypeScript Code</b></p><div id="618f"><pre><span class="hljs-keyword">let</span> <span class="hljs-attr">age</span>: <span class="hljs-built_in">number</span> = <span class="hljs-number">25</span>;</pre></div><h2 id="7a58">3. Float/Double type</h2><p id="9bd1">Float is a number that contains a decimal part, such as 3.14159, 1.0, etc.</p><ul><li>Float type represents 32-bit floating point numbers, you can use this type if you don’t need a high precision.</li><li>Double type represents 64-bit floating point numbers, please use this type when you need to store very large or high-precision floating point numbers.</li></ul><p id="fb34"><b>Swift Code</b></p><div id="bb70"><pre><span class="hljs-keyword">let</span> radius: <span class="hljs-type">Float</span> <span class="hljs-operator">=</span> <span class="hljs-number">2.5</span> <span class="hljs-keyword">let</span> pi: <span class="hljs-type">Double</span> <span class="hljs-operator">=</span> <span class="hljs-number">3.1415926535</span> </pre></div><p id="29da"><b>TypeScript Code</b></p><div id="bc37"><pre><span class="hljs-keyword">const</span> <span class="hljs-attr">radius</span>: <span class="hljs-built_in">number</span> = <span class="hljs-number">2.5</span>; <span class="hljs-keyword">const</span> <span class="hljs-attr">pi</span>: <span class="hljs-built_in">number</span> = <span class="hljs-number">3.1415926535</span>;</pre></div><h2 id="0394">4. Bool type</h2><p id="bc21"><b>Swift Code</b></p><div id="ed12"><pre><span class="hljs-keyword">var</span> completed: <span class="hljs-type">Bool</span> <span class="hljs-operator">=</span> <span class="hljs-literal">true</span></pre></div><p id="4963"><b>TypeScript Code</b></p><div id="00de"><pre><span class="hljs-keyword">let</span> <span class="hljs-attr">completed</span>: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">true</span>;</pre></div><h1 id="1b67">Type inference</h1><p id="0251">Swift and TypeScript are both type-safe languages. They check types at compile time to ensure that code does not attempt to perform unallowed operations. In addition, they both support <b>type inference</b>, which means that the compiler can automatically infer the type of a variable.</p><p id="5362"><b>Swift Code</b></p><div id="024b"><pre><span class="hljs-comment">// The type of the message variable is inferred to be a String.</span> <span class="hljs-keyword">var</span> message <span class="hljs-operator">=</span> <span class="hljs-string">"Swift is amazing!"</span>

<span class="hljs-comment">// The type of the pages variable is inferred to be a Int.</span> <span class="hljs-keyword">var</span> pages <span class="hljs-operator">=</span> <span class="hljs-number">50</span></pre></div><p id="bbc0"><b>TypeScript Code</b></p><div id="ada9"><pre><span class="hljs-comment">// The type of the message variable is inferred to be a string.</span> <span class="hljs-keyword">let</span> message = <span class="hljs-string">"TypeScript is awesome!"</span>;

<span class="hljs-comment">// The type of the pages variable is inferred to be a number.</span> <span class="hljs-keyword">let</span> numberOfPages = <span class="hljs-number">50</span>;</pre></div><h1 id="a2a6">Type alias</h1><p id="c589">In programming, type aliasing is a way to give a new name to an existing type. This helps to improve the readability and maintainability of the code. In both Swift and TypeScript, we can use type aliases to defin

Options

e our own named types.</p><p id="ab6a">In Swift, we use the <code>typealias</code> keyword to create type aliases. In TypeScript, type aliases are created using the <code>type</code> keyword.</p><p id="aeaf"><b>Swift Code</b></p><div id="756d"><pre><span class="hljs-keyword">typealias</span> <span class="hljs-type">SampleRate</span> <span class="hljs-operator">=</span> <span class="hljs-type">Double</span>

<span class="hljs-keyword">let</span> standardSampleRate: <span class="hljs-type">SampleRate</span> <span class="hljs-operator">=</span> <span class="hljs-number">44100.0</span> <span class="hljs-keyword">let</span> highQualitySampleRate: <span class="hljs-type">SampleRate</span> <span class="hljs-operator">=</span> <span class="hljs-number">96000.0</span></pre></div><p id="27bf"><b>TypeScript Code</b></p><div id="9c7a"><pre><span class="hljs-keyword">type</span> <span class="hljs-title class_">SampleRate</span> = <span class="hljs-built_in">number</span>;

<span class="hljs-keyword">const</span> <span class="hljs-attr">standardSampleRate</span>: <span class="hljs-title class_">SampleRate</span> = <span class="hljs-number">44100.0</span>; <span class="hljs-keyword">const</span> <span class="hljs-attr">highQualitySampleRate</span>: <span class="hljs-title class_">SampleRate</span> = <span class="hljs-number">96000.0</span>;</pre></div><h1 id="6853">String interpolation</h1><p id="0bc4">String interpolation is a way of embedding variables or expressions in strings, making them more flexible and dynamic. In both Swift and TypeScript, we can use string interpolation to build strings with dynamic content.</p><p id="6adb">In Swift, string interpolation is accomplished by including variables or expressions in a string and using the <code>()</code> syntax. In TypeScript, we use backquotes (``) to create template strings and the <code>${}</code> syntax to insert variables or expressions.</p><p id="4a48"><b>Swift Code</b></p><div id="653b"><pre><span class="hljs-keyword">let</span> x <span class="hljs-operator">=</span> <span class="hljs-number">2023</span> <span class="hljs-keyword">let</span> y <span class="hljs-operator">=</span> <span class="hljs-number">1</span>

<span class="hljs-keyword">let</span> result <span class="hljs-operator">=</span> <span class="hljs-string">"The sum of <span class="hljs-subst">(x)</span> and <span class="hljs-subst">(y)</span> is <span class="hljs-subst">(x <span class="hljs-operator">+</span> y)</span>."</span> <span class="hljs-built_in">print</span>(result)</pre></div><p id="82bd"><b>TypeScript Code</b></p><div id="e477"><pre><span class="hljs-keyword">const</span> <span class="hljs-attr">x</span>: <span class="hljs-built_in">number</span> = <span class="hljs-number">2023</span>; <span class="hljs-keyword">const</span> <span class="hljs-attr">y</span>: <span class="hljs-built_in">number</span> = <span class="hljs-number">1</span>;

<span class="hljs-keyword">const</span> <span class="hljs-attr">result</span>: <span class="hljs-built_in">string</span> = <span class="hljs-string">The sum of <span class="hljs-subst">${x}</span> and <span class="hljs-subst">${y}</span> is <span class="hljs-subst">${x + y}</span>.</span>; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(result);</pre></div><p id="96df">This article introduces the variables, constants, and data types in Swift. By comparing it with TypeScript syntax, we hope it will help you understand Swift better. If you want to learn Swift, you can follow me on <a href="https://medium.com/@bytefer">Medium</a> or <a href="https://twitter.com/Tbytefer">Twitter</a> to read more about Swift and TS!</p><div id="1440" class="link-block"> <a href="https://medium.com/@bytefer/list/688ee7c12807"> <div> <div> <h2>Mastering TypeScript Series</h2> <div><h3>This series will introduce the core knowledge and techniques of TypeScript in the form of animations.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*624748c44fc686389a9ed7259663c489edb2ceb7.jpeg)"></div> </div> </div> </a> </div></article></body>

Swift Tutorials for Front-end Developers: Variables, Constants, and Data Types

Master Swift in 2024 and Start Developing Your Own iOS/macOS App

Welcome to the Mastering Swift tutorial series, in this article we will cover variables, constants, and data types in Swift.

Next, we launch Xcode and select “File” > “New” > “Playground”. Create a new Playground and name it “Variables”.

Declare and use variables

Use the var keyword to define variables in Swift. Variable values ​​can be changed during their lifetime.

Swift Code

var name = "John"
name = "Bytefer"

TypeScript Code

let name = "John";
name = "Bytefer";

Declare and use constants

Use the let keyword to define constants in Swift. Once a constant is assigned a value, its value cannot be changed.

Swift Code

let birthYear = 2015
// Error: Cannot assign to value: 'birthYear' is a 'let' constant
birthYear = 2016

TypeScript Code

const birthYear = 2015;
// Error: Cannot assign to 'birthYear' because it is a constant.
birthYear = 2016; 

Data types

Now you know how to define variables and constants. While defining constants and variables, we can explicitly declare the type of the variable. Below we will cover the common data types in Swift.

1. String type

The String type is a collection of character sequences.

Swift Code

var greeting: String = "Hello, Swift!"

TypeScript Code

let greeting: string = "Hello, TypeScript!";

2. Int type

The Int type is a special type with the same length as the native word length of the current platform:

  • On 32-bit platforms, Int and Int32 are the same length.
  • On 64-bit platforms, Int and Int64 are the same length.

Swift Code

var age: Int = 25

TypeScript Code

let age: number = 25;

3. Float/Double type

Float is a number that contains a decimal part, such as 3.14159, 1.0, etc.

  • Float type represents 32-bit floating point numbers, you can use this type if you don’t need a high precision.
  • Double type represents 64-bit floating point numbers, please use this type when you need to store very large or high-precision floating point numbers.

Swift Code

let radius: Float = 2.5
let pi: Double = 3.1415926535

TypeScript Code

const radius: number = 2.5;
const pi: number = 3.1415926535;

4. Bool type

Swift Code

var completed: Bool = true

TypeScript Code

let completed: boolean = true;

Type inference

Swift and TypeScript are both type-safe languages. They check types at compile time to ensure that code does not attempt to perform unallowed operations. In addition, they both support type inference, which means that the compiler can automatically infer the type of a variable.

Swift Code

// The type of the message variable is inferred to be a String.
var message = "Swift is amazing!"

// The type of the pages variable is inferred to be a Int.
var pages = 50

TypeScript Code

// The type of the message variable is inferred to be a string.
let message = "TypeScript is awesome!";

// The type of the pages variable is inferred to be a number.
let numberOfPages = 50;

Type alias

In programming, type aliasing is a way to give a new name to an existing type. This helps to improve the readability and maintainability of the code. In both Swift and TypeScript, we can use type aliases to define our own named types.

In Swift, we use the typealias keyword to create type aliases. In TypeScript, type aliases are created using the type keyword.

Swift Code

typealias SampleRate = Double

let standardSampleRate: SampleRate = 44100.0
let highQualitySampleRate: SampleRate = 96000.0

TypeScript Code

type SampleRate = number;

const standardSampleRate: SampleRate = 44100.0;
const highQualitySampleRate: SampleRate = 96000.0;

String interpolation

String interpolation is a way of embedding variables or expressions in strings, making them more flexible and dynamic. In both Swift and TypeScript, we can use string interpolation to build strings with dynamic content.

In Swift, string interpolation is accomplished by including variables or expressions in a string and using the \() syntax. In TypeScript, we use backquotes (``) to create template strings and the ${} syntax to insert variables or expressions.

Swift Code

let x = 2023
let y = 1

let result = "The sum of \(x) and \(y) is \(x + y)."
print(result)

TypeScript Code

const x: number = 2023;
const y: number = 1;

const result: string = `The sum of ${x} and ${y} is ${x + y}.`;
console.log(result);

This article introduces the variables, constants, and data types in Swift. By comparing it with TypeScript syntax, we hope it will help you understand Swift better. If you want to learn Swift, you can follow me on Medium or Twitter to read more about Swift and TS!

Programming
Web Development
JavaScript
iOS
Swift
Recommended from ReadMedium