Home > Net >  Return multiple items from a function with for loop
Return multiple items from a function with for loop

Time:12-09

I apologise for the poor title, any changes are welcomed.

I have a function that checks the mounted external drives and prints them, that works perfectly, as the for loop loops printing each item out.

I want it to also return these drives concatenated as a string or an array. The problem is while it will print them all perfectly, it will only return the last item (as this is obviously the one left when the loop finished.

Any help getting it to return all mounted disks (not just the last one) would be wonderful.

func checkMountedDrives() -> String
{
    var volumeURLString = "unkmown"
    if let session = DASessionCreate(kCFAllocatorDefault) {
    let mountedVolumeURLs = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil)!
    for volumeURL in mountedVolumeURLs {
        if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volumeURL as CFURL),
            let bsdName = DADiskGetBSDName(disk) {
            let bsdString = String(cString : bsdName)
            print("           
  • Related