Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

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

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

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

yohei sugigami

July 17, 2014
Tweet

More Decks by yohei sugigami

Other Decks in Technology

Transcript

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

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

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

    @end ! @implementation HogeFuga (Wrapper) + (void) hogeUsingBlockWrapper:(Block)block; { [HogeFuga hogeUsingBlock:block]; } @end ObjCͰܕΛ໌ࣔͨ͠ϒϩοΫΛ ࣋ͭϝιουͰϥοϓ͢Δ
  4. 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
  5. class Hoge: NSObject { var name: String? var note: String?

    override var description: String! { get { return "Name = \(self.name), Note = \(self.note)" } } } ϓϩύςΟͷΦʔόʔϥΠυ͢Δ
  6. Swift UIViewAnimationOptions.LayoutSubviews typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) { UIViewAnimationOptionLayoutSubviews = 1 <<

    0, UIViewAnimationOptionAllowUserInteraction = 1 << 1, … } ObjC UIViewAnimationOptionLayoutSubviews ڐ༰ൣғͳྫ
  7. typedef NS_ENUM(NSInteger, UIViewContentMode) { UIViewContentModeScaleToFill, ... } ! self.view.contentMode =

    0 ! // => ERROR Cannot convert the expression's type '()' to type 'UIViewContentMode' Swiftͷܕ͸ݫ͍͠Ͱ͢Ͷ
  8. 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Ͱ͸εΧϥʔܕͷΩϟετ͸ͳ͍ ୅ΘΓʹίϯόʔδϣϯΛར༻
  9. let index: NSInteger = 1 let width: CGFloat = 100

    let r = width * (CGFloat)index ! // => ERROR Consecutive statements on a line must be separated by ';' ObjCϥΠΫͳΩϟετ͸Ͱ͖ͳ͍
  10. 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)") } ิ଍
 ΦϒδΣΫτܕ͸ΩϟετՄೳ
  11. for movie in someObjects as [Movie] { println("Movie: '\(movie.name)', dir.

    \(movie.director)") } ഑ྻΛΩϟετ͢ΔͱίϯύΫτʹॻ͚Δ
  12. 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ͷԋࢉ Ұݟਖ਼͘͠ಈ࡞ͦ͠͏͕ͩΤϥʔʹͳΔ
  13. let index: NSInteger = 1 let width: CGFloat = 100

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

    let r = width * CGFloat(index) // => OK CGFloatͱͷԋࢉ͸CGFloatʹίϯόʔ δϣϯ͠·͠ΐ͏
  15. 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 }
  16. let u: NSUInteger = 1 ! // ERROR => Use

    of undeclared type 'NSUInteger'; did you mean to use 'Int'? NSUInteger ͸ར༻Ͱ͖·ͤΜ
  17. @interface RKObjectMapping : RKMapping { +(instancetype)mappingForClass:(Class)objectClass } ! ! @implementation

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

    var clazz = ???
 let mapping: RKObjectMapping = RKObjectMapping(forClass: clazz)
  19. 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ͱಉ༷ͷ࣮૷ํ๏
 ຖճ͜ΕΛॻ͘ʹͷ͸େม
  20. let _SingletonSharedInstance = Singleton() ! class Singleton { class var

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

    Static { static let instance : Singleton = Singleton() } return Static.instance } } structͷstaticఆ਺Ͱఆٛ ʢਪ঑ʣ Swiftͷclass͸staticఆ਺Λར༻Ͱ͖ͳ͍ͷ ͰɺstructͰωετͯ͠staticఆ਺Λอ࣋