如何在另一个类中使用字典的值?

时间:2015-10-12 08:27:42

标签: ios json swift dictionary uicollectionview

我一直在研究JSON字典。我使用了一个本地放置的JSON文件,并在Swift中解析它。我无法在其他类中使用该函数(包含我的JSON解析数据)。

我写了我的" CompanyHandler.swift"文件如下:

    import Foundation
    class CompanyHandler {

    // MARK: - company details
        func getCompanyDetails() -> Company {

            var jsonPath = NSBundle.mainBundle().pathForResource("companyDetails", ofType: "json")
            var data = NSData(contentsOfFile: jsonPath!)
            var jsonData: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!,
                options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

            var companyDic: NSDictionary = jsonData["companyDetails"] as NSDictionary!

           //get company detail
                var cmpID: String = companyDic["companyID"] as String
                var cmpName: String = companyDic["companyName"] as String
                var cmpAdd: String = companyDic["companyAddress"] as String
                var cmpWebAdd: String = companyDic["companyWebAddress"] as String
                var cmpfbAdd: String = companyDic["companyFB_Address"] as String
                var cmptwtAdd: String = companyDic["companyTWT_Handle"] as String
                var cmpliAdd: String = companyDic["companyLI_Address"] as String
                var abtUs: String = companyDic["aboutUs"] as String
                var emailId: String = companyDic["email_ID"] as String
                var ivrNo: String = companyDic["ivrNo"] as String

                println("\(ivrNo)")
                println("\(cmpName)")

            // get company Projects
                var projectArray : NSArray = companyDic["projects"] as NSArray
                var projectMutArray:NSMutableArray = [projectArray]

                for tempProject in projectArray
                {
                    var projectID: String = tempProject["projectID"] as String
                    var projectName: String = tempProject["projectName"] as String
                    var projectStatus: String = tempProject["projectStatus"] as String
                    var projectAddress: String = tempProject["projectAddress"] as String
                    var projectStartDate: String = tempProject["projectStartDate"] as String
                    var projectLogoImage: String = tempProject["projectLogoImage"] as String

                    println("\(projectID)")
                }

                var project = Project(projectId: "projectID", projectName: "projectName", projectStatus: "projectStatus", projectAddress: "projectAddress", projectStartDate: "projectStartDate", projectLogoImage: "projectLogoImage")

                var company = Company(companyID: "cmpID", companyName: "cmpName", companyAddress: "cmpAdd", companyWebAddress: "cmpWebAdd", companyFB_Address: "cmpfbAdd", companyTWT_Handle: "cmptwtAdd", companyLI_Address: "cmpliAdd", aboutUs: "abtUs", email_ID: "emailId", ivrNo: "ivrNo", projects: [projectArray])

                    return company
          }
    }

我的字典的intializer类是" Company.swift"如下:

class Company {
    var companyID: String
    var companyName: String
    var companyAddress: String
    var companyWebAddress: String
    var companyFB_Address: String
    var companyTWT_Handle: String
    var companyLI_Address: String
    var aboutUs: String
    var email_ID: String
    var ivrNo: String
    var projects: NSMutableArray

    init(companyID: String, companyName: String,companyAddress: String, companyWebAddress: String, companyFB_Address: String, companyTWT_Handle: String, companyLI_Address: String, aboutUs: String, email_ID: String, ivrNo: String, projects: NSMutableArray)
    {
        self.companyID = companyID
        self.companyName = companyName
        self.companyAddress = companyAddress
        self.companyWebAddress = companyWebAddress
        self.companyFB_Address = companyFB_Address
        self.companyTWT_Handle = companyTWT_Handle
        self.companyLI_Address = companyLI_Address
        self.aboutUs = aboutUs
        self.email_ID = email_ID
        self.ivrNo = ivrNo
        self.projects = [projects]
    }
}



My "CompanyViewController.swift" file is as following:

import UIKit

class CompanyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var customCollectionViewCell = CustomCollectionViewCell()


        collectionView.registerNib(UINib(nibName: "CustomCollectionViewCell", bundle:nil), forCellWithReuseIdentifier: "CustomCollectionViewCell")
        collectionView!.multipleTouchEnabled = true
        collectionView!.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(collectionView!)

        AppEngine().getCompanyDetails()

        var company1: Company

    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        var cell = collectionView.cellForItemAtIndexPath(indexPath) as CustomCollectionViewCell
        AppEngine().getCompanyDetails()

    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        var abc = CGSize(width: collectionView.frame.size.width,height: collectionView.frame.size.height/2)
        return abc
    }

    func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

    func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 15
    }

    func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CustomCollectionViewCell", forIndexPath: indexPath) as CustomCollectionViewCell

        cell.customImageView?.image = UIImage(named: "image1.png")
        cell.customLabel?.text = "\(indexPath.row)"
        return cell
    }

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

0 个答案:

没有答案