Reputation: 588
this is my code in appDelegate didFinishLaunchingWithOptions()
var paths : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentsDirectory = paths.objectAtIndex(0)
var fileName: String = String(format: "Logger.txt")
var logFilePath : NSString = documentsDirectory.stringByAppendingPathComponent(fileName)
freopen(logFilePath, "a+", stderr)
the error am getting is cannot convert NSString to UnSafepointer. can anyone help me how should i be implementing this ?
Upvotes: 1
Views: 401
Reputation: 5702
Let go of the casting ;-)
All you need to cast is the NSArray to get you the objectAtIndex() method
let paths : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths.objectAtIndex(0)
let fileName = String(format: "Logger.txt")
let logFilePath = documentsDirectory.stringByAppendingPathComponent(fileName)
freopen(logFilePath, "a+", stderr)
Extra bonus: use let instead of var.
EDIT:
A version without NSArray & NSString:
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let fileName = String(format: "Logger.txt")
let logFilePath = NSURL(fileURLWithPath: documentsDirectory).URLByAppendingPathComponent(fileName).absoluteString
freopen(logFilePath, "a+", stderr)
Upvotes: 0
Reputation: 38833
Just remove the NSString
from the logFilePath
:
var paths : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var documentsDirectory = paths.objectAtIndex(0)
var fileName: String = String(format: "Logger.txt")
var logFilePath = documentsDirectory.stringByAppendingPathComponent(fileName)
freopen(logFilePath, "a+", stderr)
And a more safer way to do this would be like this:
let file = "Logger.txt"
let text = "A safer way to do this"
if let directory : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let path = directory.stringByAppendingPathComponent(file);
print(path)
do {
try text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding)
}
catch {
}
}
Upvotes: 1