ContentsOfFile method deprecated in swift 2.2

Viewed 10394

My code in Objective-C is

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Logger.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

When I try to convert this to swift, I am not able to convert it as the method ContentsOfFile has been deprecated. Can anyone tell me how to convert that to swift? I am working with Xcode 7.2 and swift 2.2. My code in swift is

var paths : NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    var documentsDirectory : NSString = paths[0] as! String
    var filePath = documentsDirectory.stringByAppendingPathComponent("Logger.txt")
    var error:NSError?
    let content = NSString(contentsOfFile: filePath, encoding:NSUTF8StringEncoding, error: &error)
    if let theError = error {
        NSLog("\(theError.localizedDescription)")
    }
2 Answers
Related