Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从结构中的数组中获取实际产品_Ios_Swift - Fatal编程技术网

Ios 从结构中的数组中获取实际产品

Ios 从结构中的数组中获取实际产品,ios,swift,Ios,Swift,我有密码: struct CountedProduct { var productCode: String var productCountable: Bool init(productCode: String, productCountable: Bool, pricePerKg: Decimal, weightYieldInPercent: Decimal?, pricePerPortion: Decimal, countPerPortion: Int?, wei

我有密码:

struct CountedProduct
{
    var productCode: String
    var productCountable: Bool


    init(productCode: String, productCountable: Bool, pricePerKg: Decimal, weightYieldInPercent: Decimal?, pricePerPortion: Decimal, countPerPortion: Int?, weightPerPortionInGrams: Decimal?, pieceWeight: Decimal?) {
        self.productCode = productCode
        self.productCountable = productCountable
    }

}


struct Calculator
{  
    var type: String?

    let createTime: Double
    var products : [CountedProduct]?
    var activeProduct: Int? 



public mutating func getActiveProducts(index: Int)->CountedProduct{
        for (id, element) in (products?.enumerated())!{
            if id == index {
                return  (self.products?[index])!
            }
        }
        return activeProduct
    }

}  
我在函数
getActiveProducts

无法将“Int”类型的返回表达式转换为返回类型“CountedProduct”

我需要在products:
[CountedProduct]
中下载当前选择的产品。
products中的product表可以为空

您的
activeProduct
是整数类型,您在
getActiveProducts
方法中返回的
activeProduct
返回类型是
CountedProduct
,因此发生了此类错误

您可以按如下方式解决此错误

var activeProduct: CountedProduct?

您的
activeProduct
是整数类型,您在
getActiveProducts
方法中返回
activeProduct
,返回类型是
CountedProduct
,因此发生了此类错误

您可以按如下方式解决此错误

var activeProduct: CountedProduct?

错误很明显:

在函数末尾,您将返回
activeProduct
,它是一个
Int
,但返回值是
CountedProduct

我建议将
产品
声明为非可选,以消除难看的问号

var products = [CountedProduct]()
要使用此改进的功能:

public func getActiveProduct(index: Int) -> CountedProduct? {
        guard index < products.count else { return nil }
        return products[index]
    }
}  
公共函数getActiveProduct(索引:Int)->CountedProduct?{ 保护索引
您不需要循环,因为您知道索引,但必须检查索引是否小于数组中的项数。实际上,函数不是变异的,命名应该是单数形式的,因为只返回一个对象。错误很明显:

在函数末尾,您将返回
activeProduct
,它是一个
Int
,但返回值是
CountedProduct

我建议将
产品
声明为非可选,以消除难看的问号

var products = [CountedProduct]()
要使用此改进的功能:

public func getActiveProduct(index: Int) -> CountedProduct? {
        guard index < products.count else { return nil }
        return products[index]
    }
}  
公共函数getActiveProduct(索引:Int)->CountedProduct?{ 保护索引
您不需要循环,因为您知道索引,但必须检查索引是否小于数组中的项数。实际上,函数不是
变异的
,命名应该是单数形式,因为在编写代码片段时只返回一个对象

,您将
activeProduct
定义为
Int
类型,而不是
CountedProduct
类型。
getActiveProducts
的预期返回值为
CountedProduct
。因此,在使用
activeProduct
变量时,无法将返回类型强制转换为
CountedProduct
类型。这是一个简单的编译错误,请尝试理解编译器告诉您的内容,并在发布到StackOverflow上之前检查您的代码。当您编写代码段时,您将
activeProduct
定义为
Int
类型,不
CountedProduct
类型。
getActiveProducts
的预期返回值为
CountedProduct
。因此,在使用
activeProduct
变量时,无法将返回类型强制转换为
CountedProduct
类型。这是一个简单的编译错误,请尝试理解编译器告诉您的内容,并在发布到stackoverflow之前检查您的代码