swift 为什么我不能在程序中放置(for in)循环

hc8w905p  于 2023-09-30  发布在  Swift
关注(0)|答案(3)|浏览(102)

我正在尝试编写一个简单的图片显示应用程序。我想初始化数组时,应用程序启动,但我不知道在哪里放置的代码。代码本身没有错误,但我不能执行它。

struct ContentView: View {
    
    @State private var pictureNameArray = [String]()
    for i in 0...4 {
        pictureNameArray.append("imaage\(i)")
    }
    //["image0", "image1", "image2", "image3","image4"]
// I want to make array with this item

    @State private var pictureNameArrayIndex1 = 0
    @State private var pictureNameArrayIndex2 = 1


    func changePicture() {
           repeat {
            pictureNameArrayIndex1 = Int.random(in: 0 ... 4)
            
            pictureNameArrayIndex2 = Int.random(in: 0 ... 4)
            
            print(pictureNameArrayIndex1, pictureNameArrayIndex2)}
        while pictureNameArrayIndex1 == pictureNameArrayIndex2 
    }
    

    
    var body: some View {
        VStack {
            Label("Grand Son", systemImage:  "sun.and.horizon.circle").font(.title)
            Image(pictureNameArray[pictureNameArrayIndex1])
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 300, height: 200, alignment: .center)
            Image(pictureNameArray[pictureNameArrayIndex2])
                .resizable()
                .aspectRatio(contentMode: .fit)    
                .frame(width: 300, height: 200, alignment: .center)
            

            
            Button(action: changePicture) {
                Label("Push Here", systemImage: "arrow.up").font(.title)
            }
            
                        
            }
                
                
            }
            
        
    
}
arknldoa

arknldoa1#

如果我们按照Leo Dabus在评论中的建议初始化数组,然后通过只处理数组属性来简化代码,并在每次按下按钮时使用shuffle()对数组进行重新排序,我们最终会得到更少的代码。通过在显示图像时使用ForEach,我们避免了在视图代码中使用索引,这使得代码更干净,更安全。

struct ContentView: View {

    @State private var pictureNameArray = (0...4).map { "image\($0)"}

    var body: some View {
        VStack {
            Label("Grand Son", systemImage:  "sun.and.horizon.circle").font(.title)
            ForEach(pictureNameArray.prefix(2), id: \.self) { image in
                Image(image)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 300, height: 200, alignment: .center)
            }

            Button { pictureNameArray.shuffle() } label: {
                Label("Push Here", systemImage: "arrow.up").font(.title)
            }
        }
    }
}
zmeyuzjn

zmeyuzjn2#

由于您直接访问数组索引,因此在视图存在之前需要填充数组。你可以用map初始化数组,如下所示:

var pictureNameArray = (0...4).map { "image\($0)" }
struct ContentView: View {
    
    @State private var pictureNameArray = (0...4).map { "image\($0)" }
    @State private var pictureNameArrayIndex1 = 0
    @State private var pictureNameArrayIndex2 = 1
    
    func changePicture() {
        guard pictureNameArray.count > 0 else { return }
        repeat {
            pictureNameArrayIndex1 = Int.random(in: 0 ... pictureNameArray.count - 1)
            pictureNameArrayIndex2 = Int.random(in: 0 ... pictureNameArray.count - 1)
            print(pictureNameArrayIndex1, pictureNameArrayIndex2)}
        while pictureNameArrayIndex1 == pictureNameArrayIndex2
    }
    
    var body: some View {
        VStack {
            Label("Grand Son", systemImage: "sun.and.horizon")
                .font(.title)
            Image(pictureNameArray[pictureNameArrayIndex1])
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 300, height: 200, alignment: .center)
            Image(pictureNameArray[pictureNameArrayIndex2])
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 300, height: 200, alignment: .center)
            Button(action: changePicture) {
                Label("Push Here", systemImage: "arrow.up").font(.title)
            }
        }
        .padding()
    }
}

我在changePicture中添加了一些额外的安全措施,以防您决定更改数组的大小。
--更新:虽然这修复了代码并正常工作,但Joakim Danielson‘s solution更干净。

zmeyuzjn

zmeyuzjn3#

您已经将该代码放置在结构体的顶层,在那里不会执行任何代码(实际上它是无效的)。
对于这个,你可能应该把它放在结构的init中...

struct ContentView {
  init() {
    // set picture array here
  }
}

相关问题