新特性:
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编码,相应的函数有:
- (id)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options; - (NSString *)base64EncodedStringWithOptions: (NSDataBase64EncodingOptions)options; - (id)initWithBase64EncodedData:(NSData *)base64Data options:(NSDataBase64DecodingOptions)options; - (NSData *)base64EncodedDataWithOptions: (NSDataBase64EncodingOptions)options;
NSTimer:新增函数:
- (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,例子:
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:图像微笑和眨眼识别,例子:
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:支持二维码及其他类型码识别,例子:
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];
#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阅读列表中,例子:
NSURL *URL = [NSURL URLWithString:@"http://nshipster.com/ios7"]; [[SSReadingList defaultReadingList] addReadingListItemWithURL:URL title:@"NSHipster" previewText:@"..." error:nil];
AVSpeechSynthesizer:文本转语音,例子:
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:距离格式化为本地文本,例子:
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