非ARC Objective-C属性访问器是什么样子的?

非ARC Objective-C属性访问器是什么样子的?,objective-c,memory-management,accessor,declared-property,Objective C,Memory Management,Accessor,Declared Property,我想知道如何实现Objective-C属性的getter和setter,作为学习内存管理概念的一部分 除了“非原子的”和“原子的”,我还没有找到一个实际的表示法 对于具有不同属性属性的getter和setter,例如strong/weak、copy/assign和uu unsafe\u unrepaired,实际代码是什么样子的?您可以在 吸气剂: 塞特: 加载弱变量: 我不认为有任何不安全的代码。编译器只需简单地分配指针,而无需其他任何操作。标题中的非弧和问题中的强/弱是相互排斥的。@Drop

我想知道如何实现Objective-C属性的getter和setter,作为学习内存管理概念的一部分

除了“非原子的”和“原子的”,我还没有找到一个实际的表示法


对于具有不同属性属性的getter和setter,例如strong/weak、copy/assign和uu unsafe\u unrepaired,实际代码是什么样子的?

您可以在

吸气剂:

塞特:

加载弱变量:


我不认为有任何不安全的代码。编译器只需简单地分配指针,而无需其他任何操作。

标题中的非弧和问题中的强/弱是相互排斥的。@Droppy我只是好奇它们的内部实现。不管是ARC还是MRC,你需要理解的是,有了ARC,编译器在幕后完成了繁重的工作,没有人真正关心它(除了开发人员和非常好奇的人)。但是,非ARC版本的例子很多,包括本网站上的许多。@Droppy是的,你是对的。我刚刚看到了strong-
-(void)setName:(NSString*)name{[name retain];[U name release];[U name=name;}
的setter,它们有很多概念,就像为什么
[name retain]
是第一行一样。所以我只想看到实际的实现。我从未在MRC工作过。这就是为什么有时我在理解这一点时会感到一些问题。这就是为什么我现在对每件事都了如指掌。
id objc_getProperty_non_gc(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) {
    if (offset == 0) {
        return object_getClass(self);
    }

    // Retain release world
    id *slot = (id*) ((char*)self + offset);
    if (!atomic) return *slot;

    // Atomic retain release world
    spinlock_t& slotlock = PropertyLocks[slot];
    slotlock.lock();
    id value = objc_retain(*slot);
    slotlock.unlock();

    // for performance, we (safely) issue the autorelease OUTSIDE of the spinlock.
    return objc_autoreleaseReturnValue(value);
}
static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
{
    if (offset == 0) {
        object_setClass(self, newValue);
        return;
    }

    id oldValue;
    id *slot = (id*) ((char*)self + offset);

    if (copy) {
        newValue = [newValue copyWithZone:nil];
    } else if (mutableCopy) {
        newValue = [newValue mutableCopyWithZone:nil];
    } else {
        if (*slot == newValue) return;
        newValue = objc_retain(newValue);
    }

    if (!atomic) {
        oldValue = *slot;
        *slot = newValue;
    } else {
        spinlock_t& slotlock = PropertyLocks[slot];
        slotlock.lock();
        oldValue = *slot;
        *slot = newValue;        
        slotlock.unlock();
    }

    objc_release(oldValue);
}
id
objc_loadWeakRetained(id *location)
{
    id result;

    SideTable *table;

 retry:
    result = *location;
    if (!result) return nil;

    table = &SideTables()[result];

    table->lock();
    if (*location != result) {
        table->unlock();
        goto retry;
    }

    result = weak_read_no_lock(&table->weak_table, location);

    table->unlock();
    return result;
}