我在Code Data中存储了一个名为Users的表。我只是试图从返回的数据数组中提取“FirstName”,但在我的控制台中继续“nil”。
let fetchRequest = NSFetchRequest(entityName:"Users")
let fetchedResults =
managedContext.executeFetchRequest(fetchRequest, 错误:&错误)为? [用户]
var arrayvalues=NSArray(array: fetchedResults!)
if let results = managedObject?.executeFetchRequest(fetchedResults, error: nil) as? [Users] {
let someObject = results[firstName]
}
我的用户类如下:
class Users: NSManagedObject {
@NSManaged var email: String
@NSManaged var firstName: String
@NSManaged var fLast: String
@NSManaged var fullName: String
@NSManaged var id: String
@NSManaged var isActive: String
@NSManaged var lastName: String
@NSManaged var lastupdated: String
@NSManaged var role: String
@NSManaged var uPass: String
}
我一直在寻找实现这一目标的方法,但似乎无法获取任何FirstNames ......
答案 0 :(得分:0)
替换
if let results = managedObject?.executeFetchRequest(fetchedResults, error: nil) as? [Users] {
let someObject = results[firstName]
}
通过
var firstNames = [String]() // empty array to keep the firstNames
if let results = managedObject?.executeFetchRequest(fetchedResults, error: nil) as? [Users] {
for user in results {
firstNames.append(user.firstName)
}
}
我的代码段在firstNames数组中添加了每个用户的firstName。您必须意识到代码中的 someObject 不是单个用户而是用户数组。