Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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
SwiftUI-将数组传递给函数_Swiftui - Fatal编程技术网

SwiftUI-将数组传递给函数

SwiftUI-将数组传递给函数,swiftui,Swiftui,我正在尝试创建一个简单的应用程序,它可以记录服装项目列表中的按钮如何收到超过5次点击 我已尝试使用下面的代码,但在if costs[I].taps>=5行中不断出现错误“无法将'ContentView.tamping'类型的值转换为预期的参数类型'Int' 我觉得这可能与我试图将整个struct作为func的参数传递有关。也许我不该这么做 非常感谢您的指导 import SwiftUI struct ContentView: View { struct clothing

我正在尝试创建一个简单的应用程序,它可以记录服装项目列表中的按钮如何收到超过5次点击

我已尝试使用下面的代码,但在
if costs[I].taps>=5行中不断出现错误“无法将'ContentView.tamping'类型的值转换为预期的参数类型'Int'

我觉得这可能与我试图将整个
struct
作为
func
的参数传递有关。也许我不该这么做

非常感谢您的指导

import SwiftUI



struct ContentView: View {
    
    struct clothing {
        var type: String
        var taps: Int
    }

    @State var currentClothes = [
        clothing(type: "tshirt", taps: 0),
        clothing(type: "dress", taps: 0)
    ]

    
    var body: some View {
        NavigationView{

            List{
            
            ForEach(0..<currentClothes.count) { i in
                Button("\(currentClothes[i].type)") {
                    self.currentClothes[i].taps += 1
                    print(currentClothes)
                }
                    
                }
            }
            Text("\(tapCount(clothes: currentClothes))")
        }
    }
    
    func tapCount(clothes: [clothing]) -> Int {

        var total = 0
        for i in clothes {
            if clothes[i].taps >= 5
            {total += 1}
         }
        
         return total
        

    }
}
导入快捷界面
结构ContentView:View{
结构服装{
变量类型:字符串
变量:Int
}
@状态变量currentCloth=[
服装(类型:“T恤”,丝锥:0),
服装(类型:“连衣裙”,水龙头:0)
]
var body:一些观点{
导航视图{
名单{
ForEach(0..Int{
var总计=0
因为我穿着衣服{
如果衣服[i],点击>=5
{total+=1}
}
返回总数
}
}
应该是

for i in clothes {
    if i.taps >= 5
    {total += 1}
}
不是

因为你是在
衣服
上循环,而不是
衣服。count
之类的。
i
衣服
,而不是
Int

作为旁注,尽量使类和结构保持大写。
衣服
会更好,因为衣服应该是这样的

for i in clothes {
    if i.taps >= 5
    {total += 1}
}
不是

因为你是在
衣服
上循环,而不是
衣服。count
之类的。
i
衣服
,而不是
Int


作为旁注,请尽量使您的类和结构保持大写。
clothing
作为
clothing

会更好谢谢您!我似乎很接近!FPL您是!我添加了一个关于命名约定的注释btw。谢谢!我似乎很接近!@FPL您是!我添加了一个关于命名约定的注释btw。