Я пытаюсь создать шаблон MVVM с помощью Swift для Ios Project.Я исследовал различные ресурсы из Интернета.В конце концов я создал следующее для MVVM.Работает нормально.Но я не знаю, код MVVM или нет.Пожалуйста, объясните любой.
Это пример парсинга данных от API к метке.https://the-flash@bitbucket.org/the-flash/samplemvvm.git
Просмотр
//
// ViewController.swift
// SampleMVVM
//
// Created by ADMIN on 20/02/19.
// Copyright © 2019 ADMIN. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var NameLabel: UILabel!
@IBOutlet var ClassLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
service().forStudentName { cName, cClass in
let custViewModel = ViewModel(data: Model(myName: cName[0], myClass: cClass[0]))
DispatchQueue.main.async {
self.NameLabel.text = custViewModel.CustName
self.ClassLabel.text = custViewModel.CustClass
}
}
}
}
Просмотр модели
APICall.swift
//
// APICall.swift
// SampleMVVM
//
// Created by ADMIN on 20/02/19.
// Copyright © 2019 ADMIN. All rights reserved.
//
import UIKit
import Foundation
struct service{
func forStudentName(completionBlock: @escaping (Array<String>, Array<String>) -> Void) {
var CstmrName = Array<String>()
var CstmrClass = Array<String>()
let task = URLSession.shared.dataTask(with: URL(string: DefaultData.Base().Url)!){ data, response, error in
guard let jsonData = data else { return }
do {
let studentData = try JSONDecoder().decode(StudentData.self, from: jsonData)
for index in studentData.studentdetails{
CstmrName.append(index.studentname)
CstmrClass.append(index.studentdetailClass)
}
completionBlock(CstmrName, CstmrClass)
}catch let error {
print(error)
}
}
task.resume()
}
}
ViewModel.swift
//
// ViewModel.swift
// SampleMVVM
//
// Created by ADMIN on 20/02/19.
// Copyright © 2019 ADMIN. All rights reserved.
//
import UIKit
class ViewModel: NSObject {
var CustName: String?
var CustClass: String?
init(data: Model) {
self.CustName = data.CustomerName
self.CustClass = data.CustomerClass
}
}
Модель
Model.swift
//
// Model.swift
// SampleMVVM
//
// Created by ADMIN on 20/02/19.
// Copyright © 2019 ADMIN. All rights reserved.
//
import UIKit
//https://next.json-generator.com/api/json/get/4kobjnVE8
/****************************** Student Name Model *********************************/
class Model: NSObject {
var CustomerName: String?
var CustomerClass: String?
init(myName: String, myClass: String) {
CustomerName = myName
CustomerClass = myClass
}
}
/****************************** JSON Parsing Model *********************************/
struct StudentData: Codable {
let studentdetails: [Studentdetail]
}
struct Studentdetail: Codable {
let school, studentname, studentdetailClass, board: String
enum CodingKeys: String, CodingKey {
case school, studentname
case studentdetailClass = "class"
case board
}
}
DefaultData.swift
//
// DefaultData.swift
// SampleMVVM
//
// Created by ADMIN on 20/02/19.
// Copyright © 2019 ADMIN. All rights reserved.
//
import UIKit
import Foundation
class DefaultData: NSObject {
struct Base {
let Url: String = "https://next.json-generator.com/api/json/get/4kobjnVE8"
}
}
Большое спасибо за ваши ценные ответы !!!