I have a record on my CloudKit public database that was made via the NSPersistentCloudKitContainer. In Core Data, the attribute is of type [String] and when I add it to the public database, it is converted to Bytes.
When I fetch this record and try to extract the [String] like so
strings = record["CD_strings"] as! [String]
I get the following error:
Could not cast value of type '_NSInlineData' (0x7fe5ae87c2a8) to 'NSArray' (0x7fe5ad835f90).
How are you supposed to get the CKRecordValue as type [String]?
All you need to do is to have the attribute type as transformable, add NSSecureUnarchiveFromData in the transformer field and [String] in Custom Class:
Related
I have this code let Model = i.get("Model") as! String I am using it from my firestore database. I am trying to make it search that. I tried it with other fields and it worked just not with some that were integers and Model when model in my database is a String. I get this error Could not cast value of type '__NSCFNumber' (0x7f851d873250) to 'NSString' (0x7fff86cf7118).
A field in my firebase document is an array of strings which contains user ids.
When I try to initialise it in one of my views, I get the error
Cannot convert value of type 'String?' to expected element type 'Array<String>.ArrayLiteralElement' (aka 'String')
Here's what I am doing
#State var playerIds: [String] = [Auth.auth().currentUser?.uid, "SomeRandomHardcodedUserID"]
The line below works but the one mentioned above does not.
#State var playerIds: [String] = ["SomeHardcodedUID", "SomeOtherHardcodedUserID"]
My intention is to use playerIds to construct a codeable objet to send to Firestore.
The below line works with hardcoded userIds.
let newGame = Game(gameTime: gameTime, playerIds: playerIds, inProgress: gameInProgress, winnerId: winnerId)
What's the right way to go about putting current user's userId in my array?
So I figured out that the fix is to define the type of array as [String?] rather than [String], which fixed my issue.
Passing playerIds as an argument was fixed by changing my Game structs model for playerIds to also use [String?] rather than [String]
I'm just starting up with RealmSwift, and I'm trying to store an array of Strings in Realm. It doesn't work, so now I'm using List<String>() as an alternative. However, how do I convert these Realm Lists back to [String] again? And if I can't do that, are there any alternatives?
Thanks
However, how do I convert these Realm Lists back to [String] again
You can simply cast List to Array, because List has Sequence Support:
let list = List<String>()
let array = Array(list)
Bear in mind that by converting to an array you'll lose the 'dynamic' quality of a Realm collection (i.e. you'll receive a static array, whereas keeping the original List will provide automatic updating should the source change). But you can create an array by using an extension, e.g.:-
extension RealmCollection
{
func toArray<T>() ->[T]
{
return self.compactMap{$0 as? T}
}
}
Then use:-
let stringList = object.strings.toArray()
Where object is the realm object, and strings is your field.
Here are the details. how to assign an array in the realm list model.
jim.dogs.append(objectsIn: someDogs)
How to use enumeration type value in Core Data Entity attribute? except choosing transformable.
You can't store the enumeration type directly, because Core Data doesn't understand Swift enums. You'll need to declare the enumeration to have a raw value of some kind, and save the raw value in Core Data. Something like
enum Bar : Int {
case bar1;
case bar2;
}
let myBar = Bar.bar1
// Then save myBar.rawValue in Core Data as an integer type
When reading, get the integer value from Core Data and convert it to the enumeration as
let rawValue = managedObject.bar
let value = Bar(rawValue: rawValue)
I'm trying to create a dictionary of types (all types conforming to CKRecordType). This wouldn't be a dictionary of different types of objects, but an actual dictionary of different objects' types (i.e. I want to match all of the keys I'm using to save in a CKRecord field to the type of CKRecordValue that field stores). Is this possible in Swift? All of the types are named types, but I'm not sure how I'd refer to them (is there a 'type' type).
I'm looking for something like this: Dictionary<String, Type> or Dictionary<String, TypeIdentifier> or Dictionary<String, CKRecordValue.conformingTypes> etc...
EDIT
This is the code I'm currently using (in a protocol):
static var dictionaryOfKeysAndAssociatedValueTypes: Dictionary<String, CKRecordType.Type> { get }
or
static var dictionaryOfKeysAndAssociatedValueTypes: [String: CKRecordType.Type] { get }
which generates a "Use of undeclared type 'CKRecordType'" with Xcode 7.3
There is indeed a way to do this: your conformingTypes actually exists, and it's called Type:
Dictionary<String, CKRecordValue.Type>
or
[String: CKRecordValue.Type]