What's the equivalent of NSArray's writeToFile: atomically: in Swift and Array?

前端 未结 2 1720
再見小時候
再見小時候 2020-12-19 16:32

I\'ve been learning about Swift and building an Cocoa app which is based on Swift, and faced the issue that Swift\'s built-in Array type doesn\'t have wri

2条回答
  •  独厮守ぢ
    2020-12-19 16:59

    This will work...

    let array = ["One", "Two", "Three"] as NSArray
    array.writeToFile("/Users/xxx/Desktop/trial.txt", atomically:true)
    

    Output is:

    
    
    
    
        One
        Two
        Three
    
    
    

    But you must cast it as an NSArray

    let array = ["One", "Two", "Three"]  // Native Swift array will not work
    array.writeToFile("/Users/xxx/Desktop/trial.txt", atomically:true)
    

    will not work.

提交回复
热议问题