"Cannot find 'RealmProperty' in scope" when trying to use optional double, Swift - ios

I'm using Realm Sync to store data for my iOS app, coded in Swift. I wanted to create an optional double property (budget) for a Realm object (User_budgets). I created the object in the Realm schema and then copied in the Data model SDK that Realm produces which is as below:
import Foundation
import RealmSwift
class User_budgets: EmbeddedObject {
let budget = RealmProperty<Double>()
#objc dynamic var date: Date? = nil
}
I then get the error: "Cannot find 'RealmProperty' in scope". I tried changing the code to the below:
#objc dynamic var budget: Double? = nil
But then I get the error: "Property cannot be marked #objc because its type cannot be represented in Objective-C"
I've had a search but can't seem to find anyone who's had this issue before. There's an easy work around, which is simply to make the budget property required (non-optional), but it would be good to know how to be able to create optional double properties in the future. Can anyone help me out?

I believe you're using the wrong definition for that optional as it's only available in beta 10.8.0-beta.0:
What you have is
let budget = RealmProperty<Double>()
and for all other releases it should be
let budget = RealmOptional<Double>()
See RealmProperty and RealmOptional
oh and here's a link to all of the Support Property Types

Related

Realm crash on iOS 10 with 'String'

I have recently released a new version of our app and during beta testing, it's crashing on all iOS 10 devices but not other versions. Since we have Crashlytics, we found a strange crash message in the backend that we can confirm is the reason all iOS 10 crashing since it's 100% iOS 10 and there's like 40 of them.
It reads as follows:
Fatal Exception: RLMException
Property Article.id is declared as String, which is not a supported managed Object property type. If it is not supposed to be a managed property, either add it to ignoredProperties() or do not declare it as #objc dynamic. See https://realm.io/docs/swift/latest/api/Classes/Object.html for more information.
And here's the object:
class Article: Object {
#objc dynamic var id: String = UUID().uuidString
// others...
override static func primaryKey() -> String? {
return "id"
}
}
As you can see, this is perfectly nomral and runs fine on other iOS. In Realm's doc, it LITERALLY SAYS to use String with #objc dynamic and there's no way it's unsupported. I suspect there's nothing special about Article.id, and since Article starts with A, it happens to be the first String property of all realm Objects. Maybe somehow all Strings stopped working on iOS 10?
Can anyone offer some advice or insights?(Please don't say things like drop iOS 10 support. For now, we need it.)
We ran into the same issue a couple of times, trying to drag Realm fully into Swift. This is not really the answer but more of a workaround we've had success with when needing backward compatibility.
It's an ObjC object, not Swift.
There's something going on with the bridging, perhaps conforming to NSCopy'ing or something along those line, so just change it to read
#objc dynamic var id = NSUUID().uuidString
See the Getting Started Guide in the Models section which calls for using NSUUID
NSUUID: An object representing a universally unique value that bridges
to UUID; use NSUUID when you need reference semantics or other
Foundation-specific behavior.
Turns out it was a Realm's bug. We happen to have another app that runs just fine on iOS 10, and after some inspection we realized that it was using Realm 4.3.2, instead of 4.4.1. After we downgraded Realm to 4.3.2, this problem disappeared.

defaultCalendarForNewEvents is defined as optional however can't use optional binding to check if it's nil

I'm adding new functionality in my app, which is the ability to add an event in the default calendar set up on the phone. I get the permission and am ready to add the event. I check to see if there is an actual default calendar, but I get the error:
Initializer for conditional binding must have Optional type, not
'EKCalendar'
Now, defaultCalendarForNewEvents is an Optional (see definition below) and it should be perfectly fine to use optional binding to check if it's nil or not. What am I missing?
defaultCalendarForNewEvents definition in EKEventStore.h:
open var defaultCalendarForNewEvents: EKCalendar? { get }
I'm using Swift 3 on iOS11.2.(Edited to correct the Swift version I'm using.)
Here's the code:
if let defaultCalendar = eventStore.defaultCalendarForNewEvents { <-- error line
newEvent.title = "Some Event Name"
newEvent.startDate = Date()
newEvent.endDate = Date()
}
I asked this question at the Swift discussion forum at swift.org and got a response. So as per the response, 'defaultCalendarForNewEvents' was marked non-optional in Swift 3 by accident and that was fixed in Swift 4. That's why there was a discrepancy: documentation showing declaration in Swift 4 but optional binding failing as I'm on Swift 3. Hope this helps someone who is having the same issue.
I was also told that this issue was not release-noted as it was a minor update.
The error is telling you that the defaultCalendarForNewEvents is not, in fact, an Optional. Perhaps there is some nil-coalescing or something else happening that is not visible to you. Regardless, if the compiler is telling you it's not optional there's no need to fight for optional binding.

Opening Realm hangs after implementing new object class

I wish to support rearranging a UITableView. I have seen other answers here, here, and here recommend using a another class to manage the realm objects. The only problem is as soon as I add the class I cannot successfully open a Realm.
import RealmSwift
class Data: Object {
dynamic var id = ""
}
// Adding this class causes issues
class DataList: Object {
let list dataList = List<Data>()
}
Any ideas on what is going wrong here? When I attempt to open the Realm it just hangs: no error thrown.
Edit:
From the realm doc it says they should be declared as let.
Thanks to Tj3n for the solution. Migrate your schema or reinstall the app fixes the issue. Here is some doc on that.

Swift 2.0 Subscript issues with Dictionary

I've looked at the other questions on here about subscripting with dictionaries and I didn't see anything that quite fit what my scenario is. It may be that I'm still too new to Swift to realize it but in any case here is my scenario. I'm getting the typical can't "Subscript" dictionary with type string. I've seen the posts on here about it being an optional and needing to unwrap it however when I try that, Xcode suggests that I remove the !, so I do that, then I get the subscript error.
I've watched tons of tutorials on swift development and a lot of them use playgrounds and I never remembered seeing anyone have to do this in any of the tutorials. So I tried the same thing in a playground and it worked.
Here is what I have in the ViewController that DOESN'T work.
var validFields:Dictionary = ["loanBalanceInput":false,"cashOutInput":false,"appraisedValueInput":false,"interestRateInput":false]
func validationSuccess(sender:UITextField){
sender.backgroundColor = green
switch sender {
case loanBalanceInput:
validFields["loanBalanceInput"] = true
break
default:
break
}
}
What I've done is create a dictionary of strings that refer to textfields and their validation status to track whether or not they have been validated. The concept is that when everything in the dictionary is true, I can activate the calculate button.
However I get the "cannot subscript a value of type dictionary with an index of type string" error... However this code in a playground works...
var validatedFields:Dictionary = ["loanBalanceInput":false,"cashOutInput":false,"appraisedValueInput":false,"interestRateInput":false]
validatedFields["loanBalanceInput"]
validatedFields["loanBalanceInput"] = true
I don't understand what's going on here. Is it because this is an optional?
#IBOutlet weak var loanBalanceInput: UITextField!
I'm not unwrapping it in my switch? I'm not trying to get at the value of loanBalanceInput though, I'm just checking to see if it was the sender.
Normally a Swift Dictionary declaration needs also the type of the containing keys and values like
var validFields: Dictionary<String,Bool> = [" ...
but as the compiler can infer the type just delete the annotation.

What is the correct way to use RLMResults in Swift?

I had an xcode project and I was using Swift and Objective c code. Now in other Swift project I want use a function to load and manage some data from realm databases like:
#objc func myData(_ allData: RLMResults<RLMObject>) -> [[AnyHashable]] {
...
}
If I remove the #objc reference I get the error:
Use of undeclared type 'RLMResults'
I have imported the realm sdk and sdk-swift, how can I declare this function in Swift?
What could be the error?
Thanks!
I'm assuming you are using RealmSwift since you want to remove the #objc tag and and you are talking about a "Swift Project"
RLMResults is used for Obj-C version only. When using RealmSwift you need to use Results
Below you need to replace with the object result
import RealmSwift
func myData(_ allData: Results<Object>) -> [[AnyHashable]] {
}
Results is an auto-updating container type in Realm returned from object queries.
Results can be queried with the same predicates as List<Element>, and you can chain queries to further filter query results.
Results always reflect the current state of the Realm on the current thread, including during write transactions on the current thread.
More information can be found HERE

Resources