首页 > 代码库 > iOS 10 / Swift 3.0 / XCode 8 总结
iOS 10 / Swift 3.0 / XCode 8 总结
1,iOS10 新增的privacy settings
iOS10添加了新的权限控制范围 如果你尝试访问这些隐私数据时得到如下错误:
> This app has crashed because it attempted to access privacy-sensitive> data without a usage description. The app‘s Info.plist must contain> an NSCameraUsageDescription key with a string value explaining to the> user how the app uses this data
因为它企图访问敏感数据时没有在应用程序的Info.plist
设置privacy key 新增的privacy setting如下:
2, OS_ACTIVITY_MODE
更新Xcode 8 如果控制台出现enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0enable_oversize: 可通过如下方法设置:
Edit Scheme-> Run -> Arguments, 在Environment Variables里边添加OS_ACTIVITY_MODE = Disable
3,iOS10 layoutIfNeed
iOS10 在一个控件上调用layoutIfNeed是只会单独计算约束,它所约束的控件不会生效,想要达到之前的效果需要在父级控件上调用layoutIfNeed
4, NSDate
Swift3.0会将oc的NSDate转为Data类型,有些操作NSDate的第三方库会闪退
5, Notification
Swift3.0字符串类型的通知常量被定义为struct
static let MyGreatNotification = Notification.Name("MyGreatNotification")// Use site (no change)NotificationCenter.default().post(name: MyController.MyGreatNotification, object: self)‘
6, Zip2Sequence(::) 被移除
在Swift3.0Zip2Sequence(_:_:)方法被替换为zip(_:_:)
7, Range<>.reversed 被移除
在Swift3.0Range<>.reversed方法被移除,被替换为<Collection>[<Range>].indices.reversed().
var array = ["A","B","C","D"]for i in array.indices.reversed() { print("\(i)")}输出:3 2 1 0
8, Range新增至四种类型
RangeCountableRangeClosedRangeCountableClosedRange
不同的表达式会生成不同的Range
var countableRange = 0..<20 ‘CountableRange(0..<20)‘var countableClosedRange = 0...20 ‘CountableClosedRange(0...20)‘
9, Swift3.0 Collection 新增 index(_:)系列方法
Index的successor(), predecessor(), advancedBy(_:), advancedBy(_:limit:), or distanceTo(_:)方法被移除,这些操作被移动到Collection
myIndex.successor() => myCollection.index(after: myIndex)myIndex.predecessor() => myCollection.index(before: myIndex)myIndex.advance(by: …) => myCollection.index(myIndex, offsetBy: …)
10, iOS10 UIStatusBar过期
如果你需要操作UIStatusBar,在iOS10需要改为
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleDefault;}
11, iOS10 UICollectionView 性能优化
在iOS10 UICollectionView 最大的改变是增加了Pre-Fetching(预加载),
如果你翻看UICollectionView的最新API你可以发现新增了如下属性:
@property (nonatomic, weak, nullable) id<UICollectionViewDataSourcePrefetching> prefetchDataSource @property (nonatomic, getter=isPrefetchingEnabled) BOOL
在iOS10 Pre-Fetching 是默认开启的,如果出于某些原因你不想开启Pre-Fetching,可以通过如下设置禁用:
collectionView.isPrefetchingEnabled = false
UICollectionViewDataSourcePrefetching协议定义如下:
@protocol UICollectionViewDataSourcePrefetching <NSObject>@required// indexPaths are ordered ascending by geometric distance from the collection view- (void)collectionView:(UICollectionView *)collectionView prefetchItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths NS_AVAILABLE_IOS(10_0);@optional// indexPaths that previously were considered as candidates for pre-fetching, but were not actually used; may be a subset of the previous call to -collectionView:prefetchItemsAtIndexPaths:- (void)collectionView:(UICollectionView *)collectionView cancelPrefetchingForItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths NS_AVAILABLE_IOS(10_0);@end
12, iOS10 UITableView 性能优化
和UICollectionView一样UITableView也增加了Pre-Fetching技术,UITableView新增了如下属性:
@property (nonatomic, weak) id<UITableViewDataSourcePrefetching> prefetchDataSource NS_AVAILABLE_IOS(10_0);
奇怪的是UITableView并没有找到isPrefetchingEnabled属性的定义
13,iOS10 UIScrollView 新增 refreshControl 属性
UIScrollView新增了refreshControl属性
@property (nonatomic, strong, nullable) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(10_0) __TVOS_PROHIBITED;
这意味着UICollectionView和UITableView都支持refresh功能了。
我们也可以脱离UITableViewController使用UIRefreshControl了。
14, Swif3.0 新增作用域访问级别 fileprivate
目前有如下访问级别:
- 公开(public)
- 内部(internal)
- 文件外私有(fileprivate)
- 私有(private)
15,Swift3.0 允许关键字作为参数标签
Swift3.0开始我们将能使用除inout var let关键字作为参数标签
// Swift 3 calling with argument label: calculateRevenue(for sales: numberOfCopies, in .dollars) // Swift 3 declaring with argument label: calculateRevenue(for sales: Int, in currency: Currency) func touchesMatching(phase: NSTouchPhase, in view: NSView?) -> Set<NSTouch>
如果你坚持要使用inout var let关键字可以使用 `` 包裹参数标签
func addParameter(name: String, `inout`: Bool)
iOS 10 / Swift 3.0 / XCode 8 总结