respondsToSelector的相关使用

-(BOOL) isKindOfClass: classObj 用来判断是否是某个类或其子类的实例
-(BOOL) isMemberOfClass: classObj 用来判断是否是某个类的实例
-(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
+(BOOL) instancesRespondToSelector: selector 用来判断实例是否有以某个名字命名的方法. 和上面一个不同之处在于, 前面这个方法可以用在实例和类上,而此方法只能用在类上.
-(id) performSelector: selector

SEL sel = @selector (start:) ; // 指定action
if ([obj respondsToSelector:sel])
{ //判断该对象是否有相应的方法
[obj performSelector:sel withObject:self]; //调用选择器方法
}
使用[[UIApplication sharedApplication] keyWindow]查找应用程序的主窗口对象

respondsToSelector判断是否实现了某方法

Tester.h

#import <Foundation/Foundation.h>

@interface Tester : NSObject {
}
-(void) test:(NSString*) msg;
-(void) notImp;

@end


Tester.m

#import "Tester.h"
@implementation Tester

-(void) test:(NSString*) msg
{
NSLog(@"%@", msg);
}

@end


注意:没有实现notImp方法
main.m


#import <Foundation/Foundation.h>
#import "Tester.h"

int main (int argc, const char * argv[])
{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

id tester = [[Tester alloc] init];//注意,这里使用id

SEL testSelector = @selector(test:);
SEL notImpSelector = @selector(notImp:);

if([tester respondsToSelector:testSelector])
{
//tester.m中实现了test方法
[tester test:@"invoke test method"];
}
if([tester respondsToSelector:notImpSelector])
{
//test.m中没有实现此主就去
[tester notImp];
}

[pool drain];
return 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、付费专栏及课程。

余额充值