从Core Data Attribute Swift声明一个数组

时间:2022-05-30 15:57:34

I have an Entity named: Transaction and an Attribute named: amount. I would like to declare an array from amount attribute that can using the summing an array sample code

我有一个名为:Transaction的实体和一个名为:amount的属性。我想从amount属性声明一个可以使用求和数组示例代码的数组

sum = array_name.reduce(0,+)

sum = array_name.reduce(0,+)

I am trying to do this way to declare an array from Attribute

我试图这样做从Attribute中声明一个数组

var amountArray = 
 (self.transactionsArray as NSArray).value(forKey: "amount") as! NSArray
sum = amountArray.reduce(0,+)

but it's not working and throw the error

但它不起作用并抛出错误

Ambiguous reference to member '+'

1 个解决方案

#1


0  

Using Swift 4, @MartinR's suggestion should work. If it doesn't, you should provide more information about your code, maybe specifically about the details of Transaction, the type of the amount variable, and about the exact nature of transactionsArray.

使用Swift 4,@ MartinR的建议应该有效。如果没有,您应该提供有关您的代码的更多信息,可能具体关于Transaction的详细信息,amount变量的类型以及transactionsArray的确切性质。

Assuming that transactionsArray is declared as the non-optional [Transaction] and that amount is one of the Core Data numeric types,

假设transactionsArray被声明为非可选[Transaction],并且该数量是Core Data数字类型之一,

let sum = transactionsArray.reduce(0) { $0 + $1.amount }

...is correct. You might make it slightly more explicit by declaring the type of sum:

...是正确的。您可以通过声明总和的类型使其更明确:

let sum : Double = transactionsArray.reduce(0) { $0 + $1.amount }

(Make sure to use the same type as amount here, I'm just guessing Double).

(确保在这里使用与数量相同的类型,我只是猜测Double)。

It's possible to get the sum with an NSArray and the @sum operator, for example

例如,可以通过NSArray和@sum运算符获得总和

if let sum = (transactionsArray as NSArray).value(forKeyPath:"@sum.amount") as? Double {
    ....
}

The as? Double is needed because of the lack of type info on NSArray that @vadian mentioned.

作为?由于缺乏@vadian提到的NSArray类型信息,因此需要加倍。

#1


0  

Using Swift 4, @MartinR's suggestion should work. If it doesn't, you should provide more information about your code, maybe specifically about the details of Transaction, the type of the amount variable, and about the exact nature of transactionsArray.

使用Swift 4,@ MartinR的建议应该有效。如果没有,您应该提供有关您的代码的更多信息,可能具体关于Transaction的详细信息,amount变量的类型以及transactionsArray的确切性质。

Assuming that transactionsArray is declared as the non-optional [Transaction] and that amount is one of the Core Data numeric types,

假设transactionsArray被声明为非可选[Transaction],并且该数量是Core Data数字类型之一,

let sum = transactionsArray.reduce(0) { $0 + $1.amount }

...is correct. You might make it slightly more explicit by declaring the type of sum:

...是正确的。您可以通过声明总和的类型使其更明确:

let sum : Double = transactionsArray.reduce(0) { $0 + $1.amount }

(Make sure to use the same type as amount here, I'm just guessing Double).

(确保在这里使用与数量相同的类型,我只是猜测Double)。

It's possible to get the sum with an NSArray and the @sum operator, for example

例如,可以通过NSArray和@sum运算符获得总和

if let sum = (transactionsArray as NSArray).value(forKeyPath:"@sum.amount") as? Double {
    ....
}

The as? Double is needed because of the lack of type info on NSArray that @vadian mentioned.

作为?由于缺乏@vadian提到的NSArray类型信息,因此需要加倍。