Skip to content

Instantly share code, notes, and snippets.

View ytyubox's full-sized avatar
😎
TooFastToSlow

Tsungyu Yu ytyubox

😎
TooFastToSlow
View GitHub Profile
import Combine
func aPublisher() -> some Publisher {
Just(1)
}
protocol IntNeverPublisher: Publisher where Output == Int, Failure == Never { }
extension Just: IntNeverPublisher where Output == Int {}
func aIntNeverPublisher() -> some IntNeverPublisher {
import Combine
final class DeviceOrientation: ObservableObject {
@Published private(set) var orientation = UIDevice.current.orientation
private var listener: AnyCancellable?
init() {
orientation = UIDevice.current.orientation
print(orientation.isPortrait)
listener = NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)
.compactMap { ($0.object as? UIDevice)?.orientation }
.compactMap{

Before

  Text("Hello")
  Text("world")

After apply <Leader>ai on both line selected by V

👇 cursor here
@ytyubox
ytyubox / README.md
Last active January 13, 2021 15:38

XCTAssertEqual + Tuple Keypath

GYB to Swift code

//bash
gyb XCTAssertEqual+TupleKeypath.swift.gyb --line-directive "" -o XCTAssertEqual+TupleKeypath.swift

Usage

@ytyubox
ytyubox / AutoUpdateColor.swift
Last active January 18, 2021 06:49
Dark mode color without xcassets
import UIKit
class AutoUpdateColor: UIColor {
convenience init(lightColor: UIColor, darkColor: UIColor) {
if #available(iOS 13.0, *) {
self.init {
traitCollection in
traitCollection.userInterfaceStyle == .dark
? darkColor
: lightColor
}
@ytyubox
ytyubox / Create-xcframework-from-swiftpackage.md
Last active November 14, 2022 02:57
Solution of make XCFramework from Swift Package

before execute, gen xcode project by

swift package generate-xcodeproj

and configure the build setting as you need

then excute the script with two argument

  1. the name of .xcodeproj
  2. the sheme to gen (may be different due to auto generate xcode project)
@ytyubox
ytyubox / MustInjected.swift
Created December 19, 2020 11:43
@MustInjected replace var:!
@propertyWrapper struct MustInjected<Value> {
private var _value: Value! = nil
#if DEBUG
let line: UInt
let file: StaticString
#endif
@available(*, unavailable)
init(wrappedValue: @autoclosure @escaping () -> Value) {
self._value = nil
line = #line
@ytyubox
ytyubox / XCTGetIndex.swift
Created December 17, 2020 09:46
XCTGetIndex
import XCTest
enum XCTGetIndexError: Error {
case outofRange
}
func XCTGetIndex<C, T>(
_ array: C, with index: C.Index,
_ message: @autoclosure () -> String = "Index out of range",
file: StaticString = #file,
line: UInt = #line) throws -> T
where C: RandomAccessCollection, C.Element == T
struct HardCodedString: ExpressibleByStringLiteral, CustomStringConvertible {
private var backing: String
@available(*, deprecated, message: "This method was used by compiler, you should not call directly")
init(stringLiteral value: String) {
self.backing = value
}
var description: String {backing}
}
import XCTest
final class TwitterMathProbramTests: XCTestCase {
func testBFS() throws {
var q = (1 ..< 10).map(\.description)
var res = [String]()
var cur = ""
while true {
if q.isEmpty { break }
cur = q.removeFirst()
if cur.count == 10 {