ios category问题
巴扎黑
巴扎黑 2017-04-17 17:48:59
0
3
310

学习ios category的时候文章上说category无法向已有类添加实例变量
但是作者给出的例子让我有点懵圈了


求解释那个name是怎么回事

巴扎黑
巴扎黑

reply all(3)
左手右手慢动作

This is an attribute, not a variable.
Add attributes to the class and corresponding variables will be automatically generated.
And adding attributes in the category will not generate corresponding instance variables.
Because methods can be added to categories, you can think of:
@property(nonatomic, copy) NSString *name; Actually, a set and get method is added

Ty80

objc is a dynamic language, we can add properties to existing classes at runtime.

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

id objc_getAssociatedObject(id object, const void *key)

For example in SVPullToRefresh:

@property (nonatomic, strong, readonly) SVPullToRefreshView *pullToRefreshView;

- (void)setPullToRefreshView:(SVPullToRefreshView *)pullToRefreshView {
    //[self willChangeValueForKey:@"SVPullToRefreshView"];
    objc_setAssociatedObject(self, &UIScrollViewPullToRefreshView,
                             pullToRefreshView,
                             OBJC_ASSOCIATION_ASSIGN);
    //[self didChangeValueForKey:@"SVPullToRefreshView"];
}

- (SVPullToRefreshView *)pullToRefreshView {
    return objc_getAssociatedObject(self, &UIScrollViewPullToRefreshView);
}
阿神

Adding attributes actually declares the set and get methods, but the specific set and get methods must be implemented by yourself.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template