Search code examples
iosswiftcore-datamagicalrecord

How can I properly fetch data using MagicalRecord for Swift 4?


I am trying to fetch data from my User class:

@objc(User)
class User: NSManagedObject {

// Attributes
   @NSManaged var cAge: Int16
   @NSManaged var rAge: Int16
   @NSManaged var cIncome: Int64
   @NSManaged var rIncome: Int64
   @NSManaged var state: String
   @NSManaged var returnRate: Float

}

Here is my fetch call:

let user = User.mr_findAll()! as! [User]
print(user)

Here is the output from print(user):

[<User: 0x2812da300> (entity: User; id: 0x2831da740 <x-coredata:///User/t34A918DC-3B79-4ECC-91EF-16F658B20B8A2> ; 
data: {
    cAge = 23;
    cIncome = 125086;
    rAge = 50;
    rIncome = 365048;
    returnRate = "0.78";
    state = In;
})]

How can I just retrieve the data, and not all the other information?


Solution

  • I needed to loop through the results to extract the attributes from the User class.

    let users = User.mr_findAll()! as! [User]
    for user in users{
         print(user.cAge)
    }