1

Getting this error and don't understand why.

This line below works. Note that both settingVersion and settingRelease are Strings:

let isInserted = sharedInstance.database!.executeUpdate(sqlStatement, withArgumentsInArray: [settingRecord.settingVersion, settingRecord.settingRelease)

But if I add an Int32 into the mix, where settingsTimesUsed is an Int32, I get the "_" is not convertible to Int32 error.

let isInserted = sharedInstance.database!.executeUpdate(sqlStatement, withArgumentsInArray: [settingRecord.settingVersion, settingRecord.settingRelease, settingRecord.settingTimesUsed])

I also get the same error if I make the final item completely explicit as an Int32, e.g.

let a = 1 as Int32
let isInserted = sharedInstance.database!.executeUpdate(sqlStatement, withArgumentsInArray: [settingRecord.settingVersion, settingRecord.settingRelease, a])

Any ideas.

Edward Hasted
  • 3,201
  • 8
  • 30
  • 48

2 Answers2

2

Adding an Int32 tries to move this from [String] to [AnyObject]. Swift will never automatically infer Any or AnyObject. You must make that explicit if you need it. In this case, that would be:

let settings: [AnyObject] = [settingRecord.settingVersion, settingRecord.settingRelease, settingRecord.settingTimesUsed]
let isInserted = sharedInstance.database!.executeUpdate(sqlStatement, withArgumentsInArray: settings)

That said, I'd make sure you really want that. This throws away type safety in running executeUpdate.

If this is an ObjC interface (which are commonly type-unsafe), then it probably actually takes an NSArray. In that case, you can just be explicit by calling:

...withArgumentsInArray: NSArray(objects: settingRecord.settingVersion, settingRecord.settingRelease, settingRecord.settingTimesUsed))
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
1

If you want to retain the type safety of the other variables, instead of casting the entire array to 'AnyObject', you could just cast that particular variable to a string to get around this issue.

For example

let settingTimesUsedString = String(settingRecord.settingTimesUsed)

let isInserted = sharedInstance.database!.executeUpdate(sqlStatement, withArgumentsInArray: [settingRecord.settingVersion, settingRecord.settingRelease, settingTimesUsedString])
perNalin
  • 161
  • 9