I am getting an error message
when I click on a button the NSOpenPanel comes up. I select a txt file and then I create a string with the contents of the txt file. I then proceed to remove all new lines and white spaces and the return type from this is an array. So far so good. But now I want to write the array out to a txt file again. I am trying to use each index of the array to be a new line in the text document. The problem looks like a type casting issue? The problem is the line highlighted in red. I am unsure how to resolve this issue?
When I set break points I can see everything is correct including the nameArray, each name is in it's own index. What am I doing wrong?
Type '[AnyObject]?' does not conform to protocol 'SequenceType'
when I click on a button the NSOpenPanel comes up. I select a txt file and then I create a string with the contents of the txt file. I then proceed to remove all new lines and white spaces and the return type from this is an array. So far so good. But now I want to write the array out to a txt file again. I am trying to use each index of the array to be a new line in the text document. The problem looks like a type casting issue? The problem is the line highlighted in red. I am unsure how to resolve this issue?
When I set break points I can see everything is correct including the nameArray, each name is in it's own index. What am I doing wrong?
Code:
@IBAction func importButton(sender: NSButton){ // Opens up the window to select a file
var openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
openPanel.canChooseFiles = true
openPanel.beginWithCompletionHandler { (result) -> Void in
if result == NSFileHandlingPanelOKButton {
let URLString = openPanel.URL
let pathStringFromURL = URLString?.absoluteString
let checkExt = pathStringFromURL?.pathExtension
if(checkExt == "txt"){
let documentDirectoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL
let fileDestinationUrl = documentDirectoryURL.URLByAppendingPathComponent("names2names.txt")
var inString = NSString(contentsOfURL: URLString!, encoding: NSUTF8StringEncoding, error: nil)
let nameArray = inString?.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString:" ,\n"))
[B][COLOR="Red"]let str = "\n".join(nameArray)[/COLOR][/B]
str.writeToURL(fileDestinationUrl, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
println(nameArray!)
}
else{
println("NO, not equal to")
}
}
}
}