InKwon Kim InKwon Kim - 9 months ago 60
iOS Question

How to access data index in CoreData using Swift3

I saved datas in entity of

Also I can access data in CoreData through
. But, I don't know how to access data using index.

follow code is my access way

let context = getContext()
let fetchRequest : NSFetchRequest<Entity> = Entity.fetchRequest()
let result = try? context.fetch(fetchRequest){
for object in result

How to access data in
using index of data?

Answer Source

Your result is type of Array so you can access the object from result like result[0] and so on.

if index < result.count {
    let obj = result[index]

If you want index from object, you can use index(of:) for that.

if let index = result.index(of: obj) {