gpt4 book ai didi

ios - 解析 JSON 文件(本地存储),从而在 swift 中创建对象数组

转载 作者:行者123 更新时间:2023-11-30 14:00:32 27 4
gpt4 key购买 nike

我是 json 解析术语的初学者。我实际上正在尝试从本地存储在我的 swift 项目中的 json 文件中获取数据。我已正确完成所有操作,目前可以获取数据。但是,我无法创建对象数组(将根据来自 json 文件的值集创建对象)。然后在 View Controller (CompanyViewController.swift)类中使用该数组。我已经在不同的类中编写了我的 json 解析代码(这里为 CompanyHandler.swift ..),并且所有字段都已在另一个 swift 类中初始化(这里为 Company.swift ..)。

我的 CompanyViewController.swift Controller 文件如下:

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()

}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var cell = collectionView.cellForItemAtIndexPath(indexPath) as CustomCollectionViewCell
// cell.customImageView?.image = UIImage(named: "\(array[indexPath.row])")
AppEngine().getCompanyDetails()

// if ((cell.selected) && (indexPath.row == 0)) {
// var vc: AbcViewController = AbcViewController(nibName: "AbcViewController", bundle: nil)
// self.navigationController?.pushViewController(vc, animated: true)
// }
}

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.
}
}


My Company.swift controller file is as following:

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 = ["Project"]
// var projectStatus: String

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)
{
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
// self.projectStatus = projectStatus
}
}



My CompanyHandler.swift controller file is as following:

class CompanyHandler {

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



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 companyArray:NSArray = jsonData["companyDetails"] as NSArray!

for i in companyArray{
var cmpID: String = i["companyID"] as String
var cmpName: String = i["companyName"] as String
var cmpAdd: String = i["companyAddress"] as String
var cmpWebAdd: String = i["companyWebAddress"] as String
var cmpfbAdd: String = i["companyFB_Address"] as String
var cmptwtAdd: String = i["companyTWT_Handle"] as String
var cmpliAdd: String = i["companyLI_Address"] as String
var abtUs: String = i["aboutUs"] as String
var emailId: String = i["email_ID"] as String
var ivrNo: String = i["ivrNo"] as String
println("\(ivrNo)")

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")
}
}
}

最佳答案

您实际上并没有创建 Company 对象数组,该数组将充当 Collection View 的数据源。您只是初始化新的 Company 对象,但没有对它们执行任何操作。在 CompanyViewController 上创建一个属性,并将其设置为 [Company] 类型。在 json 字典的每次迭代中,创建新公司后,将其附加到 Company 数组中,然后将该数组用于 Collection View 。

关于ios - 解析 JSON 文件(本地存储),从而在 swift 中创建对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33051469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com