explaingit

steipete/aspects

8,429Objective-CAudience · developerComplexity · 2/5Setup · easy

TLDR

Aspects is an Objective-C library for iOS and macOS that lets you attach custom code to run before, after, or instead of any existing method, ideal for testing and debugging, not production apps.

Mindmap

mindmap
  root((aspects))
    What it does
      Hook methods at runtime
      Before after or replace
      Class or instance hooks
    How it works
      Dynamic subclass
      Message forwarding
      Thread-safe tokens
    Recommended Uses
      Debug logging
      Unit test mocks
      Analytics hooks
    Caution
      Not for production
      Message forwarding conflicts
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Add debug logging to every view controller method during development without touching each class individually.

USE CASE 2

Write unit tests that verify a specific method was actually called, without subclassing or modifying the original class.

USE CASE 3

Attach analytics tracking to third-party library methods that you cannot subclass directly.

USE CASE 4

Intercept a method on a single object instance to temporarily change its behavior during a debugging session.

Tech stack

Objective-CSwift

Getting it running

Difficulty · easy Time to first run · 30min

Conflicts with other code that uses Objective-C message forwarding, the author recommends limiting use to tests and debug builds only.

No license information was mentioned in the explanation.

In plain English

Aspects is an Objective-C library that lets you attach custom code to existing methods in your iOS or macOS app, without modifying those methods directly. You can specify whether your extra code runs before the original method, after it, or in place of it. This approach is called aspect-oriented programming, a pattern for handling concerns that touch many parts of a codebase at once, such as logging every method call or checking permissions before certain actions. The library works by intercepting Objective-C messages at runtime. When you register a hook, Aspects creates a dynamic subclass under the hood, similar to how the Key-Value Observing system works in Apple frameworks. You can attach hooks to an entire class or to a specific object instance. Calls are thread-safe, and each hook returns a token you can use to remove it later. Practical uses documented in the README include adding debug logging to view controllers, simplifying analytics tracking, verifying in unit tests that a method was actually called, and attaching behavior to classes from third-party libraries that you cannot subclass directly. There is a significant caveat stated clearly by the author: as of the time of the last update, the author does not recommend using Aspects in production code. The underlying mechanism for intercepting messages can conflict with other code that relies on the same message forwarding system. The documented recommended use is for test mocks and quick debugging during development, not for shipping apps. The library is written in Objective-C and the README shows usage examples in both Objective-C and Swift.

Copy-paste prompts

Prompt 1
Using Aspects, show me how to log every time viewDidAppear is called on any UIViewController in my iOS app, printing the class name and a timestamp.
Prompt 2
Write an XCTest that uses Aspects to assert that saveData was called exactly once on a specific model object during a test.
Prompt 3
I want to track button taps in an analytics layer without modifying my view controllers. Show me how to use Aspects to hook into sendAction:to:from:forEvent: on UIControl.
Prompt 4
Explain the difference between hooking a class versus hooking an instance with Aspects, and when each approach is safer.
Prompt 5
Show me how to remove an Aspects hook using the token it returns, so the hook only applies during one test method.
Open on GitHub → Explain another repo

← steipete on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.