/*
*/


/**
*/
/* @es_kumagai */












class Item {
// 戻り値を使わないと警告される
@warn_unused_result
func makeFilter() -> Filter {…}
// 戻り値を使わなくても警告されない
func apply(filter: Filter) -> Item {…}
}


class Item {
// 戻り値を使わないと警告される
func makeFilter() -> Filter {…}
// 戻り値を使わなくても警告されない
@discardableResult
func apply(filter: Filter) -> Item {…}
}


/// MutatingCounterpart: xxxx
/// MonmutatingCounterpart: xxxx
func makeFilter() -> Filter {…}


func add<T: FloatingPoint>(lhs: T, rhs: T) -> T {
return lhs + rhs
}
enum Item {
case binary(NSData)
}
let data1: NSMutableData = …
let data2: NSData = …
let item = Item.binary(data1)
data1.appendData(data2)
enum Item {
case binary(Data)
}
var data1: Data = …
let data2: Data = …
let item = Item.binary(data1)
data1.appendData(data2)
Advance to the next element and return it, or `nil` if no
next element exists.Once `nil` has been returned, all
subsequent calls return `nil`.
protocol IteratorProtocol {
associatedtype Element
mutating func next() -> Element?
protocol BooleanType {
var boolValue: Bool { get }
}
func remove<T:BooleanType>(recursively flag: T) {
…
}
func isKindOf<R:BooleanType>(type: AnyType) -> R {
…
}
struct Condition : BooleanType {
…
}
let condition = Condition(…)
if condition {
…
}
!, &&, ||
func && (lhs: Bool, rhs: () -> Bool) -> Bool
func || (lhs: Bool, rhs: () -> Bool) -> Bool
func ! (a: Bool) -> Bool
// 定義では `` が必要
enum DrawingStyle {
case `default`
case `repeat`
case fit
}
// 使用時は `` が不要
let style = DrawingStyle.default
// 定義では `` が必要
class Object {
var `default`: Int
func `repeat`(_ times: Int) -> String { … }
}
// 使用時は `` が不要
let object = Object()
let value = object.default
// 定義では `` が必要
enum Evaluate {
case `self`
case `dynamicType`
}
// 使用時も `` が必要
let eval = Evaluate.`self`
// プロトコル型で使う
let p: A & B = Value()
// 型引数で使う
func doSomething<T: A & B>(_ value: T) {
}
struct Value : Equatable {
static func == (lhs: Value, rhs: Value) -> Bool {
}
}
class Base : Equatable {
func isEqual(to rhs: Base) -> Bool {…}
}
class Sub : Base {
override func isEqual(to rhs: Base) -> Bool {…}
}
func == (lhs: Base, rhs: Base) -> Bool {
return lhs.isEqual(to: rhs)
}
class Base : Equatable {
class func == (lhs: Base, rhs: Base) -> Bool {…}
}
class Sub : Base {
override
class func == (lhs: Base, rhs: Base) -> Bool {…}
}
func makeIncrementer(inout value: Int) -> () -> Void {
return { value += 1; print("Inside:", value) }
}
var value = 1
let incrementer = makeIncrementer(&value)
print("Outside:", value) // Outside: 1
incrementer() // Inside: 2
print("Outside:", value) // Outside: 1
incrementer() // Inside: 3
print("Outside:", value) // Outside: 1
func something(value: inout Int) {
let noescaped: @noescape () -> Void = {
value = 10
}
let escaped: () -> Void = { [value] in
print(value)
}
}


func total(price: Int, count: Int) -> Int {
}
// 引数リストを丸ごとタプルで扱える
let item: (Int, count: Int) = (100, 5)
total(item)
func total(price: Int, count: Int) -> Int {
}
// 原則、引数リストを1つのタプルで表現できない
let item: (Int, count: Int) = (100, 5)
total(item)
func apply<T, R>(value: T, f: (T) -> R) -> R {
return f(value)
}
let item: (Int, count: Int) = (100, 5)
func total(price: Int, count: Int) -> Int { … }
apply(value: item, f: total)
switch (value1, value2) {
case let (value?, nil), let (nil, value?):
return value
case let (value1?, value2?):
return value1 + value2
case (nil, nil):
return 0
}
switch device {
case
let .iPhone(_, osVersion, network, _)
where osVersion > 8.0,
let .iPad(_, osVersion, network, _, true)
where network == .cellular,
doSomething(device, osVersion, network)
case .iPodTouch:
doSomething(device)
default:
doSomething()
}
let values = sequence(first: 1) { $0 * 2 }
struct MutableSlice<Base : MutableIndexable> {
var base: Base { get }
}
Enjoy! Swift
/* Thank you */

Swift 3.0 で変わったところ - 厳選 13 項目 #love_swift #cswift

  • 1.
  • 2.
  • 3.
  • 11.
  • 12.
  • 14.
  • 23.
    
 class Item { //戻り値を使わないと警告される @warn_unused_result func makeFilter() -> Filter {…} // 戻り値を使わなくても警告されない func apply(filter: Filter) -> Item {…} }
  • 24.
    
 class Item { //戻り値を使わないと警告される func makeFilter() -> Filter {…} // 戻り値を使わなくても警告されない @discardableResult func apply(filter: Filter) -> Item {…} }
  • 25.
    
 /// MutatingCounterpart: xxxx ///MonmutatingCounterpart: xxxx func makeFilter() -> Filter {…}
  • 29.
  • 31.
    func add<T: FloatingPoint>(lhs:T, rhs: T) -> T { return lhs + rhs }
  • 34.
    enum Item { casebinary(NSData) } let data1: NSMutableData = … let data2: NSData = … let item = Item.binary(data1) data1.appendData(data2)
  • 35.
    enum Item { casebinary(Data) } var data1: Data = … let data2: Data = … let item = Item.binary(data1) data1.appendData(data2)
  • 39.
    Advance to thenext element and return it, or `nil` if no next element exists.Once `nil` has been returned, all subsequent calls return `nil`. protocol IteratorProtocol { associatedtype Element mutating func next() -> Element?
  • 42.
    protocol BooleanType { varboolValue: Bool { get } }
  • 43.
    func remove<T:BooleanType>(recursively flag:T) { … } func isKindOf<R:BooleanType>(type: AnyType) -> R { … }
  • 44.
    struct Condition :BooleanType { … } let condition = Condition(…) if condition { … }
  • 45.
    !, &&, || func&& (lhs: Bool, rhs: () -> Bool) -> Bool func || (lhs: Bool, rhs: () -> Bool) -> Bool func ! (a: Bool) -> Bool
  • 49.
    // 定義では ``が必要 enum DrawingStyle { case `default` case `repeat` case fit } // 使用時は `` が不要 let style = DrawingStyle.default
  • 50.
    // 定義では ``が必要 class Object { var `default`: Int func `repeat`(_ times: Int) -> String { … } } // 使用時は `` が不要 let object = Object() let value = object.default
  • 51.
    // 定義では ``が必要 enum Evaluate { case `self` case `dynamicType` } // 使用時も `` が必要 let eval = Evaluate.`self`
  • 55.
    // プロトコル型で使う let p:A & B = Value() // 型引数で使う func doSomething<T: A & B>(_ value: T) { }
  • 59.
    struct Value :Equatable { static func == (lhs: Value, rhs: Value) -> Bool { } }
  • 60.
    class Base :Equatable { func isEqual(to rhs: Base) -> Bool {…} } class Sub : Base { override func isEqual(to rhs: Base) -> Bool {…} } func == (lhs: Base, rhs: Base) -> Bool { return lhs.isEqual(to: rhs) }
  • 61.
    class Base :Equatable { class func == (lhs: Base, rhs: Base) -> Bool {…} } class Sub : Base { override class func == (lhs: Base, rhs: Base) -> Bool {…} }
  • 64.
    func makeIncrementer(inout value:Int) -> () -> Void { return { value += 1; print("Inside:", value) } } var value = 1 let incrementer = makeIncrementer(&value) print("Outside:", value) // Outside: 1 incrementer() // Inside: 2 print("Outside:", value) // Outside: 1 incrementer() // Inside: 3 print("Outside:", value) // Outside: 1
  • 65.
    func something(value: inoutInt) { let noescaped: @noescape () -> Void = { value = 10 } let escaped: () -> Void = { [value] in print(value) } } 

  • 69.
    func total(price: Int,count: Int) -> Int { } // 引数リストを丸ごとタプルで扱える let item: (Int, count: Int) = (100, 5) total(item)
  • 70.
    func total(price: Int,count: Int) -> Int { } // 原則、引数リストを1つのタプルで表現できない let item: (Int, count: Int) = (100, 5) total(item)
  • 71.
    func apply<T, R>(value:T, f: (T) -> R) -> R { return f(value) } let item: (Int, count: Int) = (100, 5) func total(price: Int, count: Int) -> Int { … } apply(value: item, f: total)
  • 75.
    switch (value1, value2){ case let (value?, nil), let (nil, value?): return value case let (value1?, value2?): return value1 + value2 case (nil, nil): return 0 }
  • 76.
    switch device { case let.iPhone(_, osVersion, network, _) where osVersion > 8.0, let .iPad(_, osVersion, network, _, true) where network == .cellular, doSomething(device, osVersion, network) case .iPodTouch: doSomething(device) default: doSomething() }
  • 79.
    let values =sequence(first: 1) { $0 * 2 }
  • 83.
    struct MutableSlice<Base :MutableIndexable> { var base: Base { get } }
  • 93.