id objc_getAssociatedObject(id object, const void *key);
Returns the value associated with a given object for a given key.
Parameters
object
The source object for the association.
key
The key for the association.
Returns
The value associated with the key key for object.
Availability iOS (3.1 and later), macOS (10.6 and later), tvOS (9.0 and later), watchOS (1.0 and later)
objc_setAssociatedObject需要四个参数:源对象,关键字,关联的对象和一个关联策略。
- (UINavigationController *)filterNavigation {
return objc_getAssociatedObject(_sponsor, _cmd);
}
- (void)setFilterNavigation:(UINavigationController *)filterNavigation {
//让sponsor持有filterNavigation
objc_setAssociatedObject(_sponsor, @selector(filterNavigation), filterNavigation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}