Hello! Here I study CoreData. I learned how to write lines, but there is no comparison ... There is an Entity entity in the Data file, there is a text line. 10 lines are written there. There is a textField. It is necessary to compare the string from the textField with the strings from CoreData.Entity.text. Through do try, I can write strings to array. But through the for loop, I can't compare them for some reason.
2 answers
let array = ["test1", "test2", "test3"] let str2 = "test3" for (index, item) in array.enumerated() { if(item as String == str2) { print("\(item) equals") } } - for (index, item) in array.enumerated () {if item as String! = sender {}} does not work. The error produces Cannot convert value of type 'UserData' to type 'String' in coercion - Igor Zexyy
- You said that you compare strings. but it turns out you are trying to compare the string and UserData. Let's make you decide and we will think further. I suppose that you want to get some string from UserData first (for example, name) - Max Mikheyenko
- I have an array array: [UserData] = [], where I pass do {array = try context.fetch (UserData.fetchRequest ())} to the data of UserData entities, where there is a text attribute, which is a string. This attribute contains 10 lines. These 10 lines should be compared with the textField line! If I did not explain correctly for the first time, I apologize! - Igor Zexyy Nov.
- instead of item versus use item.text - Max Mikheyenko
- for (index, item) in array.enumerated () {if item.text == sender {loginImage.image = UIImage (named: "sadSmile.png")}} else {loginImage.image = UIImage (named: "Smile.png "); bool1 = true}} I tried it that way. There is no error, but it does not compare the strings anyway (((((((((((((((((( - Igor Zexyy
|
You can approach this much easier.
let moc = managedObjectContext let employeesFetch = NSFetchRequest(entityName: "Employee") let firstName = "Trevor" employeesFetch.predicate = NSPredicate(format: "firstName == %@", firstName) do { let fetchedEmployees = try moc.executeFetchRequest(employeesFetch) as! [AAAEmployeeMO] } catch { fatalError("Failed to fetch employees: \(error)") } As a result, we get all the elements that are equal to "Trevor".
If you need to apply for the search, then it is better:
let resultPredicate = NSPredicate(format: "name contains[c] %@", searchText) Here we get all the elements that at least in one place the words coincide with the query.
Here is the documentation therefore about: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/FetchingObjects.html
- Trying to do as in the manual and errors fly! I understand the syntax has changed, but the manual is not corrected ... All the same problem. I can not pull out lines from CoreData and compare them with a line from textField. - Igor Zexyy
- How does this method not work - let resultPredicate = NSPredicate (format: "name contains [c]% @", searchText)? Just instead of the searchText value from the textField and instead of the name specify the value in which column to search for - Kuzin Dmitry
- It probably for earlier Swift approaches. I have no executeFetchRequest method. Instead, simply execute (NSPersistentStoreRequest) ((((((((((((((((((( - Igor Zexyy
|