Ios 我可以使用category和runtime添加BOOL类型属性吗;IsCrolling“;去乌斯克罗维尤?
我以前在Ios 我可以使用category和runtime添加BOOL类型属性吗;IsCrolling“;去乌斯克罗维尤?,ios,objective-c,uiscrollview,runtime,categories,Ios,Objective C,Uiscrollview,Runtime,Categories,我以前在UIButton中添加了一个NSString*type属性,但是今天,我想在UIScrollView中添加一个BOOLtype属性iscrolling,以指示滚动视图是否以相同的方式滚动,但显示出一些错误,下面是我的代码: #import <UIKit/UIKit.h> @interface UIScrollView (Util) @property (nonatomic,assign) BOOL isScrolling; @end #import <ob
UIButton
中添加了一个NSString*
type属性,但是今天,我想在UIScrollView
中添加一个BOOL
type属性iscrolling
,以指示滚动视图是否以相同的方式滚动,但显示出一些错误,下面是我的代码:
#import <UIKit/UIKit.h>
@interface UIScrollView (Util)
@property (nonatomic,assign) BOOL isScrolling;
@end
#import <objc/objc-runtime.h>
@interface UIScrollView ()<UIScrollViewDelegate>
@end
@implementation UIScrollView (Util)
static void *strKey = &strKey;
- (void)setIsScrolling:(BOOL)isScrolling{
objc_setAssociatedObject(self, & strKey, isScrolling, OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)isScrolling{
return objc_getAssociatedObject(self, &strKey);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
self.isScrolling = YES;
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
self.isScrolling = NO;
}
@end
#导入
@界面UIScrollView(Util)
@属性(非原子,赋值)布尔伊斯克林;
@结束
#进口
@界面UIScrollView()
@结束
@实现UIScrollView(Util)
静态无效*strKey=&strKey;
-(void)SetIsCrolling:(BOOL)IsCrolling{
objc_setAssociatedObject(self、strKey、isScrolling、objc_ASSOCIATION_ASSIGN);
}
-(BOOL)烤面包{
返回objc_getAssociatedObject(self和strKey);
}
-(无效)scrollViewDidScroll:(UIScrollView*)scrollView{
self.isScrolling=是;
}
-(void)ScrollViewDiEndScrollingAnimation:(UIScrollView*)scrollView{
self.isScrolling=否;
}
@结束
错误是:
有没有办法处理这些错误?我们可以使用category和runtime来实现将BOOL
属性添加到UIScrollView
以指示滚动视图是否正在滚动的目标
希望有人能给我一些建议,谢谢。试试这个:
尝试将布尔值转换为nsnumber
-(void)setIsScrolling:(BOOL)isScrolling{
objc_setAssociatedObject(self, & strkey), [NSNumber numberWithBool:isScrolling], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
不能将基元数据类型设置为
关联对象
,它是一个对象。保存数据时,将bool
转换为NSNumber
。读取数据时,将NSNumber
转换为bool
OBJC\u ASSOCIATION\u ASSIGN
-指定对关联对象的弱引用。
OBJC\u ASSOCIATION\u RETAIN\u NONATOMIC
-指定对关联对象的强引用,并且关联不是原子性的
.h文件:
#import <UIKit/UIKit.h>
@interface UIScrollView (ScrollViewCategory)
@property (nonatomic, strong)NSNumber *isScrolling;
@end
#导入
@界面UIScrollView(滚动视图类别)
@属性(非原子,强)NSNumber*isScrolling;
@结束
.m文件
@interface UIScrollView ()
@end
#import <objc/runtime.h>
@implementation UIScrollView (ScrollViewCategory)
@dynamic isScrolling;
- (void)setAssociatedObject:(id)object {
objc_setAssociatedObject(self, @selector(associatedObject), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)associatedObject {
return objc_getAssociatedObject(self, @selector(associatedObject));
}
@interface UIScrollView()
@结束
#进口
@实现UIScrollView(ScrollViewCategory)
@动态剪切;
-(void)setAssociatedObject:(id)对象{
objc_setAssociatedObject(self、@selector(associatedObject)、object、objc_ASSOCIATION_RETAIN_NONATOMIC);
}
-(id)关联对象{
返回objc_getAssociatedObject(self,@selector(associatedObject));
}
由于错误显示BOOL到id的隐式转换,您需要发送一个对象而不是基本类型
objc_setAssociatedObject的方法签名为
/**
* Sets an associated value for a given object using a given key and association policy.
*
* @param object The source object for the association.
* @param key The key for the association.
* @param value The value to associate with the key key for object. Pass nil to clear an existing association.
* @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
*
* @see objc_setAssociatedObject
* @see objc_removeAssociatedObjects
*/
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0);
上面可以看到值应该是object
更改代码
@属性(赋值,非原子)BOOL-iscrolling
到@属性(强,非原子)NSNumber*滚动代码>
并将OBJC_ASSOCIATION_ASSIGN
更改为OBJC_ASSOCIATION_RETAIN_NONATOMIC
,在您的案例中OBJC_setassociated object(self,&strkey,滚动,OBJC_ASSOCIATION_RETAIN_NONATOMIC)代码>
并使用[\u滚动布尔值]
进行检查。关联的对象必须是该对象,因此非对象BOOL
类型的值除非包装为对象,否则将不起作用。幸运的是,这很容易:
- 在对
objc\u setassociated object
的调用中,将isScrolling
更改为@(isScrolling)
,并将objc\u ASSOCIATION\u分配给objc\u ASSOCIATION\u RETAIN\u非原子的
。这将创建并传递一个NSNumber
对象,第二个更改请求将该对象的生存期绑定到第一个参数self
的生存期
- 在
iscrolling
中,将objc\u getassociated object(self和strKey)
更改为[objc\u getassociated object(self和strKey)boolValue]
。这将从存储的NSNumber
对象中提取BOOL
值
HTH按原样发布代码。请不要截图。objc_getAssociatedObject用于对象类型。BOOL不是对象类型。这就是为什么你得到了error@LalKrishna是的,我编辑过。是的,你是对的。但是我们可以在类别中处理UIScrollView委托,以便我们可以将值设置为“滚动”属性。我刚才尝试过,但无效。你知道吗?你可以覆盖类别中的委托方法。我可以试着帮你发布代码。