respondsToSelector使用


这些方法隶属于 NSObject protocol,意味着几乎所有类的对象都适用(继承自NSObject)。

    • -(BOOL) isKindOfClass: classObj用来判断是否是某个类或其子类的实例
    • -(BOOL) isMemberOfClass: classObj用来判断是否是某个类的实例
    • -(BOOL) respondsToSelector: selector用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)

     

    • if([objectrespondsToSelector:@selector(methodName)])

     

    • +(BOOL) instancesRespondToSelector: selector用来判断实例是否有以某个名字命名的方法.和上面一个不同之处在于,前面这个方法可以用在实例和类上,而此方法只能用在类上.
    • -(id) performSelector: selector

     

    SEL sel = @selector (start:) ; // 指定action  

    if ([obj respondsToSelector:sel]) 

    { //判断该对象是否有相应的方法  

    [obj performSelector:sel withObject:self]; //调用选择器方法  

    } 

    使用[[UIApplication sharedApplication] keyWindow]查找应用程序的主窗口对象


    https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用运行时API可以定位内存泄露,具体可以通过以下步骤实现: 1. 在 Xcode 中选择 Product -> Scheme -> Edit Scheme,然后在左侧选择 Run,接着在 Arguments Passed On Launch 中添加环境变量:MallocStackLoggingNoCompact,并将其值设为 1。 2. 在代码中添加以下代码: ``` #import <objc/runtime.h> void checkMemoryLeak() { unsigned int count = 0; Class *classes = objc_copyClassList(&count); for (unsigned int i = 0; i < count; i++) { Class cls = classes[i]; if (class_respondsToSelector(cls, @selector(doesNotRecognizeSelector:))) { // This class has no method implementations of its own. continue; } unsigned int methodCount = 0; Method *methods = class_copyMethodList(cls, &methodCount); for (unsigned int j = 0; j < methodCount; j++) { Method method = methods[j]; SEL selector = method_getName(method); if (selector == @selector(alloc) || selector == @selector(new)) { // Skip alloc and new methods to avoid false positives. continue; } NSString *methodName = NSStringFromSelector(selector); if ([methodName hasPrefix:@"init"]) { // Skip init methods to avoid false positives. continue; } IMP imp = method_getImplementation(method); if (imp == NULL) { // Skip methods with no implementation. continue; } if (strstr(methodName.UTF8String, "copy") != NULL || strstr(methodName.UTF8String, "mutableCopy") != NULL) { // Skip copy and mutableCopy methods to avoid false positives. continue; } if (strstr(methodName.UTF8String, "retain") != NULL) { // Skip retain methods to avoid false positives. continue; } if (strstr(methodName.UTF8String, "release") != NULL || strstr(methodName.UTF8String, "autorelease") != NULL) { // Skip release and autorelease methods to avoid false positives. continue; } // Call the method with a nil argument to create an object without retaining it. ((id (*)(id, SEL, ...))method_invoke)(cls, selector, nil); } free(methods); } free(classes); } ``` 3. 在代码中调用 checkMemoryLeak 函数,这个函数会递归遍历所有类及其方法,并在不保留对象的情况下调用方法,从而创建对象并触发内存分配。如果有内存泄漏,可以通过 Instruments 工具捕获堆栈信息并进行分析。 需要注意的是,这个方法只是一种定位内存泄漏的手段,具体的问题还需要根据具体情况进行分析和解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值