0

For instance, asume

var hierarchicalFileSystem: [[String]] = []

This allows one to support one layer of folders, but there appears to be no way to create an array in Swift like the one above but with an undefined number of nested String arrays.

Am I missing something here?

4 Answers 4

2

An array of arrays (of arrays of arrays...) of strings doesn't really make much sense to represent a file system.

What I'd instead recommend is making a class or struct to represent objects in the file system. Perhaps something like this:

struct FileSystemObject {
    let name: String
    let extension: String?
    let isFolder: Bool
    let contents: [FileSystemObject]?
}

Something like this let's us represent a file system quite nicely.

let fileSystem = [FileSystemObject]()

So, your fileSystem variable here is an array of FileSystemObjects and it represents the root. Each object within the root has its own set of details (its name, its file extension if it has one, and whether or not its a folder), and if it's a folder it has a non-nil contents property, and if it's a non-empty folder, that contents array of FileSystemObjects contains more file system objects (some of which are folders of course, which contain contents themselves).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this works much better, thank you. I presume however that an array would be more efficient, yes?
More efficient for what? There's actually not a way for you to really represent the file structure without doing something like this. If you just want purely arrays, the only thing you can represent are the actual non-folder files--it'd be impossible to represent the file path to that folder. An array of arrays (of arrays of arrays...) of strings has no way of representing any folder names.
2

What you can perhaps do is create an array with AnyObject and add new depths as you need it

var fileSystem: [AnyObject] = []

This would be a very bad way of representing a file system however and you should really go with some kind of tree structure like

struct Node {
    children: [Node]?
    parent: Node?
    name: String 
}

5 Comments

If I were to use something like this to create a snapshot of the filesystem, and then store the result of that in an AnyObject array, would that be better?
Better than what? If you use this, you should store it in an array of [Node]. Don't ignore all the advantages that Swift's type-safety give you...
@BrandonBradley it's also worth noting that if you use some sort of data structure that maintains a pointer back to the parent object, if you use a class instead of a struct, the parent needs to be weak.
It would 1) be faster to access and 2) use less memory. From what I understand, using the array method I described is possible, however, the tradeoff between complexity and efficiency in this case isn't worth it.
@BrandonBradley It's not possible to use the array method and keep track of the names of the folders. You can only keep track of the files (or empty folders potentially). Your arrays represent folders and the strings within them represent the files. Your arrays can't have names, therefore using multidimensional arrays makes it only possible to keep track of what all the files are, and where they are relative to other files, with anonymous folders... is that useful to you?
0

Swift is type-safe language. You have to declare type of your variable, or set it to AnyObject, but please don't. So, answering your question: yes it's possible:

var array: [AnyObject] = [[[1,2,3], [1,2,3]], [[1,2,3],[1,2,3]]]

But this is awful. Try to figure out better representation for your problem. Maybe custom structures.

Comments

-1

you can have as much dimensional array as you want. is it a good idea? i don't think ...

var threeDArray: Array<Array<Array<String>>> = []
let oneDArray = ["1","2","3"]
let twoDArray1: Array<Array<String>> = [oneDArray, oneDArray, oneDArray, oneDArray, oneDArray]
let twoDArray2 = twoDArray1 + [["4","5","6"],["7","8","9"]]
threeDArray.append(twoDArray1)
threeDArray.append(twoDArray2)
let arr = [threeDArray,threeDArray,threeDArray]

print(arr.dynamicType) // Array<Array<Array<Array<String>>>>

1 Comment

This doesn't solve the problem. He's looking for how to create a dynamic number of dimensions. Your solution creates three dimensions instead of two, but it's still not dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.