Latest 1.0.2
License MIT
Platforms osx 10.10, ios 7.0, tvos 9.0, watchos 2.0, requires ARC



  • Provides lazy loading of property and object-oriented property hook by objc runtime.Perfect closed loop, can be added or unloaded.More powerful than lazy loading of macro definitions.
  • Key words : iOS lazy loading macOS lazy loading property hook iOS懒加载 属性钩子 class_removeMethods runtimelock
  • 中文文档


  • Drag directory AutoPropertyCocoa into project or use cocoapods

    #import "AutoPropertyCocoa.h"

    Main target

  • (id)lazyloadProperty{

    if(_lazyloadProperty == nil){

    _lazyloadProperty = [XClass ...];

    return _lazyloadProperty;


[instance apc_lazyLoadForProperty:@lazyloadProperty usingBlock:^id(id_apc_t instance){

return [XClass ...];


## Examples
#### Lazy loading of instance.Low coupling, no type pollution, recommended! 

APCLazyload(instance, @propertyA, @propertyB, ...);

[instance apc_lazyLoadForProperty:@property usingBlock:^id(id_apc_t instance){

    return ...;

[instance apc_lazyLoadForProperty:@arrayProperty selector:@selector(array)];

Unbind lazy loading of instance is supported.

APCUnbindLazyload(instance, @propertyA, @propertyB, ...);

[instance apc_unbindLazyLoadForProperty:@property];

Lazy loading of class.

APCClassLazyload(Class, propertyA, propertyB, ...);

APCClassUnbindLazyload(Class, propertyA, propertyB, ...);

Front-trigger hook.Called before a property is called.

[anyone apc_frontOfPropertyGetter:key bindWithBlock:^(id_apc_t instance, id value) {

    ///Before getter of property called.

Post-trigger hook.Called after a property is called.

[anyone apc_backOfPropertySetter:key bindWithBlock:^(id_apc_t instance, id value) {

    ///After setter of property called.

Condition-trigger hook.Called when the user condition is true.

[anyone apc_propertySetter:key bindUserCondition:^BOOL(id_apc_t instance, id value) {

    ///Your condition when setter called...
} withBlock:^(id_apc_t instance, id value) {

    ///If your condition has been triggered.


Basic-value type

The currently supported structure types are: XReact, XPoint, XSize, XEdgeinsets, NSRange.

  • Lazy-load is invalid on the base-value type property of the class hook.But is valid on the instance, The lazy loading method is triggered when the instance’s underlying value type property is first accessed.

User super method.

  • APCUserEnvironment supports user call user method in super class.
  • id_apc_t marks the id object as supporting APCUserEnvironment.

    [Person apc_lazyLoadForProperty:key  usingBlock:^id (id_apc_t instance) {
    return @"Person.gettersetterobj";

[Man apc_lazyLoadForProperty:key usingBlock:^id (id_apc_t instance) {
//Call above ↑
return APCSuperPerformedAsId(instance);

[Superman apc_lazyLoadForProperty:key usingBlock:^id (id_apc_t instance) {
//Call above ↑
return APCSuperPerformedAsId(instance);

## Lazy loading of instance thread safe.
- You can ignore the next instructions, If you don't access the property while binding the property, or access the property while unbinding the property, or bind the property when unbinding the property.
- In a large number of `multi-threaded` concurrency, bind - unbind, bind - access property, unbind - access property, has a small probability of generating an error : 'Attempt to use unknown class.'The error occurred when an object was accessed when object_setClass() was not completed. Although this probability is relatively small, it is still worthy of attention.This project has implemented the hooking of the runtimelock, which is used to achieve thread safety, but this seriously affects the efficiency, so instead of adopting the scheme, the scheme below is used.
- It is absolutely safe to use the following form in multi-threaded :

apc_safe_instance(instance, ^(id object) {

    APCLazyload(object, property);

apc_safe_instance(instance, ^(id object) {

    APCUnbindLazyload(object, property);

apc_safe_instance(instance, ^(Man* object) {

    [object accessProperty];

Lazy loading of class thread safe.

  • You can ignore here, if you nerver unbind class hook.
  • Bind or unbind a class hook is thread safe.
  • If the hooked class needs to be unbind, it is recommended that you implement the following method in main().

int main(int argc, const char * argv[]) {

    1. Call before app or runtime load!
    2. This method will implement the function of deleting the method at runtime, 
    and if it is not called, it will adopt the scheme of analog deletion,
    which will affect the user's use of the method swizzle.
    The effect is that the correct class must be used to method-swizzle , 
    get Method , get imp.
    Exp :
    class_replaceMethod(CorrectClass, ...);
    class_getInstanceMethod(CorrectClass, ...);
return ... ...(argc, argv);


## Possible to do
- Support all structure types
- New dynamic border

## Author
- Emergency : [email protected]

Latest podspec

    "name": "AutoPropertyCocoa",
    "version": "1.0.2",
    "platforms": {
        "osx": "10.10",
        "ios": "7.0",
        "tvos": "9.0",
        "watchos": "2.0"
    "summary": "Cocoa lazy loading of property and object-oriented property hook by objc runtime.",
    "homepage": "",
    "license": "MIT",
    "authors": {
        "Meterwhite": "[email protected]"
    "requires_arc": true,
    "source": {
        "git": "",
        "tag": "1.0.2"
    "source_files": "AutoPropertyCocoa/**/*.{h,c,m,mm}"

Pin It on Pinterest

Share This