如果列表项在swift中等于某个数字,是不是可以进行if语句?

Posted

技术标签:

【中文标题】如果列表项在swift中等于某个数字,是不是可以进行if语句?【英文标题】:Is it possible to make an if statement if a list item is equal to a certain number in swift?如果列表项在swift中等于某个数字,是否可以进行if语句? 【发布时间】:2020-03-16 00:05:56 【问题描述】:

我一直在开发一个真心话大冒险应用程序,在这个应用程序中,你转动一个***,你会随机得到一个真心话大冒险,我希望将一堆随机的真心话大冒险问题保存在 database 中。

我已经设置好了,所以当你按下旋转 button 时,它会随机选择一个真理或敢于从 array 中选择一个。 我已经尝试了一点,似乎无法让它工作。所以我的问题是有可能做类似的事情, if self.number[0] == 0 || 2 || 3 (this is where it would get the random truth) else(this is where it would get the random dare)

我不知道它是否会有所帮助,但这是我的代码的其余部分。

import SwiftUI
import CoreHaptics



class Colors
    static var drinkGradient = [LinearGradient(gradient: Gradient(colors: [Color("drinkcard"), Color("drinkcard2"), Color("drinkcard")]), startPoint: .top, endPoint: .bottom)]
    static var truthGradient = [LinearGradient(gradient: Gradient(colors: [Color("truthcard"), Color("truthcard"), Color("truthcard")]), startPoint: .top, endPoint: .bottom)]
    static var dareGradient = [LinearGradient(gradient: Gradient(colors: [Color("darecard"), Color("darecard"), Color("darecard")]), startPoint: .top, endPoint: .bottom)]


struct ContentView: View 



    @State private var timer:Timer!
    @State private var text = [String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Drink!!")]
    @State private var foregrounds = [Color.white, Color.white, Color.white, Color.white, Color.white, Color.white, Color.black]
    @State private var number = [0]
    @State private var timeKeeper: Float = 0
    @State private var angle: Double = 0
    let generator = UINotificationFeedbackGenerator()
    @State var backgrounds = [Colors.truthGradient[0], Colors.dareGradient[0], Colors.truthGradient[0], Colors.dareGradient[0], Colors.truthGradient[0], Colors.dareGradient[0], Colors.drinkGradient[0]]
    var body: some View 

        ZStack
            Rectangle()
                .foregroundColor(Color("background"))
                .edgesIgnoringSafeArea(.all)
            VStack
                HStack 
                    Text("Truth, Dare or Drink")
                        .shadow(radius: 10)
                        .padding(.top, 20)
                        .foregroundColor(.white)
                        .font(.system(.largeTitle, design: .rounded) )
                        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 100)
                        .background(Color("banner"))
                        .edgesIgnoringSafeArea(.top)
                        .shadow(radius: 15)
                


                Text("This is where the Truth or dare will go.")
                    .frame(width: 350, height: 200)
                    .foregroundColor(.white)
                    .font(.system(.headline, design: .rounded))

                Spacer()

                Text(text[number[0]])
                    .font(.system(.largeTitle, design: .rounded))
                    .foregroundColor(foregrounds[number[0]])
                    .frame(width: 250, height: 250)
                    .background(backgrounds[number[0]])
                    .clipShape(RoundedRectangle(cornerRadius: 15, style: .continuous))
                    .shadow(radius: 10)
                    .rotationEffect(.degrees(angle))
                    .animation(.easeIn)

                Spacer()

                Button(action: 
                    self.timer = Timer.scheduledTimer(withTimeInterval: 0.10, repeats: true, block: _ in

                        self.timeKeeper += 0.10
                        if self.timeKeeper < 3
                            if self.number[0] == self.text.count - 1
                                self.number[0] = 0
                                self.angle += 360
                                let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
                                impactHeavy.impactOccurred()
                            
                            else
                                self.number[0] += 1
                                self.angle += 360
                                let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
                                impactHeavy.impactOccurred()
                            
                        
                        else
                            self.number[0] = Int.random(in:
                                0...self.text.count - 1)
                            let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
                            impactHeavy.impactOccurred()
                            self.angle += 360
                            self.timeKeeper = 0
                            self.timer.invalidate()
                        
                    )
                ) 
                    Text("Spin")
                        .font(.system(.title, design: .rounded))
                        .foregroundColor(.white)
                        .frame(width: 250, height: 50)
                        .background(Color("button"))
                        .clipShape(RoundedRectangle(cornerRadius: 15, style: .continuous))
                        .shadow(radius: 15)
                
                Spacer()
            
        
    


struct ContentView_Previews: PreviewProvider 
    static var previews: some View 
        ContentView()
    

【问题讨论】:

self.number 是一个只有一个元素(即零)的数组有什么意义?为什么它是一个数组,为什么它会是一个零元素以外的任何东西?你想在这里做什么? 【参考方案1】:
if (self.number[0] == 0 || self.number[0] == 2 || self.number[0] == 3) 
   print("truth")
 else 
   print("dare")

【讨论】:

以上是关于如果列表项在swift中等于某个数字,是不是可以进行if语句?的主要内容,如果未能解决你的问题,请参考以下文章

如何进行`if`检查键入的单词是不是等于C中字符串列表中的某个单词? [复制]

“转到类型定义”上下文菜单项在哪里?

怎么判断一个字符串是不是包含某个字符

一个数中不含有某个数字的算法

验证下拉项在具有相同 id 的下拉字段列表中不会出现多次

在swift中打印图像后的数字列表是什么