Last active
June 11, 2018 23:33
-
-
Save hanawat/1cac1983697a5f48a9496a433abcd70e to your computer and use it in GitHub Desktop.
What’s New in Swift4.2 まとめ ref: https://qiita.com/hanawat/items/4605f9e357c8794b58d7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Apple Swift version 4.2 (swiftlang-1000.0.16.7 clang-1000.10.25.3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swift4.1 | |
protocol Bachelor { | |
func givesRose() | |
} | |
extension Array: Bachelor where Element: Bachelor { | |
func givesRose() { forEach { $0.givesRose() } } | |
} | |
class Man: Bachelor { | |
func givesRose() { print("🌹") } | |
} | |
let men = [Man(), Man()] | |
men.givesRose() // 🌹🌹 | |
// Swift4.2 | |
func dynamicallyBachelor(_ men: Any) { | |
if let men = men as? Bachelor { | |
men.givesRose() | |
} else { | |
fatalError("Swift4.1") | |
} | |
} | |
dynamicallyBachelor(men) // 🌹🌹 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let randomChar = "hello".randomElement() // Random character | |
let randomFruit = ["apple", "orange", "banana"].randomElement() // Random string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let shuffledChar = "hello".shuffled() // Shuffled character | |
var fruits = ["apple", "orange", "banana"] | |
fruits.shuffle() // Shuffled array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Mimicking the old random function. | |
struct OldRandomNumberGenerator: RandomNumberGenerator { | |
mutating func next() -> UInt64 { | |
return UInt64(arc4random_uniform(.max)) | |
} | |
} | |
var oldGenerator = OldRandomNumberGenerator() | |
let oldRandomInt = Int.random(in: 0...10, using: &generator) // 1-10 random number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let range = 0...9 | |
let isEven: (Int) -> Bool = { $0 % 2 == 0 } | |
let lastEven = range.last(where: isEven) // 8 | |
let lastEvenIndex = range.lastIndex(where: isEven) // inRange(8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swift4.1 | |
struct GridPoint { | |
var x: Int | |
var y: Int | |
} | |
extension GridPoint: Hashable { | |
var hashValue: Int { | |
return x.hashValue ^ y.hashValue &* 16777619 | |
} | |
static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { | |
return lhs.x == rhs.x && lhs.y == rhs.y | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// swift4.2 | |
struct GridPoint { | |
var x: Int | |
var y: Int | |
} | |
extension GridPoint: Hashable { | |
func hash(into hasher: inout Hasher) { | |
hasher.combine(x) | |
hasher.combine(y) | |
} | |
static func == (lhs: GridPoint, rhs: GridPoint) -> Bool { | |
return lhs.x == rhs.x && lhs.y == rhs.y | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let range = 0...9 | |
// Swift4.1 | |
let isSmallerThanTen = !range.contains(where: { $0 >= 10 }) // true | |
// Swift4.2 | |
let isSmallerThanTen = range.allSatisfy({ $0 < 10 }) // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct GridPoint { | |
var x: Int // Offset: 0 | |
var y: Int // Offset: 8 | |
} | |
let offset = MemoryLayout<GridPoint>.offset(of: \GridPoint.y) // 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Fruit: CaseIterable { | |
case apple | |
case orange | |
case banana | |
} | |
Fruit.allCases // [apple, orange, banana] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// error: type 'Fruit' does not conform to protocol 'CaseIterable' | |
enum Fruit: CaseIterable { | |
case apple(Int) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dynamicMemberLookup | |
struct Response { | |
private var raw = ["foo": "foo", "baz": "baz"] | |
subscript(dynamicMember name: String) -> String? { | |
get { return raw[name] } | |
set { raw[name] = newValue } | |
} | |
} | |
var response = Response() | |
response.foo // foo | |
response.bar // nil | |
response.bar = "bar" | |
response.bar // bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func doSomething() { | |
#warning("TODO: Implement logic.") | |
} | |
if canImport(UIKit) | |
// Do something. | |
#else | |
#error("Requires UIKit.") | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var faces = ["😀", "😭", "😄"] | |
faces.removeAll(where: { $0 == "😭" }) // ["😀", "😄"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x: UInt16 = 0xabcd | |
let (firstByte, secondByte) = withUnsafeBytes(of: x, { pointer in | |
return (pointer[0], pointer[1]) | |
}) | |
String(firstByte, radix: 16) // cd | |
String(secondByte, radix: 16) // ab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var flag = false | |
// Swift4.1 | |
// flag = !flag | |
// Swift4.2 | |
flag.toggle() // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let randomInt = Int.random(in: 1...10) // 1-10 random number | |
let randomInt32 = Int32.random(in: .min ... .max) // Int.min-.max random number | |
let randomDouble = Double.random(in: 0.0..<1.0) // 0.0-1.0 random number | |
let randomBool = Bool.random() // true or false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment