Skip to content

Instantly share code, notes, and snippets.

View nhathm's full-sized avatar
🌴
On vacation

Hoàng Minh Nhật nhathm

🌴
On vacation
View GitHub Profile
@nhathm
nhathm / SwiftFunction.swift
Last active November 26, 2019 06:55
Swift Function
//: Playground - noun: a place where people can play
// nhathm01247@gmail.com
import UIKit
// Basic function declare
func sayHi() {
print("Hi")
}
sayHi()
struct Student {
let name: String?
let age: Int?
let gender: String?
}
let student = Student(name: "Rio", age: 27, gender: "Male")
func validateStudentInfo (_ student: Student) -> String {
// We do NOT need to using student name, just want to check if
@nhathm
nhathm / GuardMultiCondition.swift
Created March 20, 2017 18:22
Guard multi condition
let name : String? = "Swift"
let version : Int = 3
let owner : String = "Apple"
func checkMultiCondition(name : String?, version : Int, owner : String) {
guard let name = name, version > 2, owner == "Apple" else {
print("Input parameters not match!!!")
return
}
print(name)
@nhathm
nhathm / OptinalsBindingVsGuard.swift
Created March 20, 2017 17:52
Example about Guard Statement
let name : String? = "Swift"
let nickName : String? = nil
// Example: Optional binding
// Note: underscore "_" using to ignore name of parameter when call function
// Example calling function below
func sayHiWithOptionalBinding(_ name: String?) {
// Using optional binding to check if object is NOT nil
if let checkName = name {
// If not nil, do some logic...
// Declare non-optionals variable
var notToday : String = "Tomorrow"
// Can't set non-optionals variable to nil
notToday = nil // Compile error
// Declare optionals variables
var dafug : String? = "Dafug"
var steps : Int? = 10
@nhathm
nhathm / Swfitnote.swift
Created December 4, 2016 08:32
Swift note about AVFoundation
// Get priview image for video
func previewImageFromVideo(_ url: URL) -> UIImage? {
let asset = AVAsset(url: url)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
var time = asset.duration
time.value = min(time.value, 2)
do {