Skip to content

Instantly share code, notes, and snippets.

View lexrus's full-sized avatar
🏠
Working from home

Lex Tang lexrus

🏠
Working from home
View GitHub Profile
@0x1306a94
0x1306a94 / swift_macro_not_use_spm.md
Last active March 28, 2024 12:35
Swift Macro does not use SPM integration

Swift Macro 不使用 Swift Package Manager如何集成

  • Swift Macro分为两部分,一部分为宏的具体实现(编译器插件),一部分为Macro Lib用于导出宏定义以及所需要的附属代码,这里说的不使用SPM指的是编译器插件这部分
  • 仔细观察Xcode使用SPM集成Macro时,在编译命令中通过-load-plugin-executable参数指定了对应宏实现的插件可执行文件路径
  • 同时在swift源码中也找到了相关参数TypeCheckMacros.cpp TypeCheckMacros.cpp
  • swift源码中可得知,宏实现插件即可以是动态库(dylib)也可以是可执行文件
  • 系统内置的宏实现是通过 -plugin-path <dylib path> 加载
  • 内置的宏实现动态库在Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/Lib/swift/host/plugins
具体步骤
  • 编写宏实现代码,这一步可以通过SPM也可以直接通过Xcode创建命令行程序项目,完成宏实现代码编写
@fatbobman
fatbobman / publishedObject.swift
Last active May 11, 2022 10:03
Similar to the @published implementation. Reference type support
@propertyWrapper
public struct PublishedObject<Value: ObservableObject> { // wrappedValue 要求符合 ObservableObject
public var wrappedValue: Value
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
}
public static subscript<OuterSelf: ObservableObject>(
_enclosingInstance observed: OuterSelf,

From

var fib_6 = {
  v: Int -> Int  in
  if v <= 1  { return v }
  return ???(v - 1) + ???(v - 2)
}(6)

to

@msepcot
msepcot / HealthKitPRSearch.swift
Created March 3, 2021 01:07
Search HealthKit data for running PRs.
//
// ViewController.swift
// WatchAndLearn
//
// Created by Michael Sepcot on 9/18/19.
// Copyright © 2019 Michael Sepcot. All rights reserved.
//
import UIKit
import HealthKit
@importRyan
importRyan / whenHovered.md
Last active April 6, 2024 06:54
Reliable SwiftUI mouse hover

Reliable mouseEnter/Exit for SwiftUI

Kapture 2021-03-01 at 14 43 39

On Mac, SwiftUI's .onHover closure is not always called on mouse exit, particularly with high cursor velocity. A grid of targets or with finer target shapes will often have multiple targets falsely active after the mouse has moved on.

It is easy to run back to AppKit's safety. Below is a SwiftUI-like modifier for reliable mouse-tracking. You can easily adapt it for other mouse tracking needs.

import SwiftUI
@jordansinger
jordansinger / macOS.swift
Last active February 14, 2024 03:41
macOS SwiftUI Playgrounds code
import SwiftUI
import PlaygroundSupport
struct Desktop: View {
var body: some View {
ZStack {
// Image(uiImage: #imageLiteral(resourceName: "IMG_6281.JPG"))
Color(UIColor.systemBlue)
macOS()
}
@ethanhuang13
ethanhuang13 / FacebookAuth.swift
Last active March 28, 2024 08:24
FacebookAuth is for iOS app developers who need to support Facebook login but don't want to use the official SDK
//
// FacebookAuth.swift
// GitHub: ethanhuang13
// Twitter: @ethanhuang13
import AuthenticationServices
import SafariServices
/*
Updated:
@b3ll
b3ll / BlurryVibrantView.swift
Created November 10, 2019 07:15
iOS SwiftUI Blurry Vibrant Container View
//
// BlurryVibrantView.swift
//
//
// Created by Adam Bell on 11/9/19.
// Copyright © 2019 Adam Bell. All rights reserved.
//
import SwiftUI
import UIKit
@steipete
steipete / HowToUseURLByResolvingBookmarkDataOnMacCatalyst.md
Last active October 3, 2022 16:32
Using URLByResolvingBookmarkData on Mac Catalyst: Access sandboxed URLs after an app restart.

Here's what needs to be done in order to use security scoped bookmarks on Mac Catalyst:

  1. You need an entitlement: "com.apple.security.files.bookmarks.app-scope" needs to be se to 1.

  2. Pass both NSURLBookmarkCreationWithSecurityScope and NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess when creating the bookmark.

Note: The headers mark these API as unavailable for iOS, and this indeed does only work on Mac and not iOS. However, Mac Catalyst really is a Mac app, so using these values is fine. In order to allow compilation, use following:

@unnamedd
unnamedd / MacEditorTextView.swift
Last active April 16, 2024 10:18
[SwiftUI] MacEditorTextView - A simple and small NSTextView wrapped by SwiftUI.
/**
* MacEditorTextView
* Copyright (c) Thiago Holanda 2020-2021
* https://twitter.com/tholanda
*
* MIT license
*/
import Combine
import SwiftUI