I'm trying to write a simple app in Swift, backed with Realm. When I write var
s within classes, I keep getting errors such as "Property cannot be implicitly @objc because its type cannot be represented in Objective-C
". Sometimes these errors (which seem to involve Objective-C, which I'm not using) appear and sometimes they don't--See screenshots below.
I'm an admitted Newbie to Swift, but I thought I understood enough to get started. Specifically:
1) Should I add "@objc
" before the class declaration?
2) Should I add "@objc
" before the dynamic var declaration?
3) It seems that a Float
var must be initialized, but not so for String
?
4) Why does the compiler complain about a declaration on one line, but not about identical syntax in the next line?
Would sure appreciate some guidance, or pointers to specific instructions for creating Realm object classes!
I reckon the error comes from calling @objc
instead of @objcMembers
.
Try change
@objc class Transaction: Object
to
@objcMembers class Transaction: Object
And remove all @objc
in front of your dynamic variables. So your final code should look like:
import Foundation
import RealmSwift
@objcMembers class Transaction: Object {
dynamic var picPath: String?
dynamic var transAmount: Float = 0.0
}
@objcMembers class Transaction: Object {
dynamic var currentBalance: Float = 0.0
dynamic var highWaterBalance: Float = 0.0
dynamic var acctName: String?
}
Our fellow Paul Hudson has described this attribute on his site hackingwithswift.