Skip to content

Instantly share code, notes, and snippets.

Avatar

Lee Kah Seng LeeKahSeng

View GitHub Profile
View List-Custom-Cell-IB-02.swift
struct SFSymbolNameContentConfiguration: UIContentConfiguration, Hashable {
var name: String?
func makeContentView() -> UIView & UIContentView {
// Initialize an instance of SFSymbolNameContentView
return SFSymbolNameContentView(configuration: self)
}
func updated(for state: UIConfigurationState) -> Self {
@LeeKahSeng
LeeKahSeng / Google-Sign-In-Integration-11.swift
Created Jun 8, 2020
View controller implementation for Google sign-in integration
View Google-Sign-In-Integration-11.swift
import UIKit
import GoogleSignIn
class ViewController: UIViewController {
var signInButton: UIButton!
var signOutButton: UIButton!
var greetingLabel: UILabel!
override func viewDidLoad() {
@LeeKahSeng
LeeKahSeng / Google-Sign-In-Integration-04.swift
Created Jun 8, 2020
Full Implementation of AppDelegate.swift for Google sign-in integration
View Google-Sign-In-Integration-04.swift
import UIKit
import GoogleSignIn
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Initialize Google sign-in
@LeeKahSeng
LeeKahSeng / Decode-Dynamic-Keys-JSON-Final.swift
Last active Dec 27, 2020
Decode and Flatten JSON with Dynamic Keys Using Swift Decodable (https://swiftsenpai.com/swift/decode-dynamic-keys-json/)
View Decode-Dynamic-Keys-JSON-Final.swift
let jsonString = """
{
"S001": {
"firstName": "Tony",
"lastName": "Stark"
},
"S002": {
"firstName": "Peter",
"lastName": "Parker"
},
View Property-Wrapper-AppData-Full.swift
struct User: Codable {
var firstName: String
var lastName: String
var lastLogin: Date?
}
@propertyWrapper
struct Storage<T: Codable> {
private let key: String
private let defaultValue: T
View Property-Wrapper-AppData-2.swift
struct AppData {
@Storage(key: "enable_auto_login_key", defaultValue: false)
static var enableAutoLogin: Bool
@Storage(key: "username_key", defaultValue: "")
static var username: String
}
View Property-Wrapper-AppData-1.swift
struct AppData {
private static let enableAutoLoginKey = "enable_auto_login_key"
private static let usernameKey = "username_key"
static var enableAutoLogin: Bool {
get {
// Read from UserDefaults
return UserDefaults.standard.bool(forKey:enableAutoLoginKey)
}
View Optional-Operator-4.swift
class Boy {
// Failable initialisation
init?(age: Int) {
// Age cannot be less than 0
if age < 0 {
return nil
}
}
}
View Optional-Operator-3.swift
let str: String? = "test"
// Optional binding using 'if'
if let value = str {
print(value)
}
// Optional binding using guard
guard let value = str else {
View Optional-Operator-2.swift
// Create another bird array that accept nil value
let otherBirds: [Bird?] = [chicken, nil]
let chicken4 = otherBirds[0] as? Chicken? // Cast successful: chicken4 type is Chicken??
let chicken5 = otherBirds[0] as! Chicken? // Cast successful: chicken5 type is Chicken?
let chicken6 = otherBirds[1] as? Chicken? // Cast failed: Trigger runtime error
let chicken7 = otherBirds[1] as! Chicken? // Cast failed: Trigger runtime error
let chicken8 = otherBirds[0] as? Chicken! // chicken8 type is Chicken?? | Warning: Using '!' here is deprecated and will be removed in a future release
let chicken9 = otherBirds[0] as! Chicken! // chicken9 type is Chicken? | Warning: Using '!' here is deprecated and will be removed in a future release
You can’t perform that action at this time.