N3Restful-Swift - RESTful service interaction in iOS project with Swift
N3Restful-Swift
RESTful service interaction in Swift iOS project.
- Create request with Alamofire.
- Return JSON with SwiftyJSON.
- Parse JSON to Swift object automacally with ROJSONParser.
Features
- Request with dictionary param.
- Request with object param RESTParam.
- Request with multipart data (JSON, String, File).
Requirements
- iOS 8.0 or later.
Installation
- Copy 'N3Restful' folder to your project manually.
- Install Alamofire and SwiftyJSON. </ul>
- Init invoker with a controller:
- Make a request to sign in and parse response to
SignInResultobject
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods To integrate Alamofire and SwiftyJSON into your Xcode project using CocoaPods, specify it in your Podfile:
source '
pod 'Alamofire'
pod 'SwiftyJSON'</pre> </div>
Then, run the following command:
$ pod install
Manually
If you prefer not to use either of the aforementioned dependency managers, you can integrate Alamofire and SwwiftyJSON into your project manually.
Usage
Create data model SignInResult
class SignInResult: ROJSONObject {
var access_token: String! {
return Value<String>.get(self, key: "access_token")
}
lazy var user: User! = {
return Value<User>.getROJSONOject(self, key: "user")
}()
}</pre> </div>
class User: ROJSONObject {
var email: String! {
return Value<String>.get(self, key: "email")
}
var first_name: String! {
return Value<String>.get(self, key: "first_name")
}
var last_name: String! {
return Value<String>.get(self, key: "last_name")
}
}</pre> </div>
Create object param class
class SignInParam: RESTParam {
var email: String!
var password: String!
}
Invoker
init() {
super.init(controllerName: "sessions")
}
func signIn(param: SignInParam, completion: (result: SignInResult?, error: RESTError?) -> Void) {
let request = requestWithMethodName(nil)
request.POST(param) { (result: SignInResult?, error) -> () in
completion(result: result, error: error)
}
}</pre> </div>
Contribution
If you see any problems or you want to make an improvement, please create Issues ans we can discuss.
Thanks
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
相關經驗
相關資訊