Pro Yearly is on sale from $80 to $50! »

Swiftのアプリ開発でハマったこと

 Swiftのアプリ開発でハマったこと

XCode6 Beta でSwiftで開発したときにあったトラブルを紹介します

Acbf3391de0494432a92221ffe89f34e?s=128

yohei sugigami

July 17, 2014
Tweet

Transcript

  1. Swift LT @Wantedly 2014-07-17 yohei sugigami

  2. ਿ্༸ฏ εΪΨϛϤ΢ϔΠ  (JU)VC!TVTJFZZ ઌ݄+0*/ͨ͠͹͔Γͷ৽ถϓϩάϥϚʔ ! ! ! 8BOUFEMZͰ͸ߴ͍2JJUBྗ͕ٻΊΒΕΔ SwiftͰ̎Ґ

  3. ɾSwiftͷΞϓϦ։ൃͰϋϚͬͨ ɹ͜ͱ ̍̌ݸ ! ɾXcode6ͱSwiftͷΞϓϦ։ൃͰ ɹ౿Μͩ஍ཕͨͪ̔ݸ ΞδΣϯμ

  4. Xcode6Ͱ৽نͷΞϓϦΛ։ൃͨ࣌͠ʹؾ͍ͮͨ͜ͱ Λ·ͱΊ·͢ɻΞϓϦͷ࢓༷͸ҎԼͷͱ͓Γɻ ! - ։ൃݴޠ͸Swift - CocoapodsͰطଘObjcͷϥΠϒϥϦΛར༻ - σϓϩΠλʔήοτ͸iOS8Ҏ্ -

    StoryboardΛར༻ - AutolayoutͱSizeClassesΛར༻
  5. ։ൃ؀ڥ͸ҎԼͷͱ͓Γɻ ! - Xcode6͸Beta2, 3Λར༻ - ݕূ࣮ػ͸iOS8 Beta2, 3ΛΠϯετʔϧͷiPhone5 !

    Beta2Ͱ։ൃ͍ͯͨ͠ΒɺBeta3͕ग़·ͨ͠ ʘ(^o^)ʗ
  6. SwiftͷΞϓϦ։ൃͰ ϋϚͬͨ͜ͱ

  7. ҎԼͷ༷ͳObjCϝιουʹSwiftͷ ΫϩʔδϟΛ౉͢ํ๏ @interface HogeFuga : NSObject + (void) hogeUsingBlock:(id)bock; @end

  8. ObjC id = Swift AnyObject

  9. HogeFuga.hogeUsingBlock( { () -> () in println("Fuga") }) ERROR Type

    '() -> ()' does not conform to protocol 'AnyObject'
  10. var closures: AnyObject = { () in println("Fuga") } ERROR

    Type '() -> ()' does not conform to protocol 'AnyObject'
  11. typedef void (^Block)(); ! @interface HogeFuga (Wrapper) + (void) hogeUsingBlockWrapper:(Block)block;

    @end ! @implementation HogeFuga (Wrapper) + (void) hogeUsingBlockWrapper:(Block)block; { [HogeFuga hogeUsingBlock:block]; } @end ObjCͰܕΛ໌ࣔͨ͠ϒϩοΫΛ ࣋ͭϝιουͰϥοϓ͢Δ
  12. NSObject#descriptionΛΦʔόʔϥ Πυ͢Δ ᶄ

  13. class Hoge: NSObject { var name: String? var note: String?

    override func description() -> String { return "Name = \(self.name), Note = \(self.note)" } } ERROR Method does not override any method from its superclass
  14. class Hoge: NSObject { var name: String? var note: String?

    override var description: String! { get { return "Name = \(self.name), Note = \(self.note)" } } } ϓϩύςΟͷΦʔόʔϥΠυ͢Δ
  15. ObjCͷenum஋ΛSwiftه๏Ͱهࡌ ͢Δํ๏͕Θ͔Βͳ͍ ᶅ

  16. Swift UIViewAutoresizing.FlexibleLeftMargin typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) { UIViewAutoresizingNone = 0, UIViewAutoresizingFlexibleLeftMargin

    = 1 << 0, … } ObjC UIViewAutoresizingFlexibleLeftMargin Θ͔Γ΍͍͢ྫ
  17. Swift UIViewAnimationOptions.LayoutSubviews typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) { UIViewAnimationOptionLayoutSubviews = 1 <<

    0, UIViewAnimationOptionAllowUserInteraction = 1 << 1, … } ObjC UIViewAnimationOptionLayoutSubviews ڐ༰ൣғͳྫ
  18. Swift HogeStyle.FugaValue1 typedef NS_ENUM(NSInteger, HogeStyle) { HogeFugaDefault, HogeFugaValue1, HogeFugaValue2, HogeFugaSubtitle

    }; ObjC HogeFugaValue1 ෳࡶͳྫʢPrefix͕Ұக͍ͯ͠ͳ͍ʣ
  19. enumܕͷϝιουҾ਺΍ม਺ʹ enumͰఆٛ͞Εͨఆ਺Ҏ֎Λઃఆ ͢ΔͱΤϥʔʹͳΔ ᶆ

  20. typedef NS_ENUM(NSInteger, UIViewContentMode) { UIViewContentModeScaleToFill, ... } ! self.view.contentMode =

    0 ! // => ERROR Cannot convert the expression's type '()' to type 'UIViewContentMode' Swiftͷܕ͸ݫ͍͠Ͱ͢Ͷ
  21. εΧϥʔܕͷΩϟετ͸ίϯόʔ δϣϯΛ࢖͏ ᶇ

  22. let i: Int = Int(1.0) let n: NSInteger = NSInteger(1.0)

    ! let f: Float = Float(100) let d: Double = Double(100) let g: CGFloat = CGFloat(100) SwiftͰ͸εΧϥʔܕͷΩϟετ͸ͳ͍ ୅ΘΓʹίϯόʔδϣϯΛར༻
  23. let index: NSInteger = 1 let width: CGFloat = 100

    let r = width * (CGFloat)index ! // => ERROR Consecutive statements on a line must be separated by ';' ObjCϥΠΫͳΩϟετ͸Ͱ͖ͳ͍
  24. let someObjects: [AnyObject] = [ Movie(name: "2001: A Space", director:

    "Stan"), Movie(name: "Moon", director: "Duncan Jones"), Movie(name: "Alien", director: "Ridley Scott") ] ! for object in someObjects { let movie = object as Movie // Type Casting ! println("Movie: '\(movie.name)', dir. \(movie.director)") } ิ଍
 ΦϒδΣΫτܕ͸ΩϟετՄೳ
  25. for movie in someObjects as [Movie] { println("Movie: '\(movie.name)', dir.

    \(movie.director)") } ഑ྻΛΩϟετ͢ΔͱίϯύΫτʹॻ͚Δ
  26. CGFloatͱFloat·ͨ͸Doubleͷԋࢉ ᶈ

  27. import CoreGraphics ! typealias CGFloat = Float CoreGraphicsʹ͸CGFloat͸FloatͱΤΠϦΞ ε͞Ε͍ͯΔ

  28. let index: NSInteger = 1 let width: CGFloat = 100

    let r1 = width * Float(index) ! // => ERROR Could not find an overload for '*' that accepts the supplied arguments CGFloatͱFloatͷԋࢉ Ұݟਖ਼͘͠ಈ࡞ͦ͠͏͕ͩΤϥʔʹͳΔ
  29. ͳͥͳΒ iPhone5s(arm64)ΛλʔήοτʹϏ ϧυ͍ͯ͠Δ͔Β

  30. ͜ͷͱ͖CGFloat͸SwiftͷDouble ʹΤΠϦΞε͞ΕΔ

  31. let index: NSInteger = 1 let width: CGFloat = 100

    let r2 = width * Double(index) // => OK iPhone5s(arm64)ͷ৔߹͸Doubleͩͱίϯ ύΠϧͰ͖Δ
  32. let index: NSInteger = 1 let width: CGFloat = 100

    let r = width * CGFloat(index) // => OK CGFloatͱͷԋࢉ͸CGFloatʹίϯόʔ δϣϯ͠·͠ΐ͏
  33. ObjCͷNSIntegerͱSwiftͷIntͷԋ ࢉ ᶉ

  34. typealias NSInteger = Int ! ! let i: Int =

    1 let n: NSInteger = 1 let x: CGFloat = 3 let y: Float = 3 let w = i + Int(x) + Int(y) // => OK let q = n + Int(x) + Int(y) // => OK ! if n == i { println("HERE!") // => OK }
  35. let u: NSUInteger = 1 ! // ERROR => Use

    of undeclared type 'NSUInteger'; did you mean to use 'Int'? NSUInteger ͸ར༻Ͱ͖·ͤΜ
  36. ΫϩʔδϟܕϓϩύςΟͷΦϓ γϣφϧͳॻ͖ํ ᶊ

  37. class Fuga { var completion: ( () -> () )?

    } ؙׅހͰׅΓ·͢
  38. ObjCͷClassܕΛύϥϝʔλʹͱΔ ϝιουʹSwiftͷClassΛ౉͢ํ๏ ᶋ

  39. @interface RKObjectMapping : RKMapping { +(instancetype)mappingForClass:(Class)objectClass } ! ! @implementation

    RKObjectMappingOp… { return [mapping.objectClass new]; } ౉ͨ͠Classܕ͕ObjCଆͰnew͞ΕΔέʔε ʢྫɿ Restkit)
  40. Swift͔ΒͲ͏͍͏ clazz Λ౉ͤ͹͍͍ͷ͔ class HogeModel: NSObject { } ! !

    var clazz = ???
 let mapping: RKObjectMapping = RKObjectMapping(forClass: clazz)
  41. var clazz: AnyClass = NSClassFromString(“WTDHogeModel”) ! // ERROR => EXC_BAD_INSTRUNCTION

    ࣦഊᶃ NSClassFromStringͰ౉͢
  42. var clazz: AnyClass = object_getClass(WTDHogeModel()) ! ! // newͰERROR [mapping.objectClass

    new]; ࣦഊᶄ object_getClassͰ౉͢
  43. var clazz: NSObject.Type = WTDHogeModel.self
 let mapping: RKObjectMapping = RKObjectMapping(forClass:

    clazz) ! ! // ʹʼ OK ݁࿦ NSObject.TypeΛ౉͢
  44. γϯάϧτϯύλʔϯΛ࣮૷͢Δ ̏ͭͷํ๏ ᶌ

  45. ! ! ஗ԆॳظԽʢlazy initializationʣͱεϨου ηʔϑʢthread safetyʣ͕ߟྀ͞Ε࣮ͨ૷

  46. class Singleton { class var sharedInstance : Singleton { struct

    Static { static var onceToken : dispatch_once_t = 0 static var instance : Singleton? = nil } dispatch_once(&Static.onceToken) { Static.instance = Singleton() } return Static.instance! } } dispatch_onceͰఆٛ ObjCͱಉ༷ͷ࣮૷ํ๏
 ຖճ͜ΕΛॻ͘ʹͷ͸େม
  47. let _SingletonSharedInstance = Singleton() ! class Singleton { class var

    sharedInstance : Singleton { return _SingletonSharedInstance } } άϩʔόϧఆ਺Ͱఆٛ ࠓޙSwiftͷݴޠ࢓༷͕มߋ͞Εہॴతά ϩʔόϧม਺͕ఆٛͰ͖ΔΑ͏ʹͳΕ͹ɺ ͜ͷ࣮૷ํ๏͕΋ͬͱ΋γϯϓϧ
  48. class Singleton { class var sharedInstance : Singleton { struct

    Static { static let instance : Singleton = Singleton() } return Static.instance } } structͷstaticఆ਺Ͱఆٛ ʢਪ঑ʣ Swiftͷclass͸staticఆ਺Λར༻Ͱ͖ͳ͍ͷ ͰɺstructͰωετͯ͠staticఆ਺Λอ࣋
  49. Xcode6ͱSwift&iOS8 ͷΞϓϦ։ൃͰ౿Μ ͩ஍ཕͨͪ

  50. ίϯύΠϧΤϥʔ XXXHeaderϑΝΠϧ has been modified since the precompiled header ᶃ

  51. fatal error: file ‘…/UIKit.framework/ Headers /UIVisualEffectView.h’ has been modified since

    the precompiled header …
  52. ରԠํ๏ ຖճͳ͔ͥͳ͓ΔରԠํ๏͕·ͪ·ͪ ! - Xcode6࠶ىಈ - ϏϧυΛΫϦʔϯ͢Δ - DerivedDataΛ࡟আ͢Δ -

    OSXΛ࠶ىಈ͢Δ - Xcode6ΛೖΕͳ͓͢ orz
  53. ᶄ IBOutletͷStoryboardͱͷίωΫ γϣϯ͋Γ͕දࣔ͞Εͳ͍৔߹͕ ͋Δ

  54. ίϯύΠϧޙͷΞϓϦͷڍಈ͸ਖ਼͘͠઀ଓ ͞Ε͍ͯΔঢ়ଶͰಈ࡞͍ͯ͠·ͨ͠

  55. ᶅ StoryboardͷϑΝΠϧΛ։͍ͯ΋ XMLͰදࣔ͞ΕΔ

  56. XML…

  57. ໌ࣔతʹ։͖·͠ΐ͏

  58. ᶆ ιʔείʔυϋΠϥΠτͱೖྗิ ׬͕ޮ͔ͳ͘ͳΔ

  59. ͨ·ʹ෮׆΋͠·͆͢

  60. ᶇ γϛϡϨʔλʔͰΞϓϦΛىಈ͠ Α͏ͱ͢ΔͱΤϥʔʹͳΔ

  61. Xcode6ͱγϛϡϨʔλʔΛ࠶ىಈ ͨ͠Βͳ͓Γ·ͨ͠ɻ

  62. ᶈ Xcode5ͱXcode6ͷγϛϡϨʔλʔ Ͱಉ࣌ʹΞϓϦΛىಈͰ͖ͳ͍

  63. ͲͪΒ͔ͷγϛϡϨʔλʔΛऴྃ ͠·͠ΐ͏

  64. ᶉ switchͱॻ͘ͱXcode͕ϑϦʔζ͢ Δ

  65. Beta3Ͱ͸·ͩग़͍ͯͳ͍Ͱ͢

  66. ᶊ ޡͬͯϓϩδΣΫτΛXcode5Ͱ։ ͍ͯStroyboardΛ։͘ͱXcode6Ͱ Stroyboard͕͓͔͘͠ͳ͍ͬͯΔ

  67. Xcode5Ͱޡͬͯ։͍ͨΒԿ΍ΒΤϥʔ͕ ϑΝΠϧ͕ഁյ͞Ε·ͨ͠ɻ git reset ͳͲͰϑΝΠϧΛ໭͠·͠ΐ͏ɻ

  68. Finder͔ΒϓϩδΣΫτϑΝΠϧΛ μϒϧΫϦοΫͯ͠։͘ͱXcode5 Ͱ։͔ΕΔ͜ͱ΋͋Γ·͢ɻ

  69. Xcode5Λىಈ͢ΔͱɺXcode6Ͱ࡞ ۀ͍ͯͨ͠ϓϩδΣΫτΛউखʹ ։͘৔߹͕͋Γ·͢ɻ

  70. ༧๷ࡦ ! ̍. gitͳͲͰࡉ͔͘ίϛοτͯ͠ϩʔϧόοΫͰ͖Δ Α͏ʹ ! ̎. Xcode6ͰϓϩδΣΫτΛ։͍ͨΒετʔϦϘʔυ Λ࠷ऴฤूը໘ʹͯ͠ด͡ͳ͍ !

    ̏. Xcode6Λऴྃ͢Δͱ͖͸ϓϩδΣΫτΛด͔ͯ͡ Βऴྃ͢Δ ! ̐. Xcode5ΛࣺͯΔ͆
  71. iOS,Android Rails ΤϯδχΞ ืूத hiring@wantedly.com ڵຯ͕͋Δํ͸ͪ͜Β͔Β͝࿈བྷ͍ͩ͘͞ ৽͍ٕ͠ज़ʹͲΜͲΜ௅ઓ͍ͨ͠ ϞόΠϧΤϯδχΞɾ΢Υϯςουʂʂ ͪ͜Β͔ΒͰ΋OKͰ͢

  72. ͓ΘΓ