Array Swift中的数组

时间:2016-04-14 06:14:23

标签: ios swift swift-playground

我有一个数组如下13项。

var data =[“MonthFolder/", 
"MonthFolder/January", 
"MonthFolder/February",
"MonthFolder/March",
"DayFolder/",
"DayFolder/Monday",
"DayFolder/Tuesday",
"DayFolder/Wednesday",
"DayFolder/Thursday",
"YearFolder/",
"YearFolder/2016",
"YearFolder/2015",
"YearFolder/2014"]

我正在尝试打印一个结果如下的数组

[("MonthFolder/January","MonthFolder/February","MonthFolder/March"), 
("DayFolder/Monday","DayFolder/Tuesday","DayFolder/Wednesday","DayFolder/Thursday"),
("YearFolder/2016", "YearFolder/2015”, "YearFolder/2014")]”

我已经完成了这个

var fileName  = [AnyObject]()
var allFiles = [AnyObject]()

for item in data{

    let keyString = item.characters.last

    if keyString != "/"{
        fileName.append(item)
    }
    else if keyString == "/"{
        if fileName.count > 0{
            allFiles.append(fileName)
            fileName = []
        }
    }
}

并输出

[("MonthFolder/January","MonthFolder/February","MonthFolder/March"), 
("DayFolder/Monday","DayFolder/Tuesday","DayFolder/Wednesday","DayFolder/Thursday”)]

缺少allFiles中的第三个对象,因为else if条件在最后一次迭代时失败。我如何实现我想要的输出?谢谢。

5 个答案:

答案 0 :(得分:0)

我试过这个



    var data = ["MonthFolder/", "MonthFolder/January", "MonthFolder/February", "MonthFolder/March", "DayFolder/", "DayFolder/Monday", "DayFolder/Tuesday", "DayFolder/Wednesday", "DayFolder/Thursday", "YearFolder/", "YearFolder/2016", "YearFolder/2015", "YearFolder/2014"];
    var fileName = []
    for (item in data) {        
        var keyString = data[item].split("/");
        if(keyString[1] == "")
            {
                var keyV = keyString[0];
                fileName[keyV]= new Array();
            }
            else
            {
                fileName[keyV].push(data[item]);                
            }
    }
    console.log(fileName)

我的输出就像

[


DayFolder
    ["DayFolder/Monday", "DayFolder/Tuesday", "DayFolder/Wednesday", "DayFolder/Thursday"]

MonthFolder
    ["MonthFolder/January", "MonthFolder/February", "MonthFolder/March"]

YearFolder
    ["YearFolder/2016", "YearFolder/2015", "YearFolder/2014"]
    ]

请检查

答案 1 :(得分:0)

通过这种方式,您将得到预期的输出:

var fileName  = [AnyObject]()
var allFiles = [AnyObject]()

for item in data{

  let keyString = item.characters.last

  if keyString != "/"{
    fileName.append(item)
  }
  else if keyString == "/"{
    if fileName.count > 0{
        allFiles.append(fileName)
        fileName = []
    }
  }
}
if fileName.count != 0 {
  allFiles.append(fileName)
  fileName = []
}

希望这会对你有所帮助:)。

答案 2 :(得分:0)

功能方式:

var data = ["MonthFolder/",
            "MonthFolder/January",
            "MonthFolder/February",
            "MonthFolder/March",
            "DayFolder/",
            "DayFolder/Monday",
            "DayFolder/Tuesday",
            "DayFolder/Wednesday",
            "DayFolder/Thursday",
            "YearFolder/",
            "YearFolder/2016",
            "YearFolder/2015",
            "YearFolder/2014"]

var allFolders  = [String]()
var allFiles = [String]()

func gatherFiles(files: [String], inFolders folders: [String]) -> [[String]] {
        return folders.map { folder in
            return (files.filter { $0.containsString(folder)})
        }
    }

for item in data {
    let components = item.componentsSeparatedByString("/").filter({!$0.isEmpty})
    if components.count > 1 {
        allFiles.append(item)
    } else {
        allFolders.append(item)
    }
}

let finalArr = gatherFiles(allFiles, inFolders: allFolders)

注意

  • 缺点:通过这种方式,还有更多的迭代。
  • 优点:您可以重复使用吗?

答案 3 :(得分:0)

试试这个

data.forEach { (item) -> () in
    if item.characters.last != "/" {
        fileName.append(item)
    } else if fileName.count > 0 {
        allFiles.append(fileName)
        fileName = []
    }
}
allFiles.append(fileName)
print(allFiles)

答案 4 :(得分:0)

您还可以使用基于Dictionary struct

的更通用的方法
foreach($works as $work)
{
     // then access it like you access an array
     ....... = $work['id];
}

输出

var filesDict = [String : [String]]()

for fileName in data {
    let fileComponents = fileName.componentsSeparatedByString("/")
    let fileKey = fileComponents[0]
    let fileValue = fileComponents[1]

    var itemsArray = filesDict[fileKey]
    if itemsArray == nil {
        itemsArray = []
    }

    if fileValue != "" {
        itemsArray!.append(fileName)
    }
    filesDict[fileKey] = itemsArray
}

//conversion from Dictionary to Array
let outputArray = Array(filesDict.values)
print(outputArray)