- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 tableView1 中有一个 Person 类,然后在 TableView2 中,用户添加品牌以与该人相关联。我的获取请求试图找回他们选择的人,并用该人的相关品牌填充 tableView 的行,但我被困在这里:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath) as! UITableViewCell
let brandSetAllObjects = brandSet?.allObjects
let brand = brandSetAllObjects as? Brand[indexPath.row]
cell.textLabel!.text = brand.valueForKey("name") as? String
return cell
}
获取:
//Helper Function to Fetch Core Data
func fetchCoreData() {
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
//2
let fetchRequest = NSFetchRequest(entityName:"Person")
//3
var error: NSError?
let fetchedResults =
managedContext.executeFetchRequest(fetchRequest,
error: &error) as? [NSManagedObject]
if let results = fetchedResults as? [Person] {
for selectedPerson in results {
brandSet = selectedPerson.brands
println(brandSet)
}
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
类:
import Foundation
import CoreData
class Brand: NSManagedObject {
@NSManaged var name: String
@NSManaged var people: NSSet
}
import Foundation
import CoreData
class Person: NSManagedObject {
@NSManaged var name: String
@NSManaged var brands: NSSet
func addBrandsObject(value: Brand) {
self.mutableSetValueForKey("brands").addObject(value)
}
}
最佳答案
是否有特定的可排序顺序,您希望表格 View 的内容显示在其中?
除了直接访问数据结构“Brand
”,您还可以拥有一个以统一方式操作数据的数据管理器。您可以将所有 Brand
信息保存在一个 NSSet
中,然后在您的数据管理器上有一个返回 NSArray
的方法(可能称为 brandArray
).数据管理器可以获取 NSSet
的内容,将它们添加到数组中,然后对数组进行排序以使它们处于统一顺序并返回该数组。
在 UITableView
端,只需将 Brand[indexPath.row]
替换为 dataManager.brandArray[indexPath.row]
。
我对数据管理器的建议是制作一个将作为 singleton 实现的类. this tutorial 的第 4 步很好地解释了如何设置数据管理器。
旁注:数据管理器只是一个概念,不是严格的类或对象类型。它可以是管理数据的任何对象,尽管这通常作为可以从项目中的任何其他类访问的单例执行。
关于ios - 如何使用 NSSet 填充 tableView? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30539101/
我一直在尝试使用 CoreData 和 Swift 在关系中添加对象。我很茫然,我不明白为什么我的代码不起作用。我正在尝试向“团队”添加“事件”。我找不到已接受的答案(应该有效)和我的代码(无效)之间
我有两个 NSSet。一个包含所有用户 key ,另一个包含在线用户 key 。我想分两部分展示这个值。 Section 1-在线用户和section2-其他用户 我使用如下谓词完成了此操作。 NSS
我正在努力处理核心数据...有些事情我不明白,所以如果有人能帮助我,我将不胜感激!我有一个实体(食谱)与另一个实体(膳食)存在多对多关系。我想选择所有与一顿饭无关的食谱……所以我试过了: 代码: [m
这个问题在这里已经有了答案: Odd property declaration syntax containing angular brackets <> (2 个回答) 6年前关闭。 我已生成 NS
我有两个关系表。 在尝试保存产品的 ShopItem 类中: let productEntity = NSEntityDescription.entityForName("Product", inMa
我有一个 UITableView,它通过以下方式添加核心数据中的信息: 标题中添加了各种名称的“类别” 与类别相对应的名称应加载到类别下方的单元格中 现在,我的标题加载了正确的名称以及添加的正确数量的
当列出相同的 tableView 时,有时会发生这种错误,我的意思是有时是,有时不是。当我尝试检查检索到的 NSSet 是否包含任何对象时: if(coin.dublicates.count > 0)
众所周知,比较 float 时存在 float 精度问题。 此外,NSSet 使用相等比较来保持元素在其中的唯一性。 那么当 NSSet 存储其中包含 float/double 的 NSValues
我正在将数据与核心数据一起使用,并获得一个 NSNumber 的 NSSet...我的问题是如何轻松地将所有对象更改为 int 值?或者我可以直接从数据库中获取 int 值吗? 谢谢 最佳答案 NSS
假设我有 NSManagedObject Zoo,他得到了一个到名为 cats 的“Cat”的连接 oneToMany,这个连接是 NSSet。当我使用函数 'filteredSetUsingPred
我在产品实体和购物车实体之间存在一对多关系。 用户需要有机会从购物车中删除产品。 我这样获取产品: // Fetch request for "Product": NSFetchRe
我正在尝试处理 xCode 中的关系,但遇到了一些问题: 我想创建一个简单的 User 到 favoriteThing 关系。我创建了一个模型,因此每个用户看起来都是这样的: extension Us
我想过滤categoryId == value但我的谓词失败的地方。这是我的代码。 categoryList 是 VenueCategory 类的对象数组,categoryId 是 Category
很抱歉新手问题,但我们都在这一点上:) 我正在进行枚举以从 NSSet 中获取正确的对象,以将 Cell 的标题设置为 Set 中的对象,这是我的代码: // the object we wa
我有 2 个按钮:button1 和 button2。我想为每个触摸的相应按钮创建一个 NSSet,并希望在触摸按钮 2 时显示 set1 值,反之亦然。按下按钮 1 时仅打印 set 1,按下按钮
我从 CoreData 返回了一个 Customer 对象。我正在尝试计算 NSSet 中存在的所有 amount 属性的总和: extension Customer { //...
我从 Web 服务中获取了一些 JSON,其中包含我希望分解为元素的逗号分隔字符串。 因为这是多对多关系,所以我尝试将其作为 NSSet 获取。但是,json 本身似乎由字符串组成,因此我需要先将逗号
所以我有这个 NSSet 对象,直接来自 Core Data 关系。这个 NSSet 包含的对象都是同一基础对象的所有子类(我们称它为 RVSuperClass,它本身是 NSManagedObjec
为什么我做不到 NSLog(@"mySet count: %@", [mySet count]); 然后使用 NSSet? (如果我确实使用它,应用程序会因“EXC_BAD_ACCESS”而崩溃 我已
我的核心数据中有一个一对多的关系设置: Day > Transactions 一天对象将包含交易的 NSSet 和 Date。 在某些时候,我想获取最新的 Day 并通过一段时间(每月和每年)过滤其
我是一名优秀的程序员,十分优秀!