最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - Why do I get "Subscript requires that Float conform to Value" when I try to select a field of type Flo

programmeradmin0浏览0评论

With SQLite.swift 0.15.3 in XCode 16 I'm trying to select a field of type Float but I get an error assigning it a variable. The field is defined as let iAmount = Expression<Float>("Amount");. When I try to populate a variable I get:

Subscript 'subscript(_:)' requires that 'Float' conform to 'Value'

The code:

for ri in try db.prepare(dtRecipeIngredients.order(iName).where(iUUIDRecipe == r[rUUID]))
{
let amount : Float = ri[iAmount]

let newIngredient = Ingredient (
    Name: ri[iName]
    , Qnty: amount
)

The error occurs at compile time, not run time. Should I be declaring the expression for the field in a different way for a Float? I'm able to use String and Data type.

With SQLite.swift 0.15.3 in XCode 16 I'm trying to select a field of type Float but I get an error assigning it a variable. The field is defined as let iAmount = Expression<Float>("Amount");. When I try to populate a variable I get:

Subscript 'subscript(_:)' requires that 'Float' conform to 'Value'

The code:

for ri in try db.prepare(dtRecipeIngredients.order(iName).where(iUUIDRecipe == r[rUUID]))
{
let amount : Float = ri[iAmount]

let newIngredient = Ingredient (
    Name: ri[iName]
    , Qnty: amount
)

The error occurs at compile time, not run time. Should I be declaring the expression for the field in a different way for a Float? I'm able to use String and Data type.

Share edited Nov 20, 2024 at 0:03 user4157124 3,00214 gold badges31 silver badges46 bronze badges asked Nov 19, 2024 at 19:22 Mike MathesonMike Matheson 1 1
  • 1 Did you try Double? github/stephencelis/SQLite.swift/blob/master/Documentation/… – Joakim Danielson Commented Nov 19, 2024 at 19:34
Add a comment  | 

1 Answer 1

Reset to default 0

Swift Float simply doesn't conform to Value (this is a protocol declared by SQLite.swift).

Use Double instead, which does conform to Value. This makes sense, because floating point numbers in SQLite are always 8 bytes.

let iAmount = Expression<Double>("Amount")

You can convert it to a Float after you get the column's value if you like,

let amount = Float(ri[iAmount])
发布评论

评论列表(0)

  1. 暂无评论