How to print the value from the coredata dictionary in swift "AnyObject" does not have a name named "username"?

I am trying to print the "username" value from my coredata object.

var request = NSFetchRequest(entityName: "Users") request.returnsObjectsAsFaults = false var results = context.executeFetchRequest(request, error: nil) if (results?.count > 0) { for result: AnyObject in results! { println(result.username) } } 

The line println (result.username) gives me a compilation error 'AnyObject' does not have a member named 'username'.

+2
source share
3 answers

You must point the array of the managed object to the correct type:

 for result in results! as [Users] { println(result.username) } 

It is assumed that you have created a subclass of the managed object for the Users object.

You should also distinguish whether executeFetchRequest() returned nil (i.e. the query to fetch failed) or 0 (i.e. no objects were found), and use the error parameter:

 var error : NSError? if let results = context.executeFetchRequest(request, error: &error) { if (results.count > 0) { for result in results as [Users] { println(result.username) } } else { println("No Users") } } else { println("Fetch failed: \(error)") // Handle error ... } 

Update for Swift 2 / Xcode 7 with try / catch error handling:

 do { let results = try context.executeFetchRequest(request) as! [Users] if (results.count > 0) { for result in results { print(result.username) } } else { print("No Users") } } catch let error as NSError { // failure print("Fetch failed: \(error.localizedDescription)") } 

Please note that forced as! [Users] as! [Users] is acceptable here. The returned objects are always instances of the corresponding class that were configured in the inspector of the Core Data model, otherwise you are a programming error that must be detected earlier.

+3
source

Martin's answer definitely allows you to access the properties of your object, but actuation is mandatory. Whether you like it or not, a strong Swift type system is the future. When you return the results of a sample query, you might consider testing for this type.

 func executeFetchRequestT<T:AnyObject>(request:NSFetchRequest, managedObjectContext:NSManagedObjectContext, error: NSErrorPointer = nil) -> [T]? { var localError: NSError? = nil if let results:[AnyObject] = managedObjectContext.executeFetchRequest(request, error: &localError) { if results.count > 0 { if results[0] is T { let casted:[T] = results as [T] return .Some(casted) } if error != nil { error.memory = NSError(domain: "error_domain", code: 0, userInfo: [NSLocalizedDescriptionKey: "Object in fetched results is not the expected type."]) } } else if 0 == results.count { return [T]() // just return an empty array } } if error != nil && localError != nil { error.memory = localError! } return .None } 

Using this approach, you can enter your results and get an error if the type is incorrect.

 var fetchError:NSError? = nil if let results:[Users] = executeFetchRequestT(fetchRequest, managedObjectContext: managedObjectContext, error: &fetchError) { for user in results { // access the results with confidence of the correct type } } else { // should have an error condition, handle it appropriately assertFailure("something bad happened") } 
+1
source

Change the for loop to

  for result: AnyObject in results! { if let user: AnyObject = result.valueForKey("username") { println(user) } } 

The fix uses valueForKey ("String")

+1
source

Source: https://habr.com/ru/post/977118/


All Articles