Professional Documents
Culture Documents
Session 244
© 2017 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
Frequently Occurring
II I
II I
$
II I
$
II I
? $
¢ ?
II measure! I
? $
¢ ?
measure!
III Infrequently Occurring
IV
•
Improvements in Foundation
•
Improvements in Foundation
•
Improvements in Foundation
•
Improvements in Foundation
•
NSCalendar
Improvements in Foundation
NSCalendar
NSCalendar
NSCalendar
How can I improve my code to work better and safer with it?
@implementation Container {
NSMutableArray<Item *> *_elements;
}
- (NSArray<Item *> *)elements {
return [_elements copy];
}
@end
Copy on Write Collections
There is a CoW level
How can I improve my code to work better and safer with it?
@implementation Container {
NSMutableArray<Item *> *_elements;
}
- (NSArray<Item *> *)elements {
return [_elements copy];
}
@end
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
🐄 []
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
🐄 []
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
🐄 []
a
b = [a copy]
b
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
🐄 []
a
b = [a copy]
🐄 []
b
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
🐄 []
a
b = [a copy]
🐄 []
b
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
🐄 []
a
b = [a copy]
🐄 []
b
[a addObject:@"A"]
b
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
🐄 []
a
b = [a copy]
🐄 []
b
[a addObject:@"A"]
a
🐄 [A]
b
🐄 []
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
🐄 []
a
b = [a copy]
🐄 []
b
[a addObject:@"A"]
a
🐄 [A]
b
🐄 []
Copy on Write Collections
Let’s milk this joke a bit more
a = [NSMutableArray new] a
🐄 []
a
b = [a copy]
🐄 []
b
[a addObject:@"A"]
a
🐄 [A]
b
🐄 []
//Leveraging Copy-on-write, Steer your code in the right direction
// WARNING: Don’t pass any NSMutableArrays into here
@property (strong) NSArray<Item *> *items;
//Leveraging Copy-on-write, Steer your code in the right direction
// WARNING: Don’t pass any NSMutableArrays into here
@property (strong) NSArray<Item *> *items;
//Leveraging Copy-on-write, Steer your code in the right direction
// WARNING: Don’t pass any NSMutableArrays into here
@property (strong) NSArray<Item *> *items;
18 Swift 3
16 Swift 4
14
Nanoseconds
12
10
8
6
4
2
0
Samples
// Leveraging Data, Don’t Believe the Lore
// Leveraging Data, Don’t Believe the Lore
Toll-free bridging
Bridges and How They Impact Your App
For whom the bridge tolls
Toll-free bridging
• From a CF type to a NS type
Bridges and How They Impact Your App
For whom the bridge tolls
Toll-free bridging
• From a CF type to a NS type
Toll-free bridging
• From a CF type to a NS type
Toll-free bridging
• From a CF type to a NS type
struct Data {
…
public init(referencing reference: NSData) {
_backing = _DataStorage(immutableReference: reference.copy() as! NSData)
_sliceRange = 0..<reference.length
}
…
}
// Swift Bridging
II measure! I
? $
¢ ?
measure!
III Infrequently Occurring
IV
Runs Frequently
II measure! I
? $
¢ ?
measure!
III Runs Infrequently
IV
•
String bridging
•
Ranges
•
String bridging
•
Ranges
•
// Swift
var text = label.text
String Bridging
Example 1: UILabel
// Objective-C - UIKit
// Swift
var text = label.text
@interface UILabel : UIView
// Objective-C - UIKit
// Swift
var text = label.text
@interface UILabel : UIView
bridge
String Bridging
Example 1: UILabel
Swift Framework
Swift Framework
¢
var text = label.text
II I
// Swift
var text = textView.textStorage.string
String Bridging
Example 2: NSTextStorage
// Swift
var text = textView.textStorage.string
String Bridging
Example 2: NSTextStorage
// Objective-C - UIKit
// Swift
var text = textView.textStorage.string
// Objective-C - Foundation
@interface NSMutableAttributedString :
NSAttributedString
// Objective-C - UIKit
// Swift
var text = textView.textStorage.string
// Objective-C - Foundation
@interface NSMutableAttributedString :
NSAttributedString
Swift Framework
Swift Framework
$
var text = textView.textStorage.string
II I
1 MB?
💵💵 💵
💵💵
💵💵
var text $
= textView.textStorage.string
II I
1 MB?
1 GB?
III Runs Infrequently
IV
•
String bridging
•
Ranges
•
Unicode
0x1F926 0x1F3FB 0x200D 0x2640 0xFE0F
Scalar Value
🤦 🏻 ♀
Visible
Components
Unicode
0x1F926 0x1F3FB 0x200D 0x2640 0xFE0F
Scalar Value
Unicode
0x1F926 0x1F3FB 0x200D 0x2640 0xFE0F
Scalar Value
“What a 🎉💩!”
“What a 🎉💩!”
“What a 🎉💩!”
“What a 🎉💩!”
“What a 🎉💩!”
<html>
<body>
<div>
<span>Hello</span> <b>Swift<span>test</span></b>
</div>
</body>
</html>
// String Ranges: Working with NSRegularExpression
extension String {
func rangeFromNSRange(nsRange : NSRange) -> Range<Index>? {
guard nsRange.location != NSNotFound else { return nil }
let from16 = utf16.startIndex.advanced(by: nsRange.location)
let to16 = from16.advanced(by: nsRange.length)
if let from = Index(from16, within: self),
let to = Index(to16, within: self) {
return from..<to
}
return nil
}
}
// Improved String Ranges: Working with NSRegularExpression NEW
import Foundation
import Foundation
String bridging
•
Ranges
•
Font Leading Pattern Fill LTR Font Smoothing Locale Flippedness Fonts Margin
Tribune Tower
Near North Side
Union Park
West Loop
Rookery Building
Loop
Water Tower
River North
Hyde Park
Burnham Park
Museum Campus
Union Station
Loop
Millennium Park
Loop
Tribune Tower
Near North Side
Union Park
West Loop
Rookery Building
Loop
Water Tower
River North
Hyde Park
Burnham Park
Museum Campus
Union Station
Loop
⻓长城
北北京
紫禁城
北北京
天安⻔门⼴广场
北北京
颐和园
北北京
天坛
北北京
⼤大同
⻁虎跳峡
丽江
⻰龙⻔门⽯石窟
洛洛阳
Runs Frequently
II I
II measure! I
?
3x
faster rendering with
NSTextField in macOS 10.13
What You Can Do
Higher-level strategies
// Only do this if you’re absolutely sure your text doesn’t have mixed writing directions
var myParagraphStyle = NSMutableParagraphStyle()
myParagraphStyle.baseWritingDirection = .leftToRight
myParagraphStyle.alignment = .left
What You Can Do
Lower-level tips
II measure! I
? $
¢ ?
measure!
III Infrequently Occurring
IV
More Information
https://developer.apple.com/wwdc17/244
Related Sessions