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 / 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 {
// 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 / 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...
@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)
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 / 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()
@nhathm
nhathm / SwiftClosure-DeclareClosure.swift
Created April 7, 2017 08:01
Swift closure - Declare a closure
import Foundation
// Declare a variable to hold a closure
var add: (Int, Int) -> Int
// Assign a closure to a variable
add = { (a: Int, b: Int) -> Int in
return a + b
}
@nhathm
nhathm / SwiftClosure-ShorthandClosure.swift
Last active November 6, 2018 07:41
Swift closure - Shorthand Closure
// Declare a variable to hold a closure
var add: (Int, Int) -> Int
/** SHORTHAND SYNTAX **/
// Not need return keyword when only have single return statement
add = {(a: Int, b: Int) -> Int in
a + b
}
add(1, 1)
@nhathm
nhathm / SwiftClosure.swift
Created April 10, 2017 10:27
Swift Closure
//: Playground - noun: a place where people can play
// nhathm01247@gmail.com
import Foundation
/** DECLARE A CLOSURE **/
// Declare a variable to hold a closure
var add: (Int, Int) -> Int
@nhathm
nhathm / SwiftClosure-ClosureAsParameters.Swift
Created April 10, 2017 10:28
SwiftClosure-Closure and function as Parameters
//: Playground - noun: a place where people can play
// nhathm01247@gmail.com
import Foundation
// Requirement: write a function take Int, Int, Int and a function that sum first two Int params and then sub the third Int.
// Do it function way
// Declare a function
func add(_ a: Int, _ b: Int) -> Int {