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以下:app


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: 可经过以下方法设置:ide

Edit Scheme-> Run -> Arguments, 
在Environment Variables里边添加
OS_ACTIVITY_MODE = Disable

3,iOS10 layoutIfNeed

iOS10 在一个控件上调用layoutIfNeed是只会单独计算约束,它所约束的控件不会生效,想要达到以前的效果须要在父级控件上调用layoutIfNeedpost

4, NSDate

Swift3.0会将oc的NSDate转为Data类型,有些操做NSDate的第三方库会闪退性能

5, Notification

Swift3.0字符串类型的通知常量被定义为structfetch

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().ui

var array = ["A","B","C","D"]

for i in array.indices.reversed() {

    print("\(i)")
}

输出:3 2 1 0

8, Range新增至四种类型

Range
CountableRange
ClosedRange
CountableClosedRange

不一样的表达式会生成不一样的Rangethis

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(_:)方法被移除,这些操做被移动到Collectionatom

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)
相关文章
相关标签/搜索