Stack overflow when defining subscript on CKRecord in Swift
After some testing and debugging (via a subclass), I discovered that, for CKRecord
, objectForKey:
does indeed call objectForKeyedSubscript:
. Also, implementing subscript
in a Swift class that is marked @objc
implicitly (by descending from NSObject
) or explicitly means that subscript
is implemented as objectForKeyedSubscript:
.
This means that implementing subscript
on CKRecord
in an extension hides the default implementation, which causes the stack overflow.
Does subscript operator work for CKRecord?
On this post you can see an explanation why subscripting CKRecord does not work and why you also can not add your own.
Stack overflow when defining subscript on CKRecord in Swift
There is however a sample of a workaround that comes close.
Check if record subscription already exists in CloudKit
You can use fetchAllSubscriptionsWithCompletionHandler
to query all existing subs, then you can check subscriptionID
property on each sub returned in the completion handler. The objective-c version looks like:
[publicDatabase fetchAllSubscriptionsWithCompletionHandler:^(NSArray<CKSubscription *> * _Nullable subscriptions, NSError * _Nullable error)
{
NSMutableArray *subIDs = [NSMutableArray new];
for (CKSubscription *sub in subscriptions)
{
if ([sub.subscriptionID isEqualToString:@"whatever"];
{
//do some stuff
}
}
}];
However, you mention re-entering a view controller and re-running this check. Not that this check issues a request to the server each time and will thus count against your transaction and transfer quotas. I recommend instead that you run this check once on app startup, and then save the state of each sub in class variables so you don't have to keep re-querying the server repeatedly with unnecessary calls.
Type 'Any' has no subscript members after updating to Swift 3
In FIRDataSnapshot, value
is of type id
.
In Swift 3, id
is imported as Any
.
In the Firebase documentation, it says value
can be any of NSDictionary
, NSArray
, NSNumber
, or NSString
-- clearly, subscripting doesn't make sense on all of these, especially in Swift. If you know it's an NSDictionary
in your case, then you should cast it to that.
Related Topics
Swift 4: Validating Credit Card Expiration Date
How to Make Uicollectionview Reload Once It Receives Data from Firebase
Firebase Access Keys in Queryorderby
Find Out When Uikeyboard.Frame Intersects with Other Frame
How to Draw Dashed Line in Arkit (Scenekit) Like in the Measure App
Resizing Uimage When Using Sf Symbols - Uiimage(Systemname:)
Take a Full Screenshot for All Webview in Swift
Programmatically Scroll Nsscrollview to the Right
Swift Skshapenode Shapewithsplinepoints
How to Resolve Error in Unit Testing When We Have Date Comparison in Codable
Self' Is Only Available in a Protocol or as the Result of a Class Method
How to Convert Unmanaged<Cfdata> to Nsdata
Swift: Get 30 Days Before 'Specific Date'
Swiftui View Property Willset & Didset Property Observers Not Working
Usage of Withmemoryrebound with Apples Swift 3 Beta 6
Why Mark Something Final in Swift Except for Architectural Considerations