IOS学习笔记 (2)

使用协议委托任务

协议是一个声明某些方法及属性并储存在实体文档。

协议就像是一些规范,实践协议的类必须遵守这些规范。

创建协议   xcode-->File -->New --> New File  --> Cocoa Touch --> Objective - C Protocol  -->Next -->协议名称(eg:PersonProtocol)-->Save

实际声明协议:

#import<Foundation/Foundation.h>

@protocol PersonProtocol<NSObject>

@optional

@property(nonamotic,strong)NSString *firstName;

@property(nonamotic,strong)NSString *lastName;

@required    //必须实现的方法和属性

@property(nonamotic,unsafe_unretatined)NSUInteger arg;

-(void)breathe;

@end

创建一个Father类,实现协议

#import<Foundation/Foundation.h>

#import "PersonProtocol.h"

@interface Father:NSObject<PersonProtocol>

-(void)breathe;

@end //.m里面要有实例方法。

协议里的关键字  @required 明确指定必须的属性与方法。

@optional 可以实践或不需要实践使用

监测实例方法或类方法是否有效

使用环境:你开发的SDK是最新版的,但是你希望支持执行旧版IOS SDK的装置。

使用 NSObject instancesRespondToSelector:类方法检测指定的 selector 是否存在类实例中。要确认一个类 是否响应本身的类方法,需使用 respondsToSelector:类方法。你可以使用同样方式检测,在一个实例的实例方 ,透过 instancesRespondToSelector:NSObject 的类方法

这边有两个关于 iOS SDK 的重要概念要记住:
Base SDK(基底 SDK)
这个 SDK 是用来编译应用程序。可能是最新最大的 SDK,且能存取所有新的 API
Deployment SDK/Target(部署 SDK)
这边的 SDK 使指定你希望编译后并执行的装置 SDK 版本。
因为就事实上,编译应用是基于 Base SDK Depolyment SDK 两个 SDK 编译。

举个例子:

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Item 1",
@"Item 4",
@"Item 2",

@"Item 5",
@"Item 3", nil];
NSLog(@"Array = %@", array);
if ([NSArray instancesRespondToSelector:@selector(sortUsingComparator:)]){

page77image11688.png

/* 使用 sortUsingComparator: 实例方法来排列数组*/ }
else if ([NSArray instancesRespondToSelector: @selector(sortUsingFunction:context:)]){

/* 使用 sortUsingFunction:context: 实体方法做数组排序*/ }
else {
/* 处理其他状况 */

}

确认类是否可在运行期中使用

使用环境:你正在使用最新版的 SDK,但是你无法确定是否可以使用,因为无法肯定用户是否有安装最新版的 SDK


使用字符串

使用NSString NSMutableString

NSString 类不能更改的,NSString类一旦被创建,内容就不能修改了。可变字符串NSMutableString创建以后还可以修改。

NSString *string = @"hello world";

NSString *string = [NSString stringWithString:@"hello world"];

NSMutableString同上

字符串长度   [string  length];

字符串类型转换:

NSString *string = @"123.456";

NSInteger integerOfString = [string integerValue];    //123

CGFloat  floatOfString = [string floatValue];    //123.456001

Double doubleOfString = [string doubleValue];    //123.456.000

字符串中查找字符串

NSString *one = @"hello world";

NSString *two = @"hello";

NSRange range = [one rangeOfString:two];

if(range.location == NSNotFound){

    /*Coule NOT find nedle in haystack*/

}else{

    NSLog(@"%@",range.location);

}

数字

NSNumber 类用来面向对象的方法处理数字。

NSInteger 有符号数(正数或负数) NSUInteger无符号数(正数和0)

CGFloat double 操作浮点数。

numberWithInteger:将一个整型值封装成一个NSNumber实例

numberWithUnsignedInteger:讲一个无符号整型值封装成一个NSNumber实例

numberWithFloat:将一个浮点数封装成一个NSNumber实例

numberWithDouble:将一个double类型的数封装成一个NSNumber实例

下面这些方法用来从一个NSNumber实例中提取纯数字:

integerValue/unsignedIntegerValue/floatValue/doubleValue

数字和一个字符串比较方法:

NSNumber *unsighedNumber = [NSNumber numberWithUnsignedInteger:123456];

NSString *numberInString = [NSString stringWithFormat:@"%lu",(unsigned long)[unsignedNumber unsignedIntegerValue]];

NSLog(@"%@",numberInString);


分配和使用数组:

NSArray NSMutableArray

NSArray *array = [[NSArray alloc]initWithObjects:@"one",@"two",@"three",nil];

自动释放的数组:

NSArray*array = [NSArray arrayWithObjects: @"one",@"two",@"three",nil];

数组长度   [array count];

nsmutableArray

数组增加对象[array addObject:@"four"];

删除数组[array removeObject:@"four"];

获取数组指定位置的对象

objectAtIndex

分配和使用 Dictionary

分配和使用Sets

Setsarray非常相似,二者最大的区别就是sets中相同对象只能被添加一次。当你第二次添加同一个对象时,sets会拒绝添加。

addObject 增加对象  removeObject 删除对象

快速遍历一个set中所有的对象 enumerateObjectsUsingBlock:方法。例如

[setOfNames enumerateObjectsUsingBlock:^(__strong id obj,BOOL *stop){

    if([obj isKindOfClass:[NSString class]]){

        NSString *string = (NSString *)obj;

        if([string isEqualToString:@"Kiyosaki"){

            NSLog(@"Found %@ in the set",string);

            *stop = YES;

     }

  }

}


通过NSNotificationCenter发送通知

通过App发布一条通知同时允许其他对象接收通知并采取行动,这取决于你发布的通知。

使用NSNotificationCenterdefault notificationcenterpostNotificationNameobjectuserInfo:的实例方法发布一条通知,其中携带一个对象(通常此对象激活通知)和一条用户信息词典,词典中包含了关于词条通知或者激活通知的对象的额外信息。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值