• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 1 year ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ”— Swift Macro for allowing variable declarations even in class extensions

AssociatedObject

Swift Macro for allowing variable declarations even in class extensions. It is implemented by wrapping objc_getAssociatedObject/objc_setAssociatedObject.

Github issues Github forks Github stars Github top language

Installation

SPM

.package(url: "https://github.com/p-x9/AssociatedObject", from: "0.8.0")

CocoaPods

Add below to your Podfile.

pod 'AssociatedObject', git: 'https://github.com/p-x9/AssociatedObject', tag: '0.8.0'

After pod install, you can use this Macro in your project.

Additionally, if you encounter build error like Expansion of macro 'AssociatedObject' did not produce a non-observing accessor. You should check your project setting Build Settings-OTHER_SWIFT_FLAGS.

There should be additional flags like so. Alt text

If not, you can add these two lines by yourself.

-load-plugin-executable
${PODS_ROOT}/AssociatedObject/Binary/AssociatedObjectPlugin#AssociatedObjectPlugin

Usage

For example, you can add a new stored property to UIViewController by declaring the following

import AssociatedObject

extension UIViewController {
    @AssociatedObject(.retain(nonatomic))
    var text = "text"

    /* OR */

    @AssociatedObject(.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    var text = "text"

    static var customKey = ""
    @AssociatedObject(.OBJC_ASSOCIATION_RETAIN_NONATOMIC, key: customKey)
    var somevar = "text"
}

Declared properties can be used as follows

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        print(text) // => "text"

        text = "hello"
        print(text) // => "hello"
    }

}

willSet/didSet

Properties defined using @AssociatedObject can implement willSet and didSet. In swift, it is not possible to implement willSet and didSet at the same time as setter, so they are expanded as follows.

@AssociatedObject(.copy(nonatomic))
public var hello: String = "ใ“ใ‚“ใซใกใฏ" {
    didSet {
        print("didSet")
    }
    willSet {
        print("willSet: \(newValue)")
    }
}

// โ†“โ†“โ†“ expand to ... โ†“โ†“โ†“
public var hello: String = "ใ“ใ‚“ใซใกใฏ" {
    get {
        objc_getAssociatedObject(
            self,
            &Self.__associated_helloKey
        ) as? String
        ?? "ใ“ใ‚“ใซใกใฏ"
    }

    set {
        let willSet: (String) -> Void = { [self] newValue in
            print("willSet: \(newValue)")
        }
        willSet(newValue)

        let oldValue = hello

        objc_setAssociatedObject(
            self,
            &Self.__associated_helloKey,
            newValue,
            .copy(nonatomic)
        )

        let didSet: (String) -> Void = { [self] oldValue in
            print("didSet")
        }
        didSet(oldValue)
    }
}

License

AssociatedObject is released under the MIT License. See LICENSE

More Repositories

1

iLLDB

๐Ÿ› ๏ธ LLDB Extension for iOS and macOS App Development
Python
117
star
2

ObfuscateMacro

๐Ÿ•ต๏ธ Swift macros for obfuscation
Swift
102
star
3

MachOKit

๐Ÿ”ฌ A Swift library for parsing mach-o files to obtain various information.
Swift
73
star
4

TouchTracker

๐Ÿ‘†Show a mark at the touched point on the View of SwiftUI and UIKit.
Swift
37
star
5

CocoaUI

โ˜•๏ธ Obtain and customize UIKit/Cocoa objects from SwiftUI components.
Swift
35
star
6

swift-hook

โš“๏ธ A Swift Library for hooking swift methods and functions.
Swift
33
star
7

AppContainer

๐Ÿงณ Library that makes it easy to create multiple environments within a single app. You can switch environments without deleting the application.
Swift
30
star
8

swift-system-sound

๐ŸŽถ A Swift Library that makes SystemSound playback easy.
Swift
30
star
9

RunScriptPlugin

๐ŸƒSwiftPackage Plugin for executing arbitrary ShellScript.
Swift
30
star
10

AliasMacro

๐ŸŽญ A Swift macros for defining aliases for types, functions, variables, etc.
Swift
30
star
11

swift-property-tracer

๐Ÿ“ Library for tracing access to properties.
Swift
28
star
12

SwiftUIColor

๐ŸŽจ Make system color defined in UIColor/NSColor available to SwiftUI Color.
Swift
27
star
13

SwiftLintXcodePlugin

SwiftLint Plugin for Xcode.
Objective-C
26
star
14

ScreenCapture

๐Ÿ“ฑLibrary for recording window and windowScene.
Swift
17
star
15

EditValueView

Library that makes easy to display property edit screens for SwiftUI.
Swift
16
star
16

flipper-plugin-control-ui

Flipper plug-in that can send TouchEvent and Keyboard events from PC to iPhone.(Desktop)
TypeScript
16
star
17

pock-headline-news-widget

Headline News Widget for Pock.You can display the articles fetched by rss.
Swift
15
star
18

ShowTouch

A jailbreak tweak that displays a mark at the touched location on the Window.
Swift
15
star
19

ELFKit

๐Ÿ”ฌ A Swift library for parsing ELF files to obtain various information.
Swift
14
star
20

swift-weak-self-check

๐Ÿงน A CLI tool for `[weak self]` detection by `swift-syntax` and `index-store`
Swift
13
star
21

NotchCorners

You can get the notch's corner radius and size.(iOS14)
Swift
12
star
22

HeadLineNews-ObjC

Headline News View Example App for iOS. You can display the articles fetched by rss.
Objective-C
12
star
23

pock-timer-widget

A timer widget plugin for Pock
Swift
11
star
24

swiftui-reflection-view

SwiftUI View to display property information based on Swift's reflection API for any type of value.
Swift
11
star
25

SDCALayer

Server-Driven CALayer.
Swift
10
star
26

swift-mobile-gestalt

๐Ÿ“ฒ `MobileGestalt` wrapper for Swift
Swift
10
star
27

swift-fishhook

๐ŸชRe-implementation of fishhook with Swift using MachOKit
Swift
10
star
28

swift-anti-fishhook

A Swift library to deactivate fishhook. (Anti-FishHook)
Swift
9
star
29

swift-object-association

๐Ÿ”— A swift library for associating objects as properties with reference type objects.
Swift
9
star
30

swift-function-paths

๐Ÿ›ฃ๏ธ functionPath is function version of keyPath.
Swift
9
star
31

UIKit-Japanese-Typealias

UIKitใ‚’ๆ—ฅๆœฌ่ชžใงไฝฟใ„ใŸใ„ใ‚“ใ‚„
Swift
8
star
32

Modify

โ›Swift syntax sugar for modifying properties
Swift
7
star
33

BTTPluginHeadLineNews

Headline News Widget for Better Touch Tool. You can display the articles fetched by rss.
Swift
7
star
34

swift-sysctl

๐Ÿ–ฅ๏ธ A Swift wrapper for sysctl.
Swift
7
star
35

ScreenshotResizer

๐Ÿ“ฑYou can change the default quality of your screenshots. (Tweak for Jailbroken iOS) Set the quality from the Preferences App.
Objective-C
6
star
36

pock-piano-widget

Piano Widget for Pock.
Swift
6
star
37

swift-rectangle-selector

A UI component for selecting rectangular areas.
Swift
6
star
38

swift-objc-dump

A Swift Library for dumping objective-c class/protocol/method/property/ivar informations
Swift
6
star
39

swift-literal-type-inference

๐Ÿค” swift-syntax extension to infer types from expressions based on literal information
Swift
6
star
40

ScreenshotResizer-Swift

You can change the default quality of your screenshots. (Tweak for Jailbroken iOS) Set the quality from the Preferences App.
Swift
5
star
41

CCAnimator-Swift

Tweak which allows customizing the UI of the control center and adding animations (Tested on iOS 13-14)
Swift
5
star
42

GamingWindow

iPhone's border animation
Swift
5
star
43

swift-optimization

โš™๏ธ A Swift library to set optimization levels for members on a per-type basis.
Swift
5
star
44

FlipperControlUIPlugin-iOS

Flipper plug-in that can send TouchEvent and Keyboard events from PC to iPhone.(iOS client)
Swift
4
star
45

DistributedNotificationCenter

Make `DistributedNotificationCenter` available on iOS.
Swift
4
star
46

KeyPathValue

๐Ÿ”‘ Structure for assigning values using keypath
Swift
4
star
47

swift-cpu-info

A Swift library for getting CPU informations
Swift
4
star
48

ForceUITableViewStyle

Tweak that can force substituting UITableView's Style
Swift
4
star
49

DeclarativeCocoa

๐Ÿšƒ Declarative UIKit/Cocoa. Allow defining properties in the method chain.
Swift
4
star
50

MovieWriter

๐ŸŽฅ Simple movie writing library for Swift
Swift
3
star
51

Foundation-Japanese-Typealias

Foundationใ‚’ๆ—ฅๆœฌ่ชžใงไฝฟใ„ใŸใ„ใ‚“ใ‚„
Swift
3
star
52

MoreViewBuilder

Extension to increase the number of views available in ViewBuilder
Swift
3
star
53

SCFNotification

๐Ÿ“ฌ Swift wrapper for `CFNotificationCenter`
Swift
3
star
54

DeclarativeCoreAnimation

๐Ÿšƒ Declarative CoreAnimation. Allow defining properties in the method chain.
Swift
3
star
55

pock-plugin-template

Plugin templete for pock
Swift
3
star
56

swift-magic-mirror

Implementation of Mirror not affected by CustomReflectable
Swift
3
star
57

iOSScreenRecordExample

Collection of screen recording methods on iOS
Swift
2
star
58

FoundationModifier

๐Ÿšƒ Allows `Foundation` classes to be write by method chains.
Swift
2
star
59

UISwitchAnimator

UISwitch animation app
Swift
2
star
60

HeadLineNewsWindow

jailbreak tweak
Objective-C
2
star
61

SwiftExtensions

Swift
2
star
62

GamingMac

Make the bezel of your Mac glow in rainbow colors.
Swift
2
star
63

p-x9.github.io

HTML
2
star
64

InvertAudioPhase

invert audio phase playground
Swift
1
star
65

PythonForTheos

Use Python code in jailbroken tweaks
C
1
star
66

CompetitiveProgramming-swift

1
star
67

swift-playgrounds

My Swift Playground Collection
Swift
1
star
68

UnloadTweak

1
star
69

MajorTableViewCells

ใ‚ˆใไฝฟใ†TableVIewใฎCellใ‚’้›†ใ‚ใพใ™ใ€‚
Swift
1
star
70

VideoClip

Swift
1
star
71

RainbowBorderAnimation

Rainbow border animation in swift using CoreAnimation
Swift
1
star
72

IndirectlyCodable

indirectly `Codable` protocol
Swift
1
star
73

swiftlint-rule-base

Base file of swiftlint configuration (.swiftlint.yml) per SwiftLint version
Python
1
star
74

swift-method-chainable

Generates chainable method code from Swift methods.
Swift
1
star