Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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
Arrays 我只想使用一个简单的for循环 对于0中的i.._Arrays_Swift - Fatal编程技术网

Arrays 我只想使用一个简单的for循环 对于0中的i..

Arrays 我只想使用一个简单的for循环 对于0中的i..,arrays,swift,Arrays,Swift,它如何以简单的形式工作: for i in 0..<self.etype.count{ let item = self.etype[i] if (self.etype.contains("VALUE ACTIVITY")){ print(self.etype.index(of: "VALUE ACTIVITY")) } etype = [string]() 你的例子是: for index in 1...5 { print("\(ind

它如何以简单的形式工作:

for i in 0..<self.etype.count{
    let item = self.etype[i]
    if (self.etype.contains("VALUE ACTIVITY")){
        print(self.etype.index(of: "VALUE ACTIVITY"))
    }

etype = [string]()
你的例子是:

for index in 1...5 {

    print("\(index) times 5 is \(index * 5)")

}

对于0中的i..您正在尝试实现以下目标:

for i in 0..<self.etype.count {

    let item = self.etype[i]

    if (self.etype.contains("VALUE ACTIVITY")){

    print(self.etype.index(of: "VALUE ACTIVITY"))

}

etype = [string]()
但在您的具体情况下,您可以使用更短、更高效的解决方案:

import Foundation

let input = ["VALUE ACTIVITY", "BONUS ACTIVITY", "JACKPOT EVENTS", "VALUE ACTIVITY", "JACKPOT EVENTS"]

var output = [String]()

for element in input {
    if element.contains("VALUE ACTIVITY") {
        output.append(element.replacingOccurrences(of: "VALUE ACTIVITY", with: "Value"))
    } else if element.contains("JACKPOT EVENTS") {
        output.append(element.replacingOccurrences(of: "JACKPOT EVENTS", with: "Jackpot"))
    } else if element.contains("BONUS ACTIVITY") {
        output.append(element.replacingOccurrences(of: "BONUS ACTIVITY", with: "Bonus"))
    } else {
        output.append(element)
    }

}

print(output)

这是因为您正试图打印数组中字符串的索引。对于etype{}中的项,
它应该始终为您提供0,而不是1。您不需要for来使用if contains语句,或者检查值的索引。实际上,您不会在任何地方使用item或self.etype[i]。
import Foundation

let input = ["VALUE ACTIVITY", "BONUS ACTIVITY", "JACKPOT EVENTS", "VALUE ACTIVITY", "JACKPOT EVENTS"]

let output = input.map { $0.prefix(1).uppercased() + $0.split(separator: " ")[0].lowercased().dropFirst() }

print(output)