Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Learning SwiftUI

Yi antonio081014

💭
Learning SwiftUI
View GitHub Profile
View ReadFileWithAsyncAwait.swift
class AsyncFileReading {
func asyncReadFile(_ url: URL) async throws -> Data {
let (data, _) = try await URLSession.shared.data(from: url)
return data
}
static func testout() {
let instance = AsyncFileReading()
let filename = "file_to_be_read.txt"
let url = Bundle.main.bundleURL.appending(component: filename)
View ReadFileWithClosure.swift
func load(from url: URL, completion: @escaping (Result<Data, Error>) -> Void) {
DispatchQueue.global().async {
do {
let data = try Data(contentsOf: url)
DispatchQueue.main.async {
completion(.success(data))
}
} catch {
DispatchQueue.main.async {
completion(.failure(error))
View ReadFileWithCombine.swift
import Foundation
import Combine
class FileSubscription<S: Subscriber>: Subscription
where S.Input == Data, S.Failure == Error {
// fileURL is the url of the file to read
private let fileURL: URL
private var subscriber: S?
@antonio081014
antonio081014 / UIView+Shimmering.swift
Created Mar 1, 2022
Shimmering Effect for UIView
View UIView+Shimmering.swift
import UIKit
extension UIView {
public var isShimmering: Bool {
get {
return layer.mask?.animation(forKey: shimmerAnimationKey) != nil
}
set {
if newValue {
startShimmering()
View CoreDataUnitTest_entity.swift
func test_imageEntity_properties() throws {
// Assert non nil value, and unwrap it.
let entity = try XCTUnwrap(
CoreDataStore.model?.entitiesByName["Name of Entity"]
)
entity.verify(attribute: "id", hasType: .UUIDAttributeType, isOptional: false)
entity.verify(attribute: "description", hasType: .stringAttributeType, isOptional: true)
entity.verify(attribute: "url", hasType: .URIAttributeType, isOptional: false)
}
View XCTestCase+MemoryLeakTracking.swift
import XCTest
extension XCTestCase {
func trackForMemoryLeaks(_ instance: AnyObject, file: StaticString = #file, line: UInt = #line) {
addTeardownBlock { [weak instance] in
XCTAssertNil(instance, "Instance should have been deallocated. Potential memory leak.", file: file, line: line)
}
}
}
// Here is how to call this function.
View ScrollViewController.swift
//
// ScrollViewController.swift
//
// Created by Antonio081014 on 3/6/18.
// Copyright © 2018 sample.com. All rights reserved.
//
import UIKit
@antonio081014
antonio081014 / Country.swift
Last active Feb 28, 2017 — forked from kharrison/Country.swift
Swift Hash Functions
View Country.swift
import Foundation
struct Country {
let name: String
let capital: String
var visited: Bool
}
extension Country: Equatable {
static func == (lhs: Country, rhs: Country) -> Bool {
@antonio081014
antonio081014 / Weekday.md
Last active Jan 31, 2017
This is a simple OptionSet Demo in swift.
View Weekday.md

This is a simple OptionSet Demo written in swift.

import UIKit

struct Weekday: OptionSet, CustomStringConvertible {
    let rawValue: Int
    
    /// Monday
    static let Monday = Weekday(rawValue: 1 << 2)
View SwiftProperty.swift
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground" {
//"Hello, playground"
willSet {
print("willSet \(str) : \(newValue)")
// "willSet Hello, playground : Hello, Antonio.\n"
str = "Hi"