This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
// nhathm01247@gmail.com | |
import UIKit | |
// Basic function declare | |
func sayHi() { | |
print("Hi") | |
} | |
sayHi() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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 { |
OlderNewer