Swift 中級編

Swift 中級編

1.Objective-Cを愛する人のためのSwift
2.Swift小ネタ集

562e29ba057361b2b944bd7bbd274887?s=128

Yusei Nishiyama

June 25, 2014
Tweet

Transcript

  1. 14.
  2. 15.

    ࢖ͬͯΈΔ // ద౰ͳؔ਺Λએݴͯ͠... func someFunc() { return } ! //

    idܕ૬౰ͷΦϒδΣΫτΛએݴͯ͠... var anyObj : AnyObject ! // ίϯύΠϧ͸௨Δ͸ͣ... anyObj.someFunc()
  3. 18.

    υΩϡϝϯτͷαϯϓϧίʔυ var myObject: AnyObject myObject = NSDate() myObject.characterAtIndex(5) // crash,

    myObject does't respond to that method” ! // υΩϡϝϯτͰ͸࣮ߦ࣌ΤϥʔʹͳΔͱॻ͍ͯ͋Δɻ
  4. 19.

    ΋͏গ͠ௐ΂ͯΈΔ • You can also call any Objective-C method and

    access any property without casting to a more specific class type. This includes Objective-C compatible methods marked with the @objc attribute. • @objcଐੑ͕͍ͭͨɺObjective-Cޓ׵ͷϝ ιουͰͳ͍ͱ͍͚ͳ͍Β͍͠ɻ
  5. 20.

    ͞Βʹ… • When you define a Swift class that inherits

    from NSObject or any other Objective-C class, the class is automatically compatible with Objective-C. • Ͳ͏΍ΒObjective-CͷΫϥεΛܧঝͨ͠SwiftͷΫ ϥε͸ࣗಈతʹObjective-Cޓ׵ʹͳΔΒ͍͠ɻ • ͖ͬ͞ͷྫͰݺͼग़͍ͯͨ͠characterAtIndex͸ Objective-Cͷϝιου͔ͩΒಈతʹݺͼग़ͤͨɻ
  6. 21.

    ؾΛऔΓ௚ͯ͠ // Objective-Cޓ׵ͷΫϥεɻ @objc class SomeClass { func someFunc() {

    return } } ! // AnyObjectʹIntܕͷ஋Λ୅ೖɻ var anyObj : AnyObject = 1 ! // ࣮૷͞Ε͍ͯͳ͍ϝιουͷݺͼग़͠... anyObj.someFunc()
  7. 27.

    ิ଍:
 จࣈྻޓ׵ʹ͢Δϓϩτίϧ // Selector͸ҎԼͷϓϩτίϧʹదԠ͍ͯ͠ΔͷͰɺ // จࣈྻ͔ΒॳظԽͰ͖Δɻ protocol StringLiteralConvertible : ExtendedGraphemeClusterLiteralConvertible

    { typealias StringLiteralType class func convertFromStringLiteral(value: StringLiteralType) -> Self } ! protocol ExtendedGraphemeClusterLiteralConvertible { typealias ExtendedGraphemeClusterLiteralType class func convertFromExtendedGraphemeClusterLiteral(value: ExtendedGraphemeClusterLiteralType) -> Self }
  8. 28.

    Target-Actionύλʔϯ͸݈ࡏ class UIResponder : NSObject { . . . func

    canPerformAction(action: Selector, withSender sender: AnyObject!) -> Bool // Allows an action to be forwarded to another target. By default checks - canPerformAction:withSender: to either return self, or go up the responder chain. func targetForAction(action: Selector, withSender sender: AnyObject!) -> AnyObject! . . . }
  9. 36.

    HaskellɺMLͷΑ͏ʹσϑΥϧτ ͰΧϦʔԽ͞ΕΔΘ͚Ͱ͸ͳ͍ func addTwoNumbers(a: Int, b: Int) -> Int {

    return a + b } ! var partial = addTwoNumbers(4) ! // ίϯύΠϧΤϥʔ
  10. 37.

    ͜͏ॻ͚͹෦෼ద༻Ͱ͖Δʂ func addTwoNumbers(a: Int)(b: Int) -> Int { return a

    + b } ! var partial = addTwoNumbers(4) // => (b: Int) -> Int ! var reuslt = partial(b: 5) // => 9
  11. 38.

    Preprocessor Directive • in Swift you use a global constant

    instead.
 
 ϓϦϓϩηοαͰ͸ͳ͘ɺάϩʔόϧఆ਺Λ࢖͏ɻ • Complex macros are used in C and Objective-C but have no counterpart in Swift.
 
 ؔ਺ܗࣜͷϚΫϩʹ͋ͨΔΑ͏ͳ΋ͷ͸ଘࡏ͠ͳ ͍ɻ
  12. 40.

    ૢ࡞ʴલஔࢺελΠϧ͸
 ౿ऻ͞ΕΔ໛༷ class myCounter { func incrementBy(amout: Int, numberOfTimes: Int)

    { // increment } } ! let counter = myCounter() ! // σϑΥϧτͰ͸ɺୈҰҾ਺ͷ֎෦Ҿ਺໊͸লུ͞ΕΔɻ counter.incrementBy(1, numberOfTimes:1)
  13. 41.

    ୈೋҾ਺Ҏ߱΋
 Ҿ਺໊Λলུ͍ͨ͠৔߹ class myCounter { // ΞϯμʔείΞͰΦʔόʔϥΠυͯ͠͠·͏ɻ func incrementBy(amout: Int,

    _ numberOfTimes: Int) { // increment } } ! let counter = myCounter() ! counter.incrementBy(1, 1)
  14. 43.

    ݺͼग़͠ݩʹؼͬͯ͜ͳ͍
 ؔ਺ͷએݴ ! @noreturn func tryMe() { exit(EXIT_FAILURE) } !

    // ΋ͪΖΜexitࣗମ΋@noreturn @noreturn func exit(_: CInt)
  15. 44.