Skip to content

Instantly share code, notes, and snippets.

View antonio081014's full-sized avatar
💭
Learning SwiftUI

Yi antonio081014

💭
Learning SwiftUI
View GitHub Profile
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)
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))
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 March 1, 2022 17:31
Shimmering Effect for UIView
import UIKit
extension UIView {
public var isShimmering: Bool {
get {
return layer.mask?.animation(forKey: shimmerAnimationKey) != nil
}
set {
if newValue {
startShimmering()
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)
}
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.
//
// ScrollViewController.swift
//
// Created by Antonio081014 on 3/6/18.
// Copyright © 2018 sample.com. All rights reserved.
//
import UIKit
@antonio081014
antonio081014 / Country.swift
Last active February 28, 2017 18:31 — forked from kharrison/Country.swift
Swift Hash Functions
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 January 31, 2017 17:25
This is a simple OptionSet Demo in swift.

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)
//: 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"