Objective-C 协议和运行时检查方法、类是否存在
协议的声明:
//
// Person.h
// TestOC01
//
// Created by xinye on 13-10-23.
// Copyright (c) 2013年 xinye. All rights reserved.
// #import <Foundation/Foundation.h> @protocol Person <NSObject> @property (nonatomic,strong) NSString *firstName;
@property (nonatomic,strong) NSString *lastName;
@property (nonatomic,unsafe_unretained) NSUInteger age; @optional
-(id<Person>) initWithFirstName:(NSString *) firstName
lastName:(NSString *) lastName
age:(NSUInteger) age;
@required
-(id<Person>) initWithNil;
@end
实现协议:
//
// Father.h
// TestOC01
//
// Created by xinye on 13-10-23.
// Copyright (c) 2013年 xinye. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Person.h" @interface Father : NSObject <Person> +(void) sayNil; @end //
// Father.m
// TestOC01
//
// Created by xinye on 13-10-23.
// Copyright (c) 2013年 xinye. All rights reserved.
// #import "Father.h" @implementation Father
// 实现一个协议,必须实现其@required标记的方法,并且必须@synthesize协议中定义的@requeired属性,协议中定义的方法和属性默认都是@required的
@synthesize firstName,lastName,age; -(id<Person>) initWithFirstName:(NSString *)_firstName lastName:(NSString *)_lastName age:(NSUInteger)_age
{
self = [super init];
if (self) {
self.firstName = _firstName;
self.lastName = _lastName;
self.age = _age;
} return self;
} -(id<Person>) initWithNil
{
self = [super init];
return self;
} +(void) sayNil
{
NSLog(@"say Nil Method");
}
@end
测试:
//
// main.m
// TestOC01
//
// Created by xinye on 13-10-23.
// Copyright (c) 2013年 xinye. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Person.h"
#import "Father.h" int main(int argc, const char * argv[])
{ @autoreleasepool { id<Person> per = [[Father alloc]initWithFirstName:@"张" lastName:@"三" age:]; NSLog(@"姓名:%@",[[per firstName] stringByAppendingString:per.lastName]);
NSLog(@"年龄:%li",per.age);
// 检测是否有实例方法
if([Father instancesRespondToSelector:@selector(initWithNil)]){
NSLog(@"*****Father 类中有一个实例方法:initWithNil");
}else{
NSLog(@"Father 类中没有initWithNil实例方法");
} // 检测是否有类方法
if([Father respondsToSelector:@selector(sayNil)]){
NSLog(@"*****Father 类中有sayNil类方法");
}else{
NSLog(@"Father 类中没有sayNil类方法");
} // 检测是否有实例方法
if([per respondsToSelector:@selector(initWithFirstName:lastName:age:)]){
NSLog(@"*****Father 类中有initWithFirstName:lastName:age:实例方法");
}else{
NSLog(@"Father 类中没有initWithFirstName:lastName:age:实例方法");
} // 检测指定的类是否存在
if(NSClassFromString(@"NSString") != nil){
NSLog(@"=========当前版本中存在NSString类");
}else{
NSLog(@"$$$$$$$$$当前版本中不存在NSString类");
}
if(NSClassFromString(@"NBString") != nil){
NSLog(@"=========当前版本中存在NBString类");
}else{
NSLog(@"$$$$$$$$$当前版本中不存在NBString类");
} }
return ;
}
Objective-C 协议和运行时检查方法、类是否存在的更多相关文章
- [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统
[源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统 目录 [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统 0x00 摘要 0x01 分割小批次 ...
- 深入理解OOP(三):多态和继承(动态绑定和运行时多态)
在前面的文章中,我们介绍了编译期多态.params关键字.实例化.base关键字等.本节我们来关注另外一种多态:运行时多态, 运行时多态也叫迟绑定. 深入理解OOP(一):多态和继承(初期绑定和编译时 ...
- [转] Java 的泛型擦除和运行时泛型信息获取
原文链接 https://my.oschina.net/lifany/blog/875769 前言 现在很多程序员都会在简历中写上精通 Java.但究竟怎样才算是精通 Java 呢?我觉得不仅要熟练掌 ...
- Java虚拟机系列一:一文搞懂 JVM 架构和运行时数据区
前言 之前写博客一直比较随性,主题也很随意,就是想到什么写什么,对什么感兴趣就写什么.虽然写起来无拘无束,自在随意,但也带来了一些问题,每次写完一篇后就要去纠结下一篇到底写什么,看来选择太多也不是好事 ...
- 排错-windows下 ORA-12560 TNS 协议适配器错误解决方法
排错-windows下_ORA-12560 TNS 协议适配器错误解决方法 by:授客 QQ:1033553122 问题描述: 修改SQL*Plus窗口属性后,重新打开SQL*Plus时出现ORA-1 ...
- DataTable和DataRow利用反射直接转换为Model对象的扩展方法类
DataTable和DataRow利用反射直接转换为Model对象的扩展方法类 /// <summary> /// 类 说 明:给DataTable和DataRow扩展方法,直接转换为 ...
- Objective-C面向对象-对象和类
文章都是先由本人个人博客:孙占兴:www.teilim.com,先更新,随后CSDN博客才会更新,掌握第一动态请关注本人主站. 原文链接:http://www.teilim.com/objective ...
- Java编译时常量和运行时常量
Java编译时常量和运行时常量 编译期常量指的就是程序在编译时就能确定这个常量的具体值. 非编译期常量就是程序在运行时才能确定常量的值,因此也称为运行时常量. 在Java中,编译期常量指的是用fina ...
- day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)
1. 装饰器 / 类中的方法 / 类的方法变属性 # ### 装饰器 """ 定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数 优点:在不更改原函数代码的 ...
随机推荐
- python -修改文件中某一行
写代码写错了顺序,所以想办法把x,y坐标调换回来 def change_ptsxy(fileName): fp = open(fileName) i = file_data = "" ...
- Extjs4.x treepanel,treegrid 节点选择,选中某个节点
//官方推荐 this.getModuleGrid().getRootNode().cascadeBy(function () { this.set("checked", fals ...
- mysql archive存储引擎导入数据报duplicate key
DROP TABLE IF EXISTS `test`;CREATE TABLE `test` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `ve ...
- 【html5】html5 本地存储
最近一直在学习 html5,为了后期的移动项目进行知识储备.html5 相对于 html4 新增加了一些有趣的标签.属性和方法,今天主要介绍下 html5 的本地存储. 在客户端存储数据 html5 ...
- 七牛图片存储api
https://developer.qiniu.com/kodo 属于对象存储 https://www.qiniu.com/prices 如何获取存储文件的外链接 https://developer. ...
- 微信小程序文字水平垂直居中对齐问题
我们知道常用的居中对齐方式有很多种例如: text-align:center; align-items:center; justify-content: center; margin: auto; # ...
- DataWindow.Net组件示例(全部开源)
1概述 1.1功能简介 Sybase公司的PowerBuilder开发工具,在以前VS工具没有成事以前,是相当风光的.微软都要与其合作,学习它Db方面的技术,才成就了SQLServer数据库.PB开发 ...
- linux守护进程编写实践
主要参考:http://colding.bokee.com/5277082.html (实例程序是参考这的) http://wbwk2005.blog.51cto.com/2215231/400260 ...
- 利用R里的options函数进行光标和数字位数设置
用R写代码时,打字水平不高,有时候不知道乱按了一些键(现在我还不知道哪个键),光标就变成了加粗的竖直线,又改不回去.这种情况下我们可以用options函数进行光标设置,例如:options(promp ...
- Reading and writing RData files
前面添加个lapply()或者dplyr::llply()就能读取,储存多个文件了.http://bluemountaincapital.github.io/FSharpRProvider/readi ...