带有Alamofire和ObjectMapper的Swift 4.0没有这样的模块'ObjectMapper'

问题描述 投票:0回答:3

嗨,我是IOS开发的新手。我正在创建一个小应用程序。我正在尝试导入ObjectMapper,但它没有找到任何模块。我已经安装了pod。我的pod文件看起来像:

  target 'Sample' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for Sample
pod 'Alamofire', '~> 4.5.0'
pod 'AlamofireObjectMapper', '~> 5.0'
pod 'AlamofireImage', '~> 3.3'
pod 'PKHUD', '~> 5.0'
pod 'ObjectMapper', '~> 3.0.0'

结束

我使用的是swift 4.0.2版本,Xcode版本是9.1。有没有遗漏的东西?需要一些帮助。

ios swift objectmapper
3个回答
2
投票

最新版本的ObjectMapper是pod 'ObjectMapper', '~> 3.1',有一件事就是清理并构建你的项目并确保你正在使用YourProjectName.xcworkspace


1
投票

试试3.4版。这个对我有用。

pod 'ObjectMapper', '~> 3.4'

0
投票
1) Model Class One--------------
-----------------------------------------
-----------------------------------------

import Foundation
import ObjectMapper

class MainData : NSObject, Mappable {
    var page : Int?
    var per_page : Int?
    var total : Int?
    var total_pages : Int?
    var data : [Data]?
    override init() {

    }
    required init?(map: Map) {

    }

     func mapping(map: Map) {

        page <- map["page"]
        per_page <- map["per_page"]
        total <- map["total"]
        total_pages <- map["total_pages"]
        data <- map["data"]
    }

}
2) Model Class Two--------------
-----------------------------------------
-----------------------------------------

import Foundation
import ObjectMapper

class Data : NSObject, Mappable {
    var id : Int?
    var name : String?
    var year : Int?
    var color : String?
    var pantone_value : String?
    override init() {

    }
    required init?(map: Map) {

    }

     func mapping(map: Map) {

        id <- map["id"]
        name <- map["name"]
        year <- map["year"]
        color <- map["color"]
        pantone_value <- map["pantone_value"]
    }

}
3) ViewController--------------
-----------------------------------------
-----------------------------------------


import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
       @IBOutlet weak var myTableview: UITableView!

    @IBAction func btnNewFeature(_ sender: UIButton) {
        let vc3 = self.storyboard? .instantiateViewController(withIdentifier: "TheardViewController") as! TheardViewController
        self.navigationController? .pushViewController(vc3, animated: true)


    }
    var DictData = MainData()
    var ArrayData = [Data]()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.myTableview.layer.cornerRadius = 10
        getData()

        // Do any additional setup after loading the view, typically from a nib.
    }



func getData()
{
    let dict = [
        "name":"shital",
        "rollno":"1"
    ]
    APIManager.sharedInstance.getListOfFeatures(parms: dict, onsuccess: { (responsedata, anystring) in

        self.DictData = responsedata
        self.ArrayData = self.DictData.data!
        print(self.DictData.page)
        for item in self.ArrayData
        {
            print(item.color)
            print(item.id)
            print(item.name)
        }
        self.myTableview.reloadData()


    }) { (error, error1) in
        print(error,error1)
    }

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(self.ArrayData.count)
        return self.ArrayData.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = myTableview.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell
       let element = self.ArrayData[indexPath.row]
        let id = "\(element.id!)"
        cell.txtNoofvotes.text = id
        cell.lblFeatureTitle.text = element.name
        cell.lblRequestedName.text = element.name
        cell.txtViewFeatureDesc.text = element.pantone_value
        cell.lblRequestedDate.text = "\(element.year!)"


        if (element.id == 1)
        {
            cell.btnVote.setTitle("Voted", for: .normal)
            cell.btnVote.isEnabled = false
        }
        else
        {
            cell.btnVote.setTitle("Vote", for: .normal)
        }
        cell.btnVote.tag = indexPath.row
        cell.btnVote.addTarget(self, action: #selector(voteButtonPressed(sender:)), for: .touchUpInside)
        return cell
    }


    @objc func voteButtonPressed(sender:UIButton)
    {
        let arrdata = self.ArrayData[sender.tag]
        self.myTableview.reloadData()
        let dict = [
            "commandType":"registervote",
            "requestedId":"10"
        ]
        APIManager.sharedInstance.getListOfFeatures(parms: dict, onsuccess: { (responsedata, anymessage) in
            print(responsedata,anymessage)
        }) { (error1, error2) in
            print(error1,error2)
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc2 = self.storyboard? .instantiateViewController(withIdentifier: "SecendViewController") as! SecendViewController
        let element = self.ArrayData[indexPath.row]
        vc2.arrpass = [element]
        self.navigationController? .pushViewController(vc2, animated: true)
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 300
    }
}
4) SecendViewController--------------
-----------------------------------------
-----------------------------------------

import UIKit

class SecendViewController: UIViewController {
    var arrpass = [Data]()
    @IBOutlet weak var uiMainView: UIView!

    @IBOutlet weak var lblFeatureTitile: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        let item = self.arrpass[0]
        self.lblFeatureTitile.text = item.name
        self.uiMainView.layer.cornerRadius = 10
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
5) TheardViewController--------------
-----------------------------------------
-----------------------------------------


import UIKit

class TheardViewController: UIViewController {


    @IBOutlet weak var lblUseCase: UILabel!
    @IBOutlet weak var txtUsecase: UITextView!
    @IBOutlet weak var txtFeatureDescription: UITextView!
    @IBOutlet weak var txtName: UITextField!

    @IBOutlet weak var txtFeatureTitile: UITextField!
    @IBOutlet weak var txtEmail: UITextField!
    let imgselected = UIImage.init(named: "Selected")

    let imgunselected = UIImage.init(named: "Unselected")

    @IBOutlet weak var btnLow: UIButton!
    @IBOutlet weak var btnMedium: UIButton!
    @IBOutlet weak var btnHigh: UIButton!
    var strresult = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        self.btnMedium.setImage(imgunselected, for: .normal)
        self.btnHigh.setImage(imgunselected, for: .normal)
        self.btnLow.setImage(imgunselected, for: .normal)
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func btnLowPressed(_ sender: UIButton) {

        self.strresult = "L"
        self.btnLow.setImage(imgselected, for: .normal)
        self.btnHigh.setImage(imgunselected, for: .normal)
        self.btnMedium.setImage(imgunselected, for: .normal)
        self.lblUseCase.text = "Use Case"


    }
    @IBAction func btnMediumPressed(_ sender: UIButton) {
        self.strresult = "M"
        self.btnMedium.setImage(imgselected, for: .normal)
        self.btnHigh.setImage(imgunselected, for: .normal)
        self.btnLow.setImage(imgunselected, for: .normal)
        self.lblUseCase.text = "Use Case *"

    }

    @IBAction func btnHighPressed(_ sender: UIButton) {
        self.strresult = "H"
        self.btnHigh.setImage(imgselected, for: .normal)
        self.btnLow.setImage(imgunselected, for: .normal)
        self.btnMedium.setImage(imgunselected, for: .normal)
        self.lblUseCase.text = "Use Case *"


    }
    @IBAction func btnSendReQuest(_ sender: UIButton) {
        var strname = self.txtName.text!
        var stremail = self.txtEmail.text!
        var strtitile = self.txtFeatureTitile.text!
        var strUsecase = self.txtUsecase.text!

        if strname.count <= 0
        {
            let alert = UIAlertController(title: "Warning", message: "Please Enter Name", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
        else if stremail.count <= 0
        {
            let alert = UIAlertController(title: "Warning", message: "Please Enter Email", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
        else if strtitile.count <= 0
        {
            let alert = UIAlertController(title: "Warning", message: "Please Enter Title", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
        else if strresult.count <= 0
        {
            let alert = UIAlertController(title: "Warning", message: "Please Set Priority", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
        else if (((strresult == "H") && (self.txtUsecase.text.count  <= 0)) || (strresult == "M") && (self.txtUsecase.text.count  <= 0)) {

            let alert = UIAlertController(title: "Warning", message: "Please enter uese case.", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
        else
        {

            let alert = UIAlertController(title: "Success", message: "All Ok", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)


        }

    }

}
6) APIManager--------------
-----------------------------------------
-----------------------------------------


import UIKit
import ObjectMapper
import Alamofire
class APIManager: NSObject {

    static let sharedInstance = APIManager()


    func getListOfFeatures(parms:[String:Any], onsuccess:@escaping(MainData,String) -> Void, onfailure:@escaping(String,String) ->Void)
    {
        let url = URL.init(string: "https://reqres.in/api/unknown")
        let headres: HTTPHeaders = ["content-type":"application/json"]

        Alamofire.request(url!, method: .get, parameters: parms, encoding: JSONEncoding.default, headers: headres).responseJSON
            {
                response in
                switch response.result{
                case .success:
                    let string = NSString(data: response.data!, encoding: String.Encoding.utf8.rawValue)
                    print("string:\(String(describing: string))")
                    do{
                        let jsonResponse = try JSONSerialization.jsonObject(with: response.data!, options: []) as! [String:AnyObject]
                        let userDetails = Mapper<MainData>() .map(JSON: jsonResponse)

                        if jsonResponse != nil
                        {
                            onsuccess(userDetails!,"success")
                        }
                        else
                        {
                            onfailure((response.error?.localizedDescription)!,"fail")
                        }
                    }
                    catch let parsingError
                    {
                        print("error",parsingError)
                        onfailure((response.error?.localizedDescription)!,"fail")
                    }
                    break
                case .failure(let error):
                    print(error)
                    onfailure((response.error?.localizedDescription)!,"fail")
                }    
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.