如何使用MagicalRecord for Swift 4正确获取数据?

时间:2019-01-10 17:04:39

标签: ios swift core-data magicalrecord

我正在尝试从我的User类中获取数据:

@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

}

这是我的提取呼叫:

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

这是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;
})]

我如何只检索数据,而不是所有其他信息?

1 个答案:

答案 0 :(得分:0)

我需要遍历结果以从User类中提取属性。

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