Skip to content

Swift

cotrOperators

Swift Mathematical Operators

// Swift Mathematical Operators
// Addition: +
// Subtraction: -
// Multiplication: *
// Division: /
// Modulus (Remainder): %
// Exponentiation: **
// Increment: Use += 1
// Decrement: Use -= 1
// Assignment: =
// Addition assignment: +=
// Subtraction assignment: -=
// Multiplication assignment: *=
// Division assignment: /=
// Modulus assignment: %=
// Exponentiation assignment: **=

cotrTypesBoolTrue

Swift Boolean True

true

cotrTypesBoolFalse

Swift Boolean False

false

cotrConst

Swift Create Constant

let ${1:myConst} = $2;

cotrCommentMulti

Swift Multi-Line Comment

/*
* ${1:Your comment here}
*/

cotrTypesDate

Swift Date Type

Date

cotrVarNullable

Swift Create Nullable Variable

var ${1:myVar}: ${2:Type}? = $3

cotrForLoop

Swift For Loop

for ${1:i} in ${2:0..<10} {
// Your code here
}

cotrIfElse

Swift If Else Statement

if ${1:condition} {
${2:// Your code here}
} else if ${3:condition} {
${4:// Your code here}
} else {
${5:// Your code here}
}

cotrVarDate

Swift Create Date Variable

let ${1:myDate} = Date()

cotrTypeConvert

Swift Type Conversion

// Swift Type Conversion:
// Swift emphasizes explicit type conversions for safety.
// Explicit conversions:
// - variable as? TargetType // Safe cast (returns nil if conversion fails)
// - TargetType(variable) // Forced cast (can crash if conversion fails)
// Note:
// - Use safe casts (as?) whenever possible.
// - Only use forced casts (TargetType()) when you are certain the conversion will succeed.

cotrTypesInt

Swift Integer Type

Int

cotrPrintMulti

Swift Print Multi

print("""
${1:Line 1}
${2:Line 2}
${3:Line 3}
""")

cotrInfo

Swift Info

Typing: Statically typed
Paradigm: Multi-paradigm: object-oriented, functional, imperative, block-structured
Compilation: Compiled
Concurrency: Supports multi-threading and concurrent programming with Grand Central Dispatch (GCD) and async/await

cotrEntry

Swift Entry Point

// Swift Entry Point
// To run this program, use: `swift filename.swift`
import Foundation
print("Hello, World!")

cotrTypesMap

Swift Map Type

Dictionary<${1:KeyType}, ${2:ValueType}>

cotrVarString

Swift Create String Variable

var ${1:myString} = ${2:'Your string here'};

cotrTernary

Swift Ternary Operator

${1:condition} ? ${2:trueValue} : ${3:falseValue}

cotrEqual

Swift Equal To

==

cotrTypesList

Swift List Type

Array<${1:Type}>

cotrGenMap

Swift Generate Dictionary

var ${1:myDictionary} =
Dictionary(uniqueKeysWithValues: zip([${2:'key1'}, ${3:'key
2'}], [${4:'value1'}, ${5:'value2'}]));

cotrIf

Swift If Statement

if ${1:condition} {
${2:// Your code here}
}

cotrStructure

Swift Project Structure (High-Level)

// Recommended High-Level Swift Project Structure:
// - /Sources
// - Contains the Swift source files.
// - Organize by feature or functionality in different directories.
// - /Tests
// - Contains Swift test files.
// - Organize tests to mirror the structure of the /Sources directory.
// - Package.swift
// - The manifest file for Swift Package Manager.
// - /Resources
// - Non-code resources like images, data files, etc.
// - /Documentation
// - Documentation files for the project.
// - /Scripts
// - Utility scripts for building, running, etc.
// Note:
// - This structure is common for projects managed with Swift Package Manager.
// - For Xcode projects, the structure might be different, typically organized within an .xcodeproj file.

cotrTypesChar

Swift Character Type

Character

cotrTypesAny

Swift Dynamic Type

Any

cotrLambda

Swift Closure

let ${1:myClosure} = { (${2:parameters}) -> ${3:ReturnType} in
${4:return expression}
}

cotrTypeCheck

Swift Type Check

type(of: ${1:variable})

cotrTypes

Swift Types

$BLOCK_COMMENT_START
Swift is a statically typed language.
Types in Swift include:
- Int, UInt: Signed and unsigned integers
- Float, Double: Floating-point numbers
- Bool: Boolean
- String: Textual data
- Character: A single character
- Array<Element>: An ordered collection of elements
- Dictionary<Key, Value>: A collection of key-value pairs
- Set<Element>: An unordered collection of unique elements
- Optional<Type>: An optional value of Type
- Tuple: A group of values
- Enum: An enumeration
- Struct: A structure
- Class: A class
- Protocol: A protocol
- Closure: A closure or lambda function
Read more here: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/types/
$BLOCK_COMMENT_END

cotrFunc

Swift Function

func ${2:myFunction}(${3:parameters}) -> ${1:Void} {
${4:// Your code here}
}

cotrVarNum

Swift Create Double Variable

var ${1:myDouble} = ${2:0.0};

cotrWhileLoop

Swift While Loop

while ${1:condition} {
${2:// Your code here}
}

cotrFuncArgs

Swift Function Args

// In Swift, functions can have arguments with default values.
func ${2:myFunction}(${3:arg1}: ${4:Type1}, ${5:arg2}: ${6:Type2} = ${7:defaultValue}) -> ${1:Void} {
${8:// Your code here}
}

cotrFuncArgsNamed

Swift Function Named Args

func ${2:myFunction}(${3:arg1Name} ${3:arg1}: ${4:Type1}, ${5:arg2Name} ${5:arg2}: ${6:Type2}) -> ${1:Void} {
${7:// Your code here}
}

cotrComment

Swift Comment

// ${1:Your comment here}

cotrVar

Swift Create Variable

var ${1:myVar} = $2;

cotrPrint

Swift Print

print(${1:"Your message here"})

cotrForInLoop

Swift For…In Loop

for ${1:item} in ${2:iterable} {
${3:// Your code here}
}

cotrNotEqual

Swift Not Equal To

!=

cotrTypesNum

Swift Double Type

Double

cotrVarMap

Swift Create Dictionary Variable

var ${1:myDictionary} = [
${2:'key1'}: ${3:'value1'},
${4:'key2'}: ${5:'value2'}
];

cotrGenList

Swift Generate Array

var ${1:myArray} = Array(repeating: ${2:'item'}, count: ${3:5});

cotrClass

Swift Class

class ${1:MyClass} {
${2:// Your code here}
}

cotrOperatorsBool

Swift Boolean Operators

// Swift Boolean Operators
// Logical AND: &&
// Logical OR: ||
// Logical NOT: !
// Equality: ==
// Inequality: !=
// Greater than: >
// Less than: <
// Greater than or equal to: >=
// Less than or equal to: <=

cotrTypesString

Swift String Type

String

cotrFuncSyntax

Swift Function Syntax

// Swift Function Syntax
// Basic function: func functionName(parameters) -> ReturnType { ... }
// Function with arguments: func functionName(arg1: Type1, arg2: Type2, ...) -> ReturnType { ... }
// Function with named arguments: func functionName(arg1Name arg1: Type1, arg2Name arg2: Type2, ...) -> ReturnType { ... }

cotrTryCatch

Swift Try Catch

do {
${1:// Your code here}
} catch ${2:exception} {
${3:// Your code here}
}

cotrTypesNull

Swift Null Type

nil

cotrVarNumAlt

Swift Create Float Variable

var ${1:myFloat} = ${2:0.0}f;

cotrVarList

Swift Create List Variable

var ${1:myArray} = [${2:'item1'}, ${3:'item2'}, ${4:'item3'}];

cotrThrow

Swift Throw Exception

throw ${1:Error('Your message here')};

cotrTypeCompare

Swift Type Comparison

// Check if two variables have the same type:
if type(of: ${1:variable1}) == type(of: ${2:variable2}) {
// Your code here
}

cotrTypesNumAlt

Swift Float Type

Float

cotrNow

Swift Date Now

Date()

cotrVarTyped

Swift Create Typed Variable

var ${1:myVar}: ${2:Type} = $3

cotrVarStringMulti

Swift Create Multi-Line String Variable

var ${1:myString} = """
${2:Line 1}
${3:Line 2}
${4:Line 3}
""";

cotrVarStatic

Swift Static Variable

static ${2:var} ${3:myStaticVar} = ${4:value};
// Access the static variable
// MyClass.${3:myStaticVar}

cotrSwitch

Swift Switch Statement

switch ${1:variable} {
case ${2:value1}:
${3:// Your code here}
break
case ${4:value2}:
${5:// Your code here}
break
default:
${6:// Your code here}
}

cotrEnum

Swift Enum

enum ${1:MyEnum} {
${2:value1},
${3:value2},
// Add more values here
}

cotrTypesBool

Swift Boolean Type

Bool

cotrVarSyntax

Variable Declaration Syntax

// Swift Variable Declaration Syntax:
// - var: (Scope: Block or Global)
// - Declares a mutable variable.
// - Use for variables that need to be reassigned.
// - let: (Scope: Block or Global)
// - Declares an immutable variable.
// - Preferred for values that should remain constant.
// Note:
// - Use 'let' by default for values that won't change.
// - Use 'var' only when you need to reassign the variable.

cotrVarInt

Swift Create Integer Variable

var ${1:myInt} = ${2:0};

cotrFuncAnon

Swift Anonymous Function

{ (${1:parameters}) -> ${2:ReturnType} in
${3:// Your code here}
}

cotrInterpolate

Swift Interpolate String

"\(${1:variable})"

cotrConcat

Swift Concatenate Strings

"${1:string1}" + "${2:string2}"

cotrVarBool

Swift Create Boolean Variable

var ${1:myBool} = ${2:true};