【iOS开发系列】NSObject方法介绍
NSObject是OC中的基类,全部类都继承于此,这里面也给我们提供了非常多与“类”和“方法”相关的方法,本文将解说几个非常有用的方法。
正文:
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@end</span>
Student.h
#import "Person.h"
// 继承Person类
@interface Student : Person
- (void)test1;
- (void)test2:(NSString *)string;
@end</span>
MyProtocol.h
#import <Foundation/Foundation.h>
@protocol MyProtocol
@end</span>
【1】推断student是否是Person类的对象
// - (BOOL)isMemberOfClass:(Class)aClass;
[student isMemberOfClass:[Person class]];
【2】推断student是否是Person类或子类的对象
// - (BOOL)isKindOfClass:(Class)aClass;
[student isKindOfClass:[Person class]];
[student isKindOfClass:[Person class]];
【3】推断student是否遵循MyProtocol协议(也能够用类调用,推断该类是否遵循)
// - (BOOL)conformsToProtocol:(Protocol *)aProtocol;
[student conformsToProtocol:@protocol(MyProtocol)];
// 或者使用类方法
// + (BOOL)conformsToProtocol:(Protocol *)protocol;
[Student conformsToProtocol:@protocol(MyProtocol)];
[student conformsToProtocol:@protocol(MyProtocol)]; // 或者使用类方法
// + (BOOL)conformsToProtocol:(Protocol *)protocol;
[Student conformsToProtocol:@protocol(MyProtocol)];
【4】推断student的test1方法是否响应(即:是否声明并实现了test1方法)
// - (BOOL)respondsToSelector:(SEL)aSelector;
[student respondsToSelector:@selector(test1)];
[student respondsToSelector:@selector(test1)];
【5】间接调用student的test1方法(test1无參数)
// - (id)performSelector:(SEL)aSelector;
[student performSelector:@selector(test1)];
[student performSelector:@selector(test1)];
【6】间接调用student的test2方法(test2有一个參数)
// - (id)performSelector:(SEL)aSelector withObject:(id)object;
[student performSelector:@selector(test2:) withObject:@"123"];
// 最多带两个參数
//- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
[student performSelector:@selector(test2:) withObject:@"123"]; // 最多带两个參数
//- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
【7】延迟2s调用student的test1方法
(在命令行没有延迟效果,由于命令行运行完后就退出main函数了 ,在IOS部分main函数一直在运行。所以能够看到延迟效果)
<span style="font-family:SimHei;">// - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
// delay单位为(秒)
[student performSelector:@selector(test2:) withObject:@"123" afterDelay:2];</span>
【iOS开发系列】NSObject方法介绍的更多相关文章
- iOS开发系列--Swift进阶
概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...
- iOS开发系列--通知与消息机制
概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地 ...
- iOS开发系列--数据存取
概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储到数据库.例如前面IOS开发系列-Objective-C之Foundation框架的文章中提到归档.plist文件存储, ...
- iOS开发系列--网络开发
概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博.微信等,这些应用本身可能采用iOS开发,但是所有的数据支撑都是基于后台网络服务器的.如今,网络编程越来越普遍,孤立的应用通常是没有生命力 ...
- iOS开发系列--让你的应用“动”起来
--iOS核心动画 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建 ...
- iOS开发系列--并行开发其实很容易
--多线程开发 概览 大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的, ...
- iOS开发系列--让你的应用“动”起来
--iOS核心动画 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建 ...
- 【转】iOS开发系列--数据存取
原文: http://www.cnblogs.com/kenshincui/p/4077833.html#SQLite 概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储 ...
- IOS开发系列 --- 核心动画
原始地址:http://www.cnblogs.com/kenshincui/p/3972100.html 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥i ...
- iOS开发系列--让你的应用“动”起来【转载】
概览 原文链接:http://www.cnblogs.com/kenshincui/p/3972100.html 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥i ...
随机推荐
- Android 使用 Application 简单介绍
Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里面实例化Application ...
- hbuilder中的 http://www.w3.org/TR/html4/strict.dtd
<!-- This is HTML 4.01 Strict DTD, which excludes the presentation attributes and elements that W ...
- 15年用canvas画的
请恕我当年的工作太轻松,用canvas手打了一个图,技术含量并没有什么,现在看看,甚是怀念_(¦3」∠)_ <!DOCTYPE html> <html> <head&g ...
- Angular——基本使用
基本介绍 1.AngularJS是一个框架(诸多类库的集合)以数据和逻辑做为驱动(核心). 2.AngularJS有着诸多特性,最为核心的是:模块化.双向数据绑定.语义化标签.依赖注入等. 模块化 使 ...
- JS——样式类的添加
1.注意current前有个空格 this.className = this.className + " current"; 2.直接将class所有的值替换成current th ...
- Linux 之CentOS7使用firewalld打开关闭防火墙与端口
一.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...
- Redis 之消息发布与订阅(publish、subscribe)
使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称 发布内容 一般做群聊,聊天室,发布公告信息等.
- ionic 创建某个文件下的page
ionic g page 文件名 --pagesDir src/pages/about
- On branch master nothing to commit, working tree clean ERROR: Repository not found. fatal: Could not read from remote repository.
将gitbash部署hexo到github:hexo deploy 报以下错误: Administrator@liu MINGW64 /Hexo $ hexo d INFO Deploying: gi ...
- CAD设置当前显示的光标(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_SetCursor 设置当前显示的光标,光标可以从cur文件加载,详细说明如下: 参数 说明 CString sCursorFi ...