4

I saved datas in entity of CoreData. Also I can access data in CoreData through object.value(forKey:"attribute"). 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
       object.value(forKey:"attribute")...
       ...
       }
    }

How to access data in CoreData using index of data?

2
  • Your result is array and each object in array is dictionary so basically which index you are talking about? Commented Nov 7, 2016 at 6:19
  • Actually, I wanted a way that to get index from object. Commented Nov 7, 2016 at 7:00

1 Answer 1

2

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) {
    print(index)
}
Sign up to request clarification or add additional context in comments.

1 Comment

When I achieved data, using this data, I hope get a index of data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.