小编Cha*_*ace的帖子

无法将'NSKnownKeysDictionary1'()类型的值转换为''()

我正在尝试将我的NSManagedObject转换为字典,因此我可以使用序列化它来获取JSON.

func fetchRecord() -> [Record] {

        let fetchRequest = NSFetchRequest<Record>(entityName:"Record")
        let context = PersistenceService.context

        fetchRequest.resultType = .dictionaryResultType

        do {
            records = try context.fetch(Record.fetchRequest())

        } catch {
            print("Error fetching data from CoreData")
        }
        print(records)
        return records
 }
Run Code Online (Sandbox Code Playgroud)

我已经讨论过这个问题:如何将NSManagedObject转换为NSDictionary,但它们的方法与我的方法非常不同.我还尝试了这个问题中提供的方法:CoreData对象到Swift 3中的JSON.但是我收到了这个错误

无法将'NSKnownKeysDictionary1'(0x108fbcaf8)类型的值转换为'iOSTest01.Record'(0x1081cd690)$

而我似乎无法找到解决方案.

此前已提到错误:核心数据:无法将"MyType_MyType_2"类型的值转换为MyType,但没有一种方法可以解决我的问题.有人能为我提供一个Swift解决方案吗?

更新

为了帮助下面的评论,我添加了以下内容:

var record: Record!
var records = [Record]()
Run Code Online (Sandbox Code Playgroud)

记录+ CoreDataClass:

public class Record: NSManagedObject {

}
Run Code Online (Sandbox Code Playgroud)

记录+ CoreDataProperties:

extension Record {

    @nonobjc public class …
Run Code Online (Sandbox Code Playgroud)

core-data ios swift

5
推荐指数
1
解决办法
1917
查看次数

以编程方式在UIVIew中嵌套UICollectionView

我试图让UICollectionView在UIView中,因为我正在使用的框架需要返回UIView.我看过这个问题:如何将UICollectionView作为子视图添加到UIView?在没有Storyboard的情况下在UIView中添加UICollectionView但不确定如何使其工作.

我有这样的尝试:

class TopView : UIView, UICollectionViewDataSource, UICollectionViewDelegate {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .red

        addSubview(collectionView)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.delegate = self
        cv.dataSource = self
        cv.register(HeaderCell.self, forCellWithReuseIdentifier: "HeaderCell")
        cv.backgroundColor = .yellow

        return cv
    }()

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1 …
Run Code Online (Sandbox Code Playgroud)

uiview ios uicollectionview swift

5
推荐指数
1
解决办法
2847
查看次数

Swift 3中的CoreData对象为JSON

我正在努力将我的CoreData对象转换为JSON,以便我可以使用它发送到Web服务器.

这就是我目前从CoreData获取对象的方式:

func fetchRecord() -> [Record] {

    do {
        records = try context.fetch(Record.fetchRequest())

    } catch {
        print("Error fetching data from CoreData")
    }
    return records
}
Run Code Online (Sandbox Code Playgroud)

我可以通过这种方式显示在我的tableView上:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "recordCell", for: indexPath) as! RecordCell

    cell.nameLbl.text = records[indexPath.row].name
    cell.quantityLbl.text = "Quantity: \(String(records[indexPath.row].quantity))"
    cell.dateLbl.text = dateString(date: records[indexPath.row].date)

    return cell
}
Run Code Online (Sandbox Code Playgroud)

我试图在我的请求中循环,如下所示:

for rec in records {
    print(rec)
}
Run Code Online (Sandbox Code Playgroud)

这给出了:

在此输入图像描述

我已经阅读了很多关于实现这一目标的方法,但它们似乎都没有对我有益.大多数示例都显示了如何将JSON提供给CoreData而不是其他方式.有谁知道任何可以帮助我实现这一目标的好教程或文档?

json core-data ios

2
推荐指数
3
解决办法
3118
查看次数

标签 统计

ios ×3

core-data ×2

swift ×2

json ×1

uicollectionview ×1

uiview ×1