Skip to content

Instantly share code, notes, and snippets.

View deathlezz's full-sized avatar
📱
Learning Swift 

DEATHLEZZ deathlezz

📱
Learning Swift 
View GitHub Profile
@ilyarmnzhdn
ilyarmnzhdn / gist:aa67838f392c532ede16861dfed93c42
Created November 1, 2017 09:28
Find the longest word in string (Swift 4)
func findLongestWord(word: String) -> String {
let wordArray = word.components(separatedBy: " ")
var maxLength = 0
for index in 0..<(wordArray.count) {
if wordArray[index].count > maxLength {
maxLength = wordArray[index].count
}
}
@deathlezz
deathlezz / Compare elements in one array.swift
Last active July 31, 2021 10:56
Compare elements in one array in Swift 5.
//
// Compare elements in one array
//
// Check if all elements in array are the same
let input = "aaaa"
let areSame = input.allSatisfy { $0 == input.last }
@deathlezz
deathlezz / Restrict certain characters.swift
Last active August 22, 2021 19:34
Restrict certain characters in Swift 5.
//
// Restrict certain characters
//
// Make certain characters impossible to use
//
import Foundation
let input = "abcd@e"
@deathlezz
deathlezz / Remove certain characters.swift
Last active July 31, 2021 10:56
Remove certain characters in Swift 5.
//
// Remove certain characters
//
let input = "1 234 567".filter {!$0.isWhitespace}
let input2 = "aAbBcCdD".filter {!$0.isLowercase}
let input3 = "!@A#$BC%DE^FG&HI".filter {!$0.isLetter}
@deathlezz
deathlezz / Convert Int array to String array.swift
Last active August 22, 2021 19:03
Convert Int array to String array and vice versa in Swift 5.
//
// Convert Int array to String array and vice versa
//
// Int array -> String array
let intArray = [1, 2, 3, 4, 5]
let stringArray = intArray.map { String($0) }
@deathlezz
deathlezz / Convert String array into String.swift
Last active March 25, 2022 13:36
Convert String array into String and vice versa in Swift 5.
//
// Convert String array into String and vice versa
//
// Also convert Int array into Int and vice versa
//
import Foundation
// String array -> String
let stringArray = ["Kevin", "Bob", "Steve"]
@deathlezz
deathlezz / Find random element in array.swift
Last active July 31, 2021 10:54
Find random element in array in Swift 5.
//
// Find random element in array
//
let array = "abcdefghijklmnopqrstuvwxyz"
let random = (0...3).compactMap { _ in array.randomElement() } // (0...3) - it means amount of characters
print(random) // ["f", "u", "y", "m"]
@deathlezz
deathlezz / Rounding numbers.swift
Created July 17, 2021 13:44
Rounding numbers in Swift 5.
//
// Rounding numbers
//
import Foundation
let number = 15.4444444445
let round = String(format: "%.2f", number) // 2f stands for two decimal places
@deathlezz
deathlezz / Error handling.swift
Last active July 31, 2021 10:54
Error handling in Swift 5.
//
// Error handling
//
// Create error
enum InputError: Error {
case EmptyName
}
// Create function
@deathlezz
deathlezz / Check the current date.swift
Last active July 23, 2021 10:58
Check the current date in Swift 5.
//
// Check the current date
//
import Foundation
// Create current date
var currentDate = Date()
// Create a DateFormatter object