gitmyhub

Aspects

Objective-C ★ 8.4k updated 6mo ago ▣ archived

Delightful, simple library for aspect oriented programming in Objective-C and Swift.

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.

Objective-CSwiftsetup: easycomplexity 2/5

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.

Where it fits