Swift3 Alamofire SwiftyJSON 练习

前端之家收集整理的这篇文章主要介绍了Swift3 Alamofire SwiftyJSON 练习前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自我的github: https://github.com/uniquejava/AlamofireSwiftyJSONDemo

Alamofire | SwiftyJSON Demo

My swift3 exercise for this excellent tutorial with some of my own changes.

Setup CocoaPods

  1. sudo gem update --system
  2. sudo gem install cocoapods
  3. pod setup
  4. (Xcode8 create single view application)
  5. cd AlamofireSwiftyJSONDemo
  6. pod init
  7. open -a Xcode Podfile
  8. Make some changes here,see below
  9. pod install
  10. Analyzing dependencies
  11. Downloading dependencies
  12. Installing Alamofire (4.0.1)
  13. Installing SwiftyJSON (3.1.1)
  14. Generating Pods project
  15. Integrating client project
  16.  
  17. [!] Please close any current Xcode sessions and use `AlamofireSwiftyJSONDemo.xcworkspace` for this project from now on.
  18. Sending stats
  19. Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

Podfile: Before modification(pod init)

  1. # Uncomment the next line to define a global platform for your project
  2. # platform :ios,'9.0'
  3.  
  4. target 'AlamofireSwiftyJSONDemo' do
  5. # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  6. use_frameworks!
  7.  
  8. # Pods for AlamofireSwiftyJSONDemo
  9.  
  10. end

Podfile (after)

  1. # Uncomment the next line to define a global platform for your project
  2. platform :ios,'10.0'
  3.  
  4. target 'AlamofireSwiftyJSONDemo' do
  5. # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  6. use_frameworks!
  7.  
  8. # Pods for AlamofireSwiftyJSONDemo
  9. pod 'Alamofire'
  10. pod 'SwiftyJSON'
  11.  
  12. end

Implement this App

  1. From storyboard,drag a table view into the generated view controller
  2. Resize it and add missing constraints
  3. Set cell type to be subtitle
  4. Give cell a reusable identifier ContactCell
  5. Ctrl drag this table view to ViewController.swift and bind a var for it
  6. Meanwhile set ViewController as its datasource and delegate
  7. Implement the 2 required datasource methods
  8. Embed the controller into a navigation controller.
  9. Add a title for this view controller
  10. Select this view controller,uncheck the 2 options under Attribute Inspector - Extend Edges,otherwise there will be some blank area above the table view.
  11. Change Info.plist add App Transport Security Settings - Allow Arbitrary Loads: Yes,otherwise you won't be able to issue any http request.

Code

  1. @IBOutlet weak var contactsTableView: UITableView!
  2. var contacts = [[String: AnyObject]]() // Array of dictionary
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. // Do any additional setup after loading the view,typically from a nib.
  6. Alamofire.request("http://api.androidhive.info/contacts/").responseJSON {
  7. (res) in
  8. guard let resultValue = res.result.value else {
  9. return
  10. }
  11. let swiftyJsonVar = JSON(resultValue)
  12. if let resData = swiftyJsonVar["contacts"].arrayObject {
  13. self.contacts = resData as! [[String: AnyObject]]
  14. }
  15. if self.contacts.count > 0 {
  16. self.contactsTableView.reloadData()
  17. }
  18. }
  19. }
  20. func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
  21. return contacts.count
  22. }
  23. func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  24. let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell",for: indexPath)
  25. let contact = contacts[indexPath.row]
  26. cell.textLabel?.text = contact["name"] as? String
  27. cell.detailTextLabel?.text = contact["email"] as? String
  28. return cell
  29. }

猜你在找的Swift相关文章