ios7新增基础类库以及OC新特性

新特性:

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 在CODE上查看代码片 派生到我的代码片
  1. - (id)initWithBase64EncodedString:(NSString *)base64String   
  2.       options:(NSDataBase64DecodingOptions)options;  
  3.    
  4. - (NSString *)base64EncodedStringWithOptions:  
  5.       (NSDataBase64EncodingOptions)options;  
  6.    
  7. - (id)initWithBase64EncodedData:(NSData *)base64Data   
  8.       options:(NSDataBase64DecodingOptions)options;  
  9.    
  10. - (NSData *)base64EncodedDataWithOptions:  
  11.       (NSDataBase64EncodingOptions)options;  


NSTimer:新增函数:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (NSTimeInterval)tolerance;  
  2. - (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 在CODE上查看代码片 派生到我的代码片
  1. NSURLComponents *components = [NSURLComponents componentsWithString:@"http://nshipster.com"];  
  2. components.path = @"/iOS7";  
  3. components.query = @"foo=bar";  
  4.   
  5. NSLog(@"%@", components.scheme); // @"http"  
  6. NSLog(@"%@", [components URL]); // @"http://nshipster.com/iOS7?foo=bar"  

CIDetectorSmile & CIDetectorEyeBlink:图像微笑和眨眼识别,例子:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CIDetector *smileDetector = [CIDetector detectorOfType:CIDetectorTypeFace  
  2.                                 context:context   
  3.                                 options:@{CIDetectorTracking: @YES,   
  4.                                           CIDetectorAccuracy: CIDetectorAccuracyLow}];  
  5. NSArray *features = [smileDetector featuresInImage:image options:@{CIDetectorSmile:@YES}];  
  6. if (([features count] > 0) && (((CIFaceFeature *)features[0]).hasSmile)) {  
  7.     UIImageWriteToSavedPhotosAlbum(image, self, @selector(didFinishWritingImage), features);  
  8. } else {  
  9.     self.label.text = @"Say Cheese!"  
  10. }  

AVCaptureMetaDataOutput:支持二维码及其他类型码识别,例子:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. AVCaptureSession *session = [[AVCaptureSession alloc] init];  
  2. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
  3. NSError *error = nil;  
  4.   
  5. AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device  
  6.                                                                     error:&error];  
  7. if (input) {  
  8.     [session addInput:input];  
  9. } else {  
  10.     NSLog(@"Error: %@", error);  
  11. }  
  12.   
  13. AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];  
  14. [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];  
  15. [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];  
  16. [session addOutput:output];  
  17.   
  18. [session startRunning];  

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #pragma mark - AVCaptureMetadataOutputObjectsDelegate  
  2.   
  3. - (void)captureOutput:(AVCaptureOutput *)captureOutput  
  4. didOutputMetadataObjects:(NSArray *)metadataObjects  
  5.        fromConnection:(AVCaptureConnection *)connection  
  6. {  
  7.     NSString *QRCode = nil;  
  8.     for (AVMetadataObject *metadata in metadataObjects) {  
  9.         if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) {  
  10.             // This will never happen; nobody has ever scanned a QR code... ever  
  11.             QRCode = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];  
  12.             break;  
  13.         }  
  14.     }  
  15.   
  16.     NSLog(@"QR Code: %@", QRCode);  
  17. }  

SSReadingList:添加URL到safari阅读列表中,例子:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. NSURL *URL = [NSURL URLWithString:@"http://nshipster.com/ios7"];  
  2. [[SSReadingList defaultReadingList] addReadingListItemWithURL:URL  
  3.                                                         title:@"NSHipster"  
  4.                                                   previewText:@"..."   
  5.                                                         error:nil];  

AVSpeechSynthesizer:文本转语音,例子:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];  
  2. AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Just what do you think you're doing, Dave?"];  
  3. utterance.rate = AVSpeechUtteranceMinimumSpeechRate; // Tell it to me slowly  
  4. [synthesizer speakUtterance:utterance];  

MKDistanceFormatter:距离格式化为本地文本,例子:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CLLocation *sanFrancisco = [[CLLocation alloc] initWithLatitude:37.775 longitude:-122.4183333];  
  2. CLLocation *portland = [[CLLocation alloc] initWithLatitude:45.5236111 longitude:-122.675];  
  3. CLLocationDistance distance = [portland distanceFromLocation:sanFrancisco];  
  4.   
  5. MKDistanceFormatter *formatter = [[MKDistanceFormatter alloc] init];  
  6. formatter.units = MKDistanceFormatterUnitsImperial;  
  7. NSLog(@"%@", [formatter stringFromDistance:distance]); // 535 miles  


参考网址: http://nshipster.com/ios7/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值