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

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

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

Acbf3391de0494432a92221ffe89f34e?s=128

yohei sugigami

July 17, 2014
Tweet

Transcript

  1. 9.

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

    '() -> ()' does not conform to protocol 'AnyObject'
  2. 10.

    var closures: AnyObject = { () in println("Fuga") } ERROR

    Type '() -> ()' does not conform to protocol 'AnyObject'
  3. 11.

    typedef void (^Block)(); ! @interface HogeFuga (Wrapper) + (void) hogeUsingBlockWrapper:(Block)block;

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

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

    override var description: String! { get { return "Name = \(self.name), Note = \(self.note)" } } } ϓϩύςΟͷΦʔόʔϥΠυ͢Δ
  6. 17.

    Swift UIViewAnimationOptions.LayoutSubviews typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) { UIViewAnimationOptionLayoutSubviews = 1 <<

    0, UIViewAnimationOptionAllowUserInteraction = 1 << 1, … } ObjC UIViewAnimationOptionLayoutSubviews ڐ༰ൣғͳྫ
  7. 20.

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

    0 ! // => ERROR Cannot convert the expression's type '()' to type 'UIViewContentMode' Swiftͷܕ͸ݫ͍͠Ͱ͢Ͷ
  8. 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Ͱ͸εΧϥʔܕͷΩϟετ͸ͳ͍ ୅ΘΓʹίϯόʔδϣϯΛར༻
  9. 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ϥΠΫͳΩϟετ͸Ͱ͖ͳ͍
  10. 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)") } ิ଍
 ΦϒδΣΫτܕ͸ΩϟετՄೳ
  11. 25.

    for movie in someObjects as [Movie] { println("Movie: '\(movie.name)', dir.

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

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

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

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

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

    let u: NSUInteger = 1 ! // ERROR => Use

    of undeclared type 'NSUInteger'; did you mean to use 'Int'? NSUInteger ͸ར༻Ͱ͖·ͤΜ
  17. 39.

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

    RKObjectMappingOp… { return [mapping.objectClass new]; } ౉ͨ͠Classܕ͕ObjCଆͰnew͞ΕΔέʔε ʢྫɿ Restkit)
  18. 40.

    Swift͔ΒͲ͏͍͏ clazz Λ౉ͤ͹͍͍ͷ͔ class HogeModel: NSObject { } ! !

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

    let _SingletonSharedInstance = Singleton() ! class Singleton { class var

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

    class Singleton { class var sharedInstance : Singleton { struct

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