我有以下两个模型(用户和项目):
class User: Object { // Users that logged in app in specific device atleast once
@objc dynamic var serverId: Int = 0
@objc dynamic var firstName: String = ""
@objc dynamic var lastName: String = ""
@objc dynamic var email: String?
@objc dynamic var company: String?
@objc dynamic var phoneNumber: String = ""
@objc dynamic var syncBaseTime: String = ""
let projects = LinkingObjects(fromType: Project.self, property: "user")
convenience init(_serverId: Int, _firstName: String, _lastName: String, _phoneNumber: String) {
self.init()
self.serverId = _serverId
self.firstName = _firstName
self.lastName = _lastName
self.phoneNumber = _phoneNumber
}
override static func primaryKey() -> String? {
return "serverId"
}
}
class Project: Object {
@objc dynamic var serverId: Int = 0
@objc dynamic var name: String = ""
@objc dynamic var stateId: Int = 0
@objc dynamic var stateName: String = ""
@objc dynamic var cityId: Int = 0
@objc dynamic var cityName: String = ""
@objc dynamic var user: User?
@objc dynamic var isOwner: Bool = false
@objc dynamic var isActive: Bool = false
@objc dynamic var compoundKey: String = ""
@objc dynamic var syncDetailTime: String = ""
let accountTitles = LinkingObjects(fromType: AccountTitle.self, property: "project")
let notes = LinkingObjects(fromType: Note.self, property: "project")
convenience init(_serverId: Int, _name: String, _stateId: Int, _stateName: String,
_cityId: Int, _cityName: String, _user: User, _isOwner: Bool, _isActive: Bool) {
self.init()
self.serverId = _serverId
self.name = _name
self.stateId = _stateId
self.stateName = _stateName
self.cityId = _cityId
self.cityName = _cityName
self.user = _user
self.isOwner = _isOwner
self.isActive = _isActive
self.compoundKey = "\(self.user!.serverId)-\(self.serverId)"
}
override static func primaryKey() -> String? {
return "compoundKey"
}
}
当我想对它们执行查询并获取当前用户正在使用它们的项目,但当前未选择它们时,会出现问题:
self.realm.objects(Project.self).filter(NSPredicate(format: "user.serverId == %@ && isActive == true && serverId != %@", self.currentUser.serverId, self.currentProject.serverId))
我收到此错误,没有来自Xcode的更多信息:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x11)
我找不到我的错误在哪里,对此我将不胜感激。
答案 0 :(得分:1)
您在格式字符串中使用了错误的说明符,local
对于对象仅 ,而%@
不是Int
< / p>
将NSPredicate
用于%ld
Int
注意:这是Swift。请不要在下划线字符前使用难看的 objective-c-ish 变量名。 self.realm.objects(Project.self).filter(NSPredicate(format: "user.serverId == %ld && isActive == true && serverId != %ld", self.currentUser.serverId, self.currentProject.serverId))
确实可以编译。