您的位置:首页 > 理论基础 > 计算机网络

iOS 网络与多线程--7.Performselector消息处理方法

2015-12-04 23:19 429 查看
创建一个IOSApp类

IOSApp.h文件

#import <Foundation/Foundation.h>

@interface IOSApp : NSObject

// 1.添加一个无参数的方法
-(void)printInfomation;

// 2.添加一个有参数的方法
-(void)buyApp:(id)appName;

@end


IOSApp.m文件

#import "IOSApp.h"

@implementation IOSApp

// 3.实现头文件中无参数的方法
-(void)printInfomation
{
NSLog(@"Xcode Interactive Tutorials");
}

// 4.实现头文件中带有参数的方法
-(void)buyApp:(id)appName
{
NSLog(@"Buy the App%@",appName);
}

@end


ViewController.m 文件

#import "ViewController.h"
// 5.导入钢材创建的类的头文件
#import "IOSApp.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// 6.初始化一个类对象
IOSApp *app = [[IOSApp alloc] init];
// 7.@selector()可以理解为取类方法的编号,它的行为基本可以等同c语言中的函数指针,它的结果是SEL类型。
SEL method = @selector(printInfomation);
// 8.respondsToSelector()方法,用来判断是否有,以某个名字命名的方法。
if ([app respondsToSelector:method]){

// 9.performSelector是由运行时系统负责去找方法的,在编译时不做任何校验
// 调用方法
[app performSelector:method];
}

SEL method2 = @selector(buyApp:);
if ([app respondsToSelector:method2]) {
// 调用方法
[app performSelector:method2 withObject:(@"Photoshop Interactive Tutorials")];
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: