Reputation: 909
I am doing a simple pull from Core Data, and filtering the results with a predicate, with the following:
do {
let request: NSFetchRequest<CSTProjectDetails> = CSTProjectDetails.fetchRequest()
request.predicate = NSPredicate(format: "projectID == %@", cstProjectID)
cstProjectDetails = try context.fetch(request)
} catch {
print("There was an error fetching CST Project Details.")
}
When I try printing the following details, I get the following.
print("CSTProjectDetails: \(cstProjectDetails)")
This will produce something like this:
CSTProjectDetails: [ (entity: CSTProjectDetails; id: 0xd000000000080004 ; data: ), (entity: CSTProjectDetails; id: 0xd0000000000c0004 ; data: ), (entity: CSTProjectDetails; id: 0xd000000000100004 ; data: ), (entity: CSTProjectDetails; id: 0xd000000000140004 ; data: ), (entity: CSTProjectDetails; id: 0xd000000000180004 ; data: )]
I tried casting "cstProjectDetails" as an array, and doing some tests with the results:
let hrData = self.cstProjectDetails as NSArray
print("There are \(hrData.count) items in this project")
This will actually print out the correct number of items that were pulled from Core Data.
If I do this:
dump(hrData)
I get something like this:
- (entity: CSTProjectDetails; id: 0xd000000000080004 ; data: ) #1
- super: LogsterBeta3.CSTProjectDetails
- super: NSManagedObject
- (entity: CSTProjectDetails; id: 0xd0000000000c0004 ; data: ) #2
- super: LogsterBeta3.CSTProjectDetails
- super: NSManagedObject
- (entity: CSTProjectDetails; id: 0xd000000000100004 ; data: ) #3
- super: LogsterBeta3.CSTProjectDetails
- super: NSManagedObject
- (entity: CSTProjectDetails; id: 0xd000000000140004 ; data: ) #4
- super: LogsterBeta3.CSTProjectDetails
- super: NSManagedObject
- (entity: CSTProjectDetails; id: 0xd000000000180004 ; data: ) #5
- super: LogsterBeta3.CSTProjectDetails
- super: NSManagedObject
With all of that said, I attempted to access properties of the entity in a loop with something like this:
for logDetails in hrData {
print (logDetails.species)
}
However, this produces an immediate error, saying "species" is not a valid member.
Can anybody let me know what I am missing here? How can I get the values from my NSFetchRequest into an array?
Upvotes: 0
Views: 936
Reputation: 151
Try this
class TableViewVC: UIViewController, UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate {
@IBOutlet weak var tableView: UITableView!
var cstProject: CstProjectDetails?
var cstProjectDetails = [CstProjectDetails]()
let controller = CoredataController()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
cstProjectDetails = controller.fetchCstProjectDetails()
}
override func viewDidAppear(_ animated: Bool) {
tableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cstProjectDetails.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CstProjectDetailsCell", for: indexPath)
self.cstProject = cstProjectDetails[indexPath.row]
cell.configureCell(cstProjectDetails: cstProject!)
return cell
}
and
class CoredataController: NSObject, NSFetchedResultsControllerDelegate {
func fetchCstProjectDetails() -> [CstProjectDetails] {
var cstProjectDetails = [CstProjectDetails]()
let fetchProject: NSFetchRequest<CstProjectDetails> = CstProjectDetails.fetchRequest()
do {
let fetchedResult = try context.fetch(fetchCstProjectDetails)
for project in fetchedResult as [NSManagedObject] {
cstProjectDetails.append(project as! CstProjectDetails)
}
} catch {
let error = error as NSError
print("ATTEMPT FETCH ERROR IS: \(error)")
}
return cstProjectDetails
}
}
Upvotes: 0
Reputation: 535138
How can I get the values from my NSFetchRequest into an array?
You don't have to "get" anything "into an array". The result of calling fetch
is an array. You had an array to start with.
Then you did a very odd thing: you cast hrData
to an NSArray. Well, an NSArray has no element type. So now when you say
for logDetails in hrData {
Swift doesn't have any information about what a logDetails
even is. It just types it as an AnyObject.
If you want to extract its species
property you will need to cast your logDetails
back down to whatever it really is, i.e. a CSTProjectDetails.
Alternatively, don't cast to an NSArray, since this erases your element type information. Cast down to a Swift array of CSTProjectDetails. Now Swift will have type information from the get-go.
Upvotes: 2