简体   繁体   English

无法将“字符串”类型的值转换为预期的参数类型“ NSManagedObject” Swift

[英]Cannot convert value of type 'String' to expected argument type 'NSManagedObject' Swift

I was wondering why I can't add my data from Core Data to my tableview. 我想知道为什么不能将核心数据中的数据添加到表视图中。 It was first added and stored but when the app is reloaded the data is gone so I need to reload it but I can't seem to add the data to my tableview? 它是第一次添加和存储的,但是当重新加载应用程序时,数据已经消失了,所以我需要重新加载它,但是似乎无法将数据添加到我的tableview中? Here is the code 这是代码

var titlename: [NSManagedObject] = []

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let manageContext = appDelegate.persistentContainer.viewContext
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Items")

    fetchRequest.returnsObjectsAsFaults = false

    do {
        let results = try manageContext.fetch(fetchRequest)

        for result in results as! [NSManagedObject] {
            if let theName = result.value(forKey: "name") as? String {
                titlename.append(theName)
            }
        }
    } catch let error as NSError {
        print("Could not fetch. \(error), \(error.userInfo)")
    }

    tableView.reloadData()
}

您可能需要将titleName类型更改为

 var titlename: [String] = []

If tableview use NSManagedObject for create cell the simply add NSManagedObject data to the array. 如果tableview使用NSManagedObject创建单元格,只需将NSManagedObject数据添加到数组中。

for result in results as! [NSManagedObject] {
         titlename.append(result)
    }

OR 要么

The tableview only use the string from titlename array for creating cell then just change the type NSManagedObject to String . tableview仅使用titlename数组中的字符串来创建单元格,然后只需将NSManagedObject类型更改为String

var titlename: [String] = []

This will help. 这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ios无法将类型“()”的值转换为预期的参数类型“字符串”,迅速3 - ios Cannot convert value of type '()' to expected argument type 'String' swift 3 无法在Swift 3中将字符串类型的值转换为预期的参数类型int - cannot convert value of type string to expected argument type int in swift 3 尝试保存数组时CoreData出错。 &#39;无法将&#39;String&#39;类型的值转换为预期的参数类型&#39;NSManagedObject&#39;&#39; - Error in CoreData when trying to save an array. 'Cannot convert value of type 'String' to expected argument type 'NSManagedObject'' 无法将&#39;()&#39;的值转换为预期的参数类型&#39;String&#39;swift 3.0 - cannot convert value of '()' to expected argument type 'String' swift 3.0 Swift 1.2到Swift 2:无法将类型的值转换为预期的参数类型 - Swift 1.2 to swift 2: Cannot convert value of type to expected argument type 无法将类型&#39;String?.Type&#39;的值转换为预期的参数类型&#39;String?&#39; - Cannot convert value of type 'String?.Type' to expected argument type 'String?' 获取时出错:“无法转换类型&#39;NSFetchRequest的值 <NSManagedObject> &#39;到预期的参数类型&#39;NSFetchRequest <NSFetchRequestResults> “” - Error in Fetch: “Cannot convert value of type 'NSFetchRequest<NSManagedObject>' to expected argument type 'NSFetchRequest<NSFetchRequestResults>'” Swift类别:“无法将类型的值转换为预期的参数类型&#39;AnyObject!&#39; - Swift category: "Cannot convert value of type to expected argument type 'AnyObject!' Swift:无法将类型&#39;() - &gt; Bool&#39;的值转换为预期的参数类型&#39;PFObject&#39; - Swift: Cannot convert value of type '() -> Bool' to expected argument type 'PFObject' Swift 4:无法将“数据”类型的值转换为预期的参数类型“数据” - Swift 4 : Cannot convert value of type 'Data' to expected argument type 'Data'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM