Sam
Sam

Reputation: 4284

Setting NSPredicate in Swift CoreData

Am Rewriting my existing objective c code(ios) to swift and now am facing some issues with Coredata NSPredicate in swift as there is some many to one relationships and vice-versa.. am using the following code,

    let fetchRequest = NSFetchRequest(entityName: "TUMessage")
    let sortSequence = NSSortDescriptor(key: "sequence", ascending: true)
    let sortTime = NSSortDescriptor(key: "epoch_time", ascending: true)
    var filterPredicate:NSPredicate = NSPredicate(format:"thread == [c] %@",contact.threads.anyObject())
    fetchRequest.predicate = filterPredicate
    var sortDescriptor:NSArray = NSArray(objects: sortTime,sortSequence)
    fetchRequest.sortDescriptors = sortDescriptor
    fetchRequest.fetchBatchSize = 50
    return fetchRequest

where "TUMessage" is the table we need to fetch data from, "sequence" for sorting the fetched result, "thread == [c] %@" is a relationship of message table (many to one since each contact have multiple thread) ,

and contact.threads.anyObject() is the predicate which am trying to add.Unfortunately am getting the following error

  type anyobject? does not confirm to protocol, CVarArgType

Any help would be appreciated..

Thank you

Upvotes: 0

Views: 1647

Answers (1)

Johan
Johan

Reputation: 1951

You need to unwrap it and cast to the specific class

contact.threads.anyObject()! as MYClass

Upvotes: 1

Related Questions