ios7新增基础类库以及OC新特性
2016-09-27 09:33
411 查看
新特性:
Modules:用XCode5新建工程默认支持modules编译,老项目需在Build Settings里查找modules,找到的Enable Modules选项设置为YES。
对应新增语法:@import,导入系统头文件,例如:@import MapKit; 或者库的部分头文件:@import UIKit.UIView;
优点:不需要再在Build Phases里的Link Binary With Libraries添加系统framework文件;缺点:不支持自定义或第三方库
新返回类型:instancetype,用在构造函数返回类型上,建议以前用id作为返回类型的都改成instancetype。
好处:有更严格的编译类型检查,便于编译时即可发现潜在的问题;
NSArray:新增函数: - (id)firstObject; 但只要ios4以上都可以用
NSData:新增Base64编码,相应的函数有:
[objc] view
plain copy
- (id)initWithBase64EncodedString:(NSString *)base64String
options:(NSDataBase64DecodingOptions)options;
- (NSString *)base64EncodedStringWithOptions:
(NSDataBase64EncodingOptions)options;
- (id)initWithBase64EncodedData:(NSData *)base64Data
options:(NSDataBase64DecodingOptions)options;
- (NSData *)base64EncodedDataWithOptions:
(NSDataBase64EncodingOptions)options;
NSTimer:新增函数:
[objc] view
plain copy
- (NSTimeInterval)tolerance;
- (void)setTolerance:(NSTimeInterval)tolerance;
设置tolerance对于启动若干个fireDate相近的NSTimer有用,节省CPU唤醒时间
NSCharacterSet新增函数:
新增类:
NSProgress:进度通知类
NSURLComponents:可把其视作NSMutableURL,例子:
[html] view
plain copy
NSURLComponents *components = [NSURLComponents componentsWithString:@"http://nshipster.com"];
components.path = @"/iOS7";
components.query = @"foo=bar";
NSLog(@"%@", components.scheme); // @"http"
NSLog(@"%@", [components URL]); // @"http://nshipster.com/iOS7?foo=bar"
CIDetectorSmile & CIDetectorEyeBlink:图像微笑和眨眼识别,例子:
[html] view
plain copy
CIDetector *smileDetector = [CIDetector detectorOfType:CIDetectorTypeFace
context:context
options:@{CIDetectorTracking: @YES,
CIDetectorAccuracy: CIDetectorAccuracyLow}];
NSArray *features = [smileDetector featuresInImage:image options:@{CIDetectorSmile:@YES}];
if (([features count] > 0) && (((CIFaceFeature *)features[0]).hasSmile)) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(didFinishWritingImage), features);
} else {
self.label.text = @"Say Cheese!"
}
[html] view
plain copy
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
if (input) {
[session addInput:input];
} else {
NSLog(@"Error: %@", error);
}
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[session addOutput:output];
[session startRunning];
[html] view
plain copy
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection
{
NSString *QRCode = nil;
for (AVMetadataObject *metadata in metadataObjects) {
if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) {
// This will never happen; nobody has ever scanned a QR code... ever
QRCode = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
NSLog(@"QR Code: %@", QRCode);
}
[html] view
plain copy
NSURL *URL = [NSURL URLWithString:@"http://nshipster.com/ios7"];
[[SSReadingList defaultReadingList] addReadingListItemWithURL:URL
title:@"NSHipster"
previewText:@"..."
error:nil];
[html] view
plain copy
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Just what do you think you're doing, Dave?"];
utterance.rate = AVSpeechUtteranceMinimumSpeechRate; // Tell it to me slowly
[synthesizer speakUtterance:utterance];
[html] view
plain copy
CLLocation *sanFrancisco = [[CLLocation alloc] initWithLatitude:37.775 longitude:-122.4183333];
CLLocation *portland = [[CLLocation alloc] initWithLatitude:45.5236111 longitude:-122.675];
CLLocationDistance distance = [portland distanceFromLocation:sanFrancisco];
MKDistanceFormatter *formatter = [[MKDistanceFormatter alloc] init];
formatter.units = MKDistanceFormatterUnitsImperial;
NSLog(@"%@", [formatter stringFromDistance:distance]); // 535 miles
参考网址:http://nshipster.com/ios7/
http://www.raywenderlich.com/49850/whats-new-in-objective-c-and-foundation-in-ios-7
顶
1
Modules:用XCode5新建工程默认支持modules编译,老项目需在Build Settings里查找modules,找到的Enable Modules选项设置为YES。
对应新增语法:@import,导入系统头文件,例如:@import MapKit; 或者库的部分头文件:@import UIKit.UIView;
优点:不需要再在Build Phases里的Link Binary With Libraries添加系统framework文件;缺点:不支持自定义或第三方库
新返回类型:instancetype,用在构造函数返回类型上,建议以前用id作为返回类型的都改成instancetype。
好处:有更严格的编译类型检查,便于编译时即可发现潜在的问题;
NSArray:新增函数: - (id)firstObject; 但只要ios4以上都可以用
NSData:新增Base64编码,相应的函数有:
[objc] view
plain copy
- (id)initWithBase64EncodedString:(NSString *)base64String
options:(NSDataBase64DecodingOptions)options;
- (NSString *)base64EncodedStringWithOptions:
(NSDataBase64EncodingOptions)options;
- (id)initWithBase64EncodedData:(NSData *)base64Data
options:(NSDataBase64DecodingOptions)options;
- (NSData *)base64EncodedDataWithOptions:
(NSDataBase64EncodingOptions)options;
NSTimer:新增函数:
[objc] view
plain copy
- (NSTimeInterval)tolerance;
- (void)setTolerance:(NSTimeInterval)tolerance;
设置tolerance对于启动若干个fireDate相近的NSTimer有用,节省CPU唤醒时间
NSCharacterSet新增函数:
+ (id)URLUserAllowedCharacterSet
+ (id)URLPasswordAllowedCharacterSet
+ (id)URLHostAllowedCharacterSet
+ (id)URLPathAllowedCharacterSet
+ (id)URLQueryAllowedCharacterSet
+ (id)URLFragmentAllowedCharacterSet
新增类:
NSProgress:进度通知类
NSURLComponents:可把其视作NSMutableURL,例子:
[html] view
plain copy
NSURLComponents *components = [NSURLComponents componentsWithString:@"http://nshipster.com"];
components.path = @"/iOS7";
components.query = @"foo=bar";
NSLog(@"%@", components.scheme); // @"http"
NSLog(@"%@", [components URL]); // @"http://nshipster.com/iOS7?foo=bar"
CIDetectorSmile & CIDetectorEyeBlink:图像微笑和眨眼识别,例子:
[html] view
plain copy
CIDetector *smileDetector = [CIDetector detectorOfType:CIDetectorTypeFace
context:context
options:@{CIDetectorTracking: @YES,
CIDetectorAccuracy: CIDetectorAccuracyLow}];
NSArray *features = [smileDetector featuresInImage:image options:@{CIDetectorSmile:@YES}];
if (([features count] > 0) && (((CIFaceFeature *)features[0]).hasSmile)) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(didFinishWritingImage), features);
} else {
self.label.text = @"Say Cheese!"
}
AVCaptureMetaDataOutput:支持二维码及其他类型码识别,例子:
[html] viewplain copy
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
if (input) {
[session addInput:input];
} else {
NSLog(@"Error: %@", error);
}
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[session addOutput:output];
[session startRunning];
[html] view
plain copy
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection
{
NSString *QRCode = nil;
for (AVMetadataObject *metadata in metadataObjects) {
if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) {
// This will never happen; nobody has ever scanned a QR code... ever
QRCode = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
NSLog(@"QR Code: %@", QRCode);
}
SSReadingList:添加URL到safari阅读列表中,例子:
[html] viewplain copy
NSURL *URL = [NSURL URLWithString:@"http://nshipster.com/ios7"];
[[SSReadingList defaultReadingList] addReadingListItemWithURL:URL
title:@"NSHipster"
previewText:@"..."
error:nil];
AVSpeechSynthesizer:文本转语音,例子:
[html] viewplain copy
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Just what do you think you're doing, Dave?"];
utterance.rate = AVSpeechUtteranceMinimumSpeechRate; // Tell it to me slowly
[synthesizer speakUtterance:utterance];
MKDistanceFormatter:距离格式化为本地文本,例子:
[html] viewplain copy
CLLocation *sanFrancisco = [[CLLocation alloc] initWithLatitude:37.775 longitude:-122.4183333];
CLLocation *portland = [[CLLocation alloc] initWithLatitude:45.5236111 longitude:-122.675];
CLLocationDistance distance = [portland distanceFromLocation:sanFrancisco];
MKDistanceFormatter *formatter = [[MKDistanceFormatter alloc] init];
formatter.units = MKDistanceFormatterUnitsImperial;
NSLog(@"%@", [formatter stringFromDistance:distance]); // 535 miles
参考网址:http://nshipster.com/ios7/
http://www.raywenderlich.com/49850/whats-new-in-objective-c-and-foundation-in-ios-7
顶
1
相关文章推荐
- ios7新增基础类库以及OC新特性
- ios7新增基础类库以及OC新特性
- ios7新增基础类库以及OC新特性
- ios7新增基础类库以及OC新特性
- ios7新增基础类库以及OC新特性
- ios7新增基础类库以及OC新特性
- 黑马程序员——ios开发基础之OC中的面向对象以及对象特性
- OC基础—三大特性之继承
- OC中特性、静态成员(static)以及协议的基本知识
- iOS开发OC基础:类和对象以及方法的声明与实现概述
- 黑马程序员——OC语言基础——面向对象三大特性之封装
- 黑马程序员——OC基础三大特性
- 黑马程序员——OC基础——面向对象的三大特性(一)
- 黑马程序员——OC语言基础——面向对象三大特性之多态
- 建立Windows Embedded Compact 7开发环境 以及 新增特性
- 黑马程序员—OC语言基础—面向对象的三大特性(2)
- 黑马程序员——OC基础——类的三大特性(2)
- Objective - C基础: 第一天 - 2. 第3, 4个OC程序 - 多文件编译以及与C文件混编
- 黑马程序员_OC基础04_面向对象三大特性
- 黑马程序员——OC基础——面向对象的三大特性(三)