プロトコル拡張の話? #wwdc21cafe

34
EZNET 熊友宏 http://eznet.jp/ Swift 2.0 2015.07.07 @ WWDC2015 情報共有会 プロトコル拡張の話?

Upload: tomohiro-kumagai

Post on 06-Aug-2015

2.604 views

Category:

Technology


3 download

TRANSCRIPT

1. EZ-NET http://ez-net.jp/ Swift 2.0 2015.07.07 @ WWDC2015 2. EZ-NET http://ez-net.jp/ @es_kumagai Xcode 5 IP Phone with MOSA 3. #yidev iPhone EZ-NET http://ez-net.jp/ @es_kumagai 711 https://atnd.org/events/67499 4. 5. Swift 2.0 6. 7. protocol CollectionType : SequenceType { typealias Generator : GeneratorType typealias Index : ForwardIndexType subscript(index:Index) -> Generator.Element { get } var startIndex:Index { get } var endIndex:Index { get } } protocol GeneratorType { typealias Element } 8. 9. extension CollectionType { var count:Index.Distance { return distance( self.startIndex, self.endIndex) } } 10. let array = [Int]() let dictionary = [String:Int]() array.count dictionary.count 11. extension CollectionType where Generator.Element : IntegerType { var sum:Generator.Element { return self.reduce(0) { $0 + $1 } } } 12. let intArray = [Int]() let strArray = [String]() intArray.sum strArray.sum 13. 14. extension CollectionType where Index : Streamable, Generator.Element : IntegerType { func printIndexOf (element:Generator.Element, inout to stream:S) { self.indexOf(element)?.writeTo(&stream) } } 15. extension CollectionType where Generator.Element : IntegerType, Index.Distance == Generator.Element { var average:Generator.Element { return self.reduce(0) { $0 + $1 } / self.count } var count:Index.Distance { return distance( self.startIndex, self.endIndex) } } 16. extension CollectionType where Self : Equatable, Self : NillLiteralConvertible { var isNull:Bool { return self == nil } } 17. extension CollectionType where Generator.Element == String { var lastPathComponents:String { return self.map { $0.lastPathComponent } } } 18. 19. extension CollectionType { var isEmpty:Bool { return self.startIndex == self.endIndex } } struct MyCollection : CollectionType { var isEmpty:Bool { return reduce(true) { $0 && $1.isEmpty } } } 20. 21. 22. 23. 24. _ http://410319.jp/sozai/ 25. 2014 1 http://.com/namerank-3110 26. 27. 28. 29. 30. 31. 32. _ __ 33. 34.