Skip to content

Instantly share code, notes, and snippets.

View SwiftBeta's full-sized avatar
💻

SwiftBeta SwiftBeta

💻
View GitHub Profile
@SwiftBeta
SwiftBeta / quiz_question_03_03_20232.swift
Created March 3, 2023 20:01
Snippet code SwiftBeta Quiz Questions
func divide(a: Int, b: Int?) -> Int? {
guard let divisor = b, divisor != 0 else {
return nil
}
return a / divisor
}
if let result = divide(a: 10, b: 2) {
print(result)
}
@SwiftBeta
SwiftBeta / quiz_question_05_03_20230.swift
Created March 5, 2023 13:36
Snippet code SwiftBeta Quiz Questions
let session = URLSession.shared
let url = "https://www.example.com"
let task = session.dataTask(with:URL(string: url)!){ data, response, error in
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
}
}
task.resume()
@SwiftBeta
SwiftBeta / quiz_question_05_03_20230.swift
Created March 5, 2023 13:38
Snippet code SwiftBeta Quiz Questions
let number = 5
switch number {
case 1:
print("The number is 1")
case 2:
print("The number is 2")
default:
print("The number is not 1 or 2")
}
@SwiftBeta
SwiftBeta / quiz_question_05_03_20230.swift
Created March 5, 2023 13:50
Snippet code SwiftBeta Quiz Questions
let foo = 10
var bar = 20
bar = 30
foo = 40
// Error: Cannot assign to 'let' value 'foo'
@SwiftBeta
SwiftBeta / quiz_question_05_03_20231.swift
Created March 5, 2023 13:50
Snippet code SwiftBeta Quiz Questions
extension View {
func myCustomModifier() -> some View {
self
.font(.headline)
.foregroundColor(.red)
}
}
@SwiftBeta
SwiftBeta / quiz_question_05_03_20230.swift
Created March 5, 2023 16:01
Snippet code SwiftBeta Quiz Questions
let a = true
let b = false
let result = a && b
@SwiftBeta
SwiftBeta / quiz_question_05_03_20233.swift
Created March 5, 2023 16:01
Snippet code SwiftBeta Quiz Questions
let url = URL(string: "https://www.example.com")
let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
guard let data = data else { return }
print(String(data: data, encoding: .utf8)!)
}
task.resume()
@SwiftBeta
SwiftBeta / quiz_question_05_03_20237.swift
Created March 5, 2023 16:41
Snippet code SwiftBeta Quiz Questions
class Person {
lazy var name : String = "John"
}
@SwiftBeta
SwiftBeta / quiz_question_12_03_20237.swift
Created March 12, 2023 16:42
Snippet code SwiftBeta Quiz Questions
class CustomObject {
var id: Int
var value: String
init(id: Int, value: String) {
self.id = id
self.value = value
}
}
let objects = [CustomObject(id: 1, value: "c"), CustomObject(id: 3, value: "a"), CustomObject(id: 2, value: "b")]
let sortedObjects = objects.sorted { $0.id < $1.id }
@SwiftBeta
SwiftBeta / quiz_question_19_03_20237.swift
Created March 19, 2023 20:51
Snippet code SwiftBeta Quiz Questions
class MyClass {
private var myProperty: Int
init(myProperty: Int) {
self.myProperty = myProperty
}
}