Could not cast value of type '__NSCFNumber' to 'NSString' - swift

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).

Related

CloudKit bytes to array of strings

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:

conversion of Int value String using user defaults having facing an error

UserInfoDefault.saveUserType(user: String(self.type))
I have to convert a type value that is Int and I have to convert it into String value to use it into the User defaults... But facing an issue :
Cannot invoke initializer for type 'String' with an argument list of type '(Int?)'
Note the question mark, there is no init method for String that takes an optional Int. You need to unwrap it first.
let value = myOptionalInt ?? 0
let stringValue = String(value)
or use if let or guard depending on what suits your case the best

Type "Any" has no subscript members despite casting as AnyObject on Swift 3?

Recently converted code from earlier version of swift to swift 3. Got a lot of errors signifying that type "any" has no subscript members and I thought this could be fixed by casting as AnyObject, but the error persists (and therefore the code I post here does not have this cast in it). Here is the relevant code:
func textfieldTextWasChanged(_ newText: String, parentCell: CustomCell) {
let parentCellIndexPath = tblExpandable.indexPath(for: parentCell)
var address = ""
address = "\(newText)"
// TODO: add a pin to the map from input address
cellDescriptors[0][11].setValue(address, forKey: "primaryTitle")
location = cellDescriptors[0][11]["primaryTitle"]! as! String
tblExpandable.reloadData()
}
Note that cellDescriptors is defined earlier in the code as an NSMutableArray. The error shows up right after cellDescriptors[0] in both lines that it is in. Not sure how to fix this.
It's because you're using more than one subscript operator, because presumably this is something like an array of arrays. But NSMutableArray's subscript operator returns Any. As a result, cellDescriptors[0] is Any. You try to use [11] on the result, but Any doesn't accept subscripts because it's Any, not a collection type.
Casting to AnyObject doesn't help because AnyObject is also not a collection type.
What you should do is cast cellDescriptors[0] to something that accepts subscripts. The right choice depends on what kind of data you're storing in cellDescriptors, but it's presumably some kind of collection, probably an array type.
Another approach would be to change cellDescriptors to be a Swift type instead of NSMutableArray. You could specifically declare the types for each part of your data structure, and then type casting wouldn't be needed.

Cannot convert value of type 'Int' to expected argument type '_?'

Note: I'm a rookie in Swift
I'm using Former.
I'm fetching data from a realm model.
let industries = realm.objects(Industry)
Then I try to define a list of InlinePickerItem from it:
$0.pickerItems = industries.map({ industry in
return InlinePickerItem(title: industry.name, value: industry.id)
})
But XCode keeps saying: Cannot convert value of type 'Int' to expected argument type '_?', pointing to industry.id.
Am I missing something? I don't know if the issue comes from Former or from something that I don't understand in Swift. For example, what kind of type is _??
UPDATE:
After #dfri comment, attempt was unsuccessful. From my small understanding of Swift, I get that Swift gets lost. So I extracted the initialisation of the list of InlinePickerItem from the closure.
let industries = realm.objects(Industry)
let inlinePickerItems = industries.map({ industry in
return InlinePickerItem(title: industry.name, displayTitle: nil, value: industry.id)
})
let catRow = InlinePickerRowFormer<ProfileLabelCell, String>(instantiateType: .Nib(nibName: "ProfileLabelCell")) {
$0.titleLabel.text = "CATEGORY".localized
}.configure {
$0.pickerItems = inlinePickerItems
}
The error is disappeared when calling InlinePickerItem(title: industry.name, displayTitle: nil, value: industry.id) but I get something new when assigning it to $0.pickerItems which now is:
Cannot assign value of type '[InlinePickerItem<Int>]' to type '[InlinePickerItem<String>]'
Hope this will provide you with some helpful hints.
Type mismatch when assigning array to different type array
After the re-factoring of your code (after "update") its now apparent what is the source of error.
Immutable catRow is of type InlinePickerRowFormer<ProfileLabelCell, String>. From the source of [InlinePickerRowFormer] we see the that the class and its property pickerItems is declared as follows
public class InlinePickerRowFormer<T: UITableViewCell, S where T: InlinePickerFormableRow>
: ... {
// ...
public var pickerItems: [InlinePickerItem<S>] = []
// ...
}
The key here is that for an instance InlinePickerRowFormer<T,S> its property pickerItems will be an array with elements of type InlinePickerItem<S>. In your example above S is String
let catRow = InlinePickerRowFormer<ProfileLabelCell, String>
/* |
S = String */
Hence pickerItems is an array of InlinePickerItem<String> instances.
You try, however, to append the immutable inlinePickerItems to pickerItems, which means you're trying to assign an array of InlinePickerItem<Int> instances to an array with elements of type InlinePickerItem<String>; naturally leading to a type mismatch.
You can solve this type mismatch by:
Setting your catRow immutable to be of type InlinePickerRowFormer<ProfileLabelCell, Int>.

Swift 2 using Parse : could not cast value of type '__NSArrayM' to 'NSNumber'

i try to modify an array in parse using swift 2, i don't have anny issue when i build the app but when i touch the button linked to the action i got this error (see below). I already tried to modify my code using kcurrentUser or different things like that but i always get the same issue ..
Could not cast value of type '__NSArrayM' (0x10f4d58d8) to 'NSNumber' (0x10f8c7278).(lldb)
Here's my code:
PFUser.currentUser().addObject([kCurrentUser.objectForKey("Participations") as! Int + 1], forKey: "Participations")
The object for your key "Participation" is an array. You are trying to cast it as an Int.