【Swift】2次元配列をCSVファイルに変換してUIActivityで共有したい！

# 2次元配列をCSVファイルに変換してえ！
2次元配列をユーザーに渡したい時ってあるよね？
あれ、、意外とない、、？今ぱっと考えたけど全然思いつかない😇
とりまそういうこともたまにあるよね！
というわけで2次元配列をCSVに変換して、ついでにUIActivityで共有する方法までまとめてみました！！！！✨


## 2次元配列 → CSV

まず2次元配列とファイル名を決める

```swift:ViewController.swift
    var fruitsArray : [[String]] = [["りんご","150円"],["みかん","120円"],["バナナ","180円"]]
    var fileName = "fruits"
```
ボタンも作る！

```swift:ViewController.swift
    @IBAction func button(){

    }
```

2次元配列をCSVに変換！
Documentsの直下に配置しましょ！

```swift:ViewController.swift
func createFile(fileName : String, fileArrData : [[String]]){
        let filePath = NSHomeDirectory() + "/Documents/" + fileName + ".csv"
        print(filePath)
        var fileStrData:String = ""
        
        //StringのCSV用データを準備
        for singleArray in fileArrData{
            for singleString in singleArray{
                fileStrData += "\"" + singleString + "\""
                if singleString != singleArray[singleArray.count-1]{
                    fileStrData += ","
                }
            }
            fileStrData += "\n"
        }
        print(fileStrData)
        
        do{
            try fileStrData.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
            print("Success to Wite the File")
        }catch let error as NSError{
            print("Failure to Write File\n\(error)")
        }
    }
```

## CSV → UIActivity
共有するファイルをNSURLで指定して、UIActivityを設定！

```swift:ViewController.swift
        let shareFile = NSURL(fileURLWithPath: filePath)
        
        let activityItems = [shareFile]
        
        let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
        
        self.present(activityVC, animated: true, completion: nil)
```
# まとめ！

```swift:ViewController.swift
import UIKit

class ViewController: UIViewController {
    
    var fruitsArray : [[String]] = [["りんご","150円"],["みかん","120円"],["バナナ","180円"]]
    var fileName = "fruits"

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func button(){
        createFile(fileName: fileName, fileArrData: fruitsArray)
    }
    
    func createFile(fileName : String, fileArrData : [[String]]){
        let filePath = NSHomeDirectory() + "/Documents/" + fileName + ".csv"
        print(filePath)
        var fileStrData:String = ""
        
        //StringのCSV用データを準備
        for singleArray in fileArrData{
            for singleString in singleArray{
                fileStrData += "\"" + singleString + "\""
                if singleString != singleArray[singleArray.count-1]{
                    fileStrData += ","
                }
            }
            fileStrData += "\n"
        }
        print(fileStrData)
        
        do{
            try fileStrData.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
            print("Success to Wite the File")
        }catch let error as NSError{
            print("Failure to Write File\n\(error)")
        }
        
        let shareFile = NSURL(fileURLWithPath: filePath)
        
        let activityItems = [shareFile]
        
        let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
        
        self.present(activityVC, animated: true, completion: nil)
    }   
}

```




# 引用元・参考元
http://begigrammer.hatenablog.com/entry/2017/01/01/041039
https://qiita.com/nashirox/items/56894599013d712faa0a