Skip to content

Instantly share code, notes, and snippets.

View zhihuitang's full-sized avatar
😜

Zhihui Tang zhihuitang

😜
View GitHub Profile
@zhihuitang
zhihuitang / spmResolve.py
Last active December 25, 2022 13:00
This script is a workaround to resolve SPM versions.
#!/opt/homebrew/bin/python3
# Sometimes Xcode cannot resolve SPM(File -> Packages -> Resolve Package versions) if the dependency url is ssh
# This script is a workaround to resolve package versions.
# Usage:
# python spmResolve.py
# or
# python3 spmResolve.py
import os.path
import subprocess
struct AvatarView: View {
/// image
let image: String
/// size
let size: CGFloat
/// body
var body: some View {
@zhihuitang
zhihuitang / appdocdir.zsh
Created November 10, 2020 15:36 — forked from nickcheng/appdocdir.zsh
Open your app's Documents folder on iOS simulator.
function myappdocdir() {
devid=$(xcrun simctl list devices | grep Booted | sed -n 's/^.*\([A-F0-9]\{8\}-\([A-F0-9]\{4\}-\)\{3\}[A-F0-9]\{12\}\).*$/\1/p')
for folder in ~/Library/Developer/CoreSimulator/Devices/$devid/data/Containers/Data/Application/*; do
if [[ -a $folder/.com.apple.mobile_container_manager.metadata.plist ]]; then
if [[ 'com.apple.phone' = $(/usr/libexec/PlistBuddy -c 'Print :MCMMetadataIdentifier' $folder/.com.apple.mobile_container_manager.metadata.plist) ]]; then
echo $folder
break
fi
fi
done
//------------------------------------------------------------------------
// The SwiftUI Lab: Advanced SwiftUI Animations
// https://swiftui-lab.com/swiftui-animations-part1 (Animating Paths)
// https://swiftui-lab.com/swiftui-animations-part2 (GeometryEffect)
// https://swiftui-lab.com/swiftui-animations-part3 (AnimatableModifier)
//------------------------------------------------------------------------
import SwiftUI
struct ContentView: View {
// https://www.vadimbulavin.com/advanced-guide-to-userdefaults-in-swift/
// what is the difference between WWDC2019 https://devstreaming-cdn.apple.com/videos/wwdc/2019/402fd460n3p3w5c/402/402_whats_new_in_swift.pdf
// The marker protocol
protocol PropertyListValue {}
extension Data: PropertyListValue {}
extension String: PropertyListValue {}
extension Date: PropertyListValue {}
extension Bool: PropertyListValue {}
extension Int: PropertyListValue {}
# Git Fuzzy Checkout
#
# Add the following script to your ~/.gitconfig by run:
# git config --global alias.fuzzy '!f() { git branch -a | grep -m1 -e ${1}.*${2} | sed "s/remotes\/origin\///" | xargs git checkout; }; f'
#
# alias of .gitconfig looks:
[alias]
// https://stackoverflow.com/questions/33145005/swift-nsdateformatter-not-using-correct-locale-and-format/61094162#61094162
extension Date {
var timestamp: String {
let dataFormatter = DateFormatter()
dataFormatter.locale = Locale(identifier: "en_US_POSIX")
dataFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSS"
return String(format: "%@", dataFormatter.string(from: self))
}
}
@zhihuitang
zhihuitang / AsyncOperation
Last active March 31, 2020 16:52
AsyncOperation: Wrap Asynchronous Function in Operation For synchronous tasks, you can create an Operation subclass by overriding the main() method. AsyncOperation is a custom subclass of Operation that handles state changes automatically. Then, to
class AsyncOperation: Operation {
enum State: String {
case Ready, Executing, Finished
fileprivate var keyPath: String {
return "is" + rawValue
}
}
var state = State.Ready {
@zhihuitang
zhihuitang / Collection
Last active February 29, 2020 10:20
Returns the element at the specified index if it is within bounds, otherwise nil.
public extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
public extension CGSize {
public func rescale(_ scale: CGFloat) -> CGSize {
return applying(CGAffineTransform(scaleX: scale, y: scale))
}
}