Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift 中級編

Swift 中級編

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

Yusei Nishiyama

June 25, 2014
Tweet

More Decks by Yusei Nishiyama

Other Decks in Technology

Transcript

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

    idܕ૬౰ͷΦϒδΣΫτΛએݴͯ͠... var anyObj : AnyObject ! // ίϯύΠϧ͸௨Δ͸ͣ... anyObj.someFunc()
  2. υΩϡϝϯτͷαϯϓϧίʔυ var myObject: AnyObject myObject = NSDate() myObject.characterAtIndex(5) // crash,

    myObject does't respond to that method” ! // υΩϡϝϯτͰ͸࣮ߦ࣌ΤϥʔʹͳΔͱॻ͍ͯ͋Δɻ
  3. ΋͏গ͠ௐ΂ͯΈΔ • 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ޓ׵ͷϝ ιουͰͳ͍ͱ͍͚ͳ͍Β͍͠ɻ
  4. ͞Βʹ… • 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ͷϝιου͔ͩΒಈతʹݺͼग़ͤͨɻ
  5. ؾΛऔΓ௚ͯ͠ // Objective-Cޓ׵ͷΫϥεɻ @objc class SomeClass { func someFunc() {

    return } } ! // AnyObjectʹIntܕͷ஋Λ୅ೖɻ var anyObj : AnyObject = 1 ! // ࣮૷͞Ε͍ͯͳ͍ϝιουͷݺͼग़͠... anyObj.someFunc()
  6. ิ଍:
 จࣈྻޓ׵ʹ͢Δϓϩτίϧ // Selector͸ҎԼͷϓϩτίϧʹదԠ͍ͯ͠ΔͷͰɺ // จࣈྻ͔ΒॳظԽͰ͖Δɻ protocol StringLiteralConvertible : ExtendedGraphemeClusterLiteralConvertible

    { typealias StringLiteralType class func convertFromStringLiteral(value: StringLiteralType) -> Self } ! protocol ExtendedGraphemeClusterLiteralConvertible { typealias ExtendedGraphemeClusterLiteralType class func convertFromExtendedGraphemeClusterLiteral(value: ExtendedGraphemeClusterLiteralType) -> Self }
  7. 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! . . . }
  8. HaskellɺMLͷΑ͏ʹσϑΥϧτ ͰΧϦʔԽ͞ΕΔΘ͚Ͱ͸ͳ͍ func addTwoNumbers(a: Int, b: Int) -> Int {

    return a + b } ! var partial = addTwoNumbers(4) ! // ίϯύΠϧΤϥʔ
  9. ͜͏ॻ͚͹෦෼ద༻Ͱ͖Δʂ func addTwoNumbers(a: Int)(b: Int) -> Int { return a

    + b } ! var partial = addTwoNumbers(4) // => (b: Int) -> Int ! var reuslt = partial(b: 5) // => 9
  10. 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.
 
 ؔ਺ܗࣜͷϚΫϩʹ͋ͨΔΑ͏ͳ΋ͷ͸ଘࡏ͠ͳ ͍ɻ
  11. ૢ࡞ʴલஔࢺελΠϧ͸
 ౿ऻ͞ΕΔ໛༷ class myCounter { func incrementBy(amout: Int, numberOfTimes: Int)

    { // increment } } ! let counter = myCounter() ! // σϑΥϧτͰ͸ɺୈҰҾ਺ͷ֎෦Ҿ਺໊͸লུ͞ΕΔɻ counter.incrementBy(1, numberOfTimes:1)
  12. ୈೋҾ਺Ҏ߱΋
 Ҿ਺໊Λলུ͍ͨ͠৔߹ class myCounter { // ΞϯμʔείΞͰΦʔόʔϥΠυͯ͠͠·͏ɻ func incrementBy(amout: Int,

    _ numberOfTimes: Int) { // increment } } ! let counter = myCounter() ! counter.incrementBy(1, 1)
  13. ݺͼग़͠ݩʹؼͬͯ͜ͳ͍
 ؔ਺ͷએݴ ! @noreturn func tryMe() { exit(EXIT_FAILURE) } !

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