2016-04-12 14 views
1

以下のコードで、AlamofireとSwiftyjsonのおかげで、サーバーにHTTPリクエストを発行してJSONオブジェクトを取得できます。Alamofire/Swiftyjson - JSONタイプをObjcプロトコルデリゲートに渡す

しかし、SwiftyjsonのカスタムクラスJSONを代理メソッドの引数として渡すことができません。

このエラーを修正するにはどうすればよいですか?エラーと

コード行:

optional func didReceiveUserInfo(userInfo: JSON) //This the line I get the error 

エラー:パラメータのタイプはここでのObjective-C

内で表現することができないので メソッド@objcプロトコルのメンバーになることはできませんがいっぱいですコードを使用しています:

import UIKit 
import Alamofire 
import SwiftyJSON 

@objc protocol UserModelDelegate { 
    optional func didReceiveUserInfo(userInfo: JSON) //This is the line I get the error 
} 

class UserModel: NSObject, NSURLSessionDelegate { 

    // Instantiate the delegate 
    var delegate: UserModelDelegate? 

    override init() { 
     super.init() 
    } 

    func getUserInfo(token: String) { 

     let url = "http://test.app/api/userInfo" 

     let headers = [ 
      "Authorization":"Bearer \(token)", 
      "Content-Type": "application/x-www-form-urlencoded" 
     ] 

     Alamofire.request(.GET, url, headers: headers).responseJSON { response in 

      switch response.result { 

       case .Success(let data): 
        let json = JSON(data) 
        self.delegate?.didReceiveUserInfo?(json) //This is where I pass the JSON custom class type as an argument 

       case .Failure(let error): 
       print("Request failed with error: \(error)") 
      } 
     } 
    } 
} 

答えて

0

は、コードの作業です:

import UIKit 
import Alamofire 
import SwiftyJSON 

protocol UserModelDelegate { 

    func didReceiveUserInfo(userInfo: JSON) 

} 

class UserModel: NSObject { 

    // Instantiate the delegate 
    var delegate: UserModelDelegate? 

    override init() { 

     super.init() 

    } 

    func getUserInfo(token: String) { 

     let url = "http://test.app/api/userInfo" 

     let headers = [ 
      "Authorization":"Bearer \(token)", 
      "Content-Type": "application/x-www-form-urlencoded" 
     ] 


     Alamofire.request(.GET, url, headers: headers).responseJSON { response in 
     switch response.result { 
      case .Success(let data): 

       if (data as? [String:AnyObject])!["error"] == nil { 

        let json = JSON(data)     

        self.delegate?.didReceiveUserInfo(json) 

       } else { 

        self.delegate?.didReceiveUserInfo(nil) 

       } 
      case .Failure(let error): 
       print("Request failed with error: \(error)") 
      } 
     } 
    } 
} 
+0

あなたの 'var delegate:UserModelDelegate? 'を' weak var delegate:UserModelDelegate?'に変更して、保持サイクルを避けることをお勧めします。 – bitsoverflow

0

私は多分プロトコルの要件をオプションではないでしょう、この場合、あなたの代理人単一の要件があります。あなたが直面している問題は、JSONオブジェクトタイプがObjective-C互換である必要があるためです。クラスをObjective-Cクラス(例えばNSObjectなど)から継承させることで、そうすることができます。 もう1つのことは、保持サイクルを避けるためUserModeldelegateプロパティをweakと宣言することです。

編集:のObjective-Cでの迅速なクラスが互換性を持たせる方法の例:

class JSON: NSObject { 
//the code 
} 

注:私は以前あなたがクラスに@objc属性を追加する必要があると述べました。 Objective-Cでは、クラスを別の名前で表示させたくない場合は、実際には必要ありません。

SwiftとObjective-Cの相互運用性の詳細については、read hereを参照してください。ここで

+0

は、あなたの答えをいただき、ありがとうございます。問題はリクエストが失敗した場合のために2番目の要件があることです。クラスがObjective-Cクラスから継承し、@ objc属性を追加することについてのあなたの答えを理解していないか分かりません。それを行う方法?申し訳ありませんが、私はiOS開発に全く新しいです。 – sbkl

+0

@sbkl私は私の答えでさらに情報を追加しました。 – bitsoverflow

+0

@sbklまた、プロトコルの要件をオプションにすることはできません。ユーザーがプロトコル拡張を使用して実装する必要がない場合は、デフォルトの実装を使用することもできます。詳細はこちら:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521 – bitsoverflow