gpt4 book ai didi

ios - 核心数据获取相关实体

转载 作者:行者123 更新时间:2023-11-29 01:28:05 27 4
gpt4 key购买 nike

我有实体ApplicationProcess,一个应用程序可以有多个进程,但一个进程只能有一个应用程序。我得到一个特定的应用程序实体:

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let appfetchRequest = NSFetchRequest(entityName: "Application")
//appfetchRequest.returnsObjectsAsFaults = false
do{
let results = try managedContext.executeFetchRequest(appfetchRequest)
applications.addObjectsFromArray(results as! [NSMutableArray])

}catch let error as NSError{
print("Jakis blad z applications entity: \(error)")
}

let predicate:NSPredicate = NSPredicate(format: "name == %@", appTitle)

let application:NSArray = applications.filteredArrayUsingPredicate(predicate)

在这方面我有关系(名为applicationprocesses)。我尝试以多种方式获取包含这些实体的数组,但没有一个有效。事实上,我有:

    let processes = application.valueForKey("applicationprocesses").allObjects.first

print(processes?.valueForKey("applicationprocesses"))

这给我:

Optional({(
<NSManagedObject: 0x7f945871b7a0> (entity: Application; id: 0xd000000000080002 <x-coredata://00C2FE4A-143B-436E-B39B-A0A32C300B68/Application/p2> ; data: {
appcolor = "#3F3F3F";
appicon = bed;
applabel = Proces;
applabelplural = Procesy;
applicationprocesses = (
"0xd000000000140004 <x-coredata://00C2FE4A-143B-436E-B39B-A0A32C300B68/Process/p5>",
"0xd000000000100004 <x-coredata://00C2FE4A-143B-436E-B39B-A0A32C300B68/Process/p4>",
"0xd000000000180004 <x-coredata://00C2FE4A-143B-436E-B39B-A0A32C300B68/Process/p6>",
"0xd0000000001c0004 <x-coredata://00C2FE4A-143B-436E-B39B-A0A32C300B68/Process/p7>",
"0xd0000000000c0004 <x-coredata://00C2FE4A-143B-436E-B39B-A0A32C300B68/Process/p3>"
);
companyid = 392;
id = 1261;
name = "aplikacja 1";
processescount = 5;
})
)})

我需要在 UITablewView 中显示这些进程,所以我需要一个数组。如果有任何帮助,我将不胜感激。

最佳答案

您遇到的问题不是某处错误代码行的结果。它实际上正在按预期工作。但是使用 NSManagedObject

可以使工作变得容易得多

从您的数据库中提取的任何内容都会生成 [AnyObject]。如果你让它保持原样,你将被迫使用键值编码,这很痛苦而且很容易搞砸。

然而,从 CD 实体创建类并向下转换获取结果非常简单。这是 CoreData 的一个很棒的特性,不幸的是没有得到足够的重视。

link to gist

您的相关实体可能如下所示:

enter image description here

转到菜单 -> 编辑器 -> 创建....

enter image description here

选择要为其创建子类的实体。

新文件将出现在您的项目中:

enter image description here


现在你可以使用这样的代码:

插入对象

注意 .... 作为?公司 这是垂头丧气的。它允许您访问 CD 实体中的属性,就像访问结构或类中的任何属性一样。

func createACompany() {
// no appDel here. appDel and managedContext are best declared in the class scope. See gist for entire ViewController
guard let moc = managedContext else { return }

guard let company = NSEntityDescription.insertNewObjectForEntityForName("Company", inManagedObjectContext: moc) as? Company else {
return // guard is handy to "abort"
}
company.name = "Apple"

guard let bob = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext: moc) as? Employee else {
return
}
bob.name = "Bob"
bob.company = company

do {
try moc.save()
} catch {
print("core data save error : \(error)")
}

moc.refreshAllObjects()

}

获取对象

func fetchCompanies() -> [Company] {

guard let moc = managedContext else { return [] }
let request = NSFetchRequest(entityName: "Company")
request.returnsObjectsAsFaults = true

do {
let results = try moc.executeFetchRequest(request)

guard let companies = results as? [Company] else {
return []
}

return companies

}catch let error as NSError{
print("core data fetch error: \(error)")
return []
}

}

获取相关对象

仔细观察 guard 语句。

  • let employeeSet = company.employees -> 展开可选的 NSSet
  • let employees = employeeSet.allObjects -> 获取NSSet中的所有对象
  • 作为? [Employee] -> 将 allObjects 的结果向下转换为 Array员工

func getEmployees(forCompany company : Company) -> [Employee] {

guard let employeeSet = company.employees, let employees = employeeSet.allObjects as? [Employee] else {
return []
}

return employees

}

旁注:

如果您改变了对类命名的想法,并且您在所有地方都进行了更改。不要忘记在这里更新它:

class 字段必须更新。

enter image description here

类似于InterfaceBuilder。如果您更改类名,它将不再找到它。

关于ios - 核心数据获取相关实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33782598/

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