0

I am using following code but I am getting errors.

"can not convert value of type NSURL to int32"

at this line:

if let fileHandle = NSFileHandle(fileDescriptor: fileurl, closeOnDealloc: &err).

and getting this error

"type of expression ambiguous without more context" at this line

if !data.writeToURL(fileurl, options: .DataWritingAtomic, error: &err)

Code:

let dir:NSURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.CachesDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last as NSURL
let fileurl =  dir.URLByAppendingPathComponent("log.txt")

let string = "\(NSDate())\n"
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!

if NSFileManager.defaultManager().fileExistsAtPath(fileurl.path!) {
    var err:NSError?
    if let fileHandle = NSFileHandle(fileDescriptor: fileurl, closeOnDealloc: &err) {
        fileHandle.seekToEndOfFile()
        fileHandle.writeData(data)
        fileHandle.closeFile()
    }
    else {
        print("Can't open fileHandle \(err)")
    }
}
else {
    var err:NSError?
    if !data.writeToURL(fileurl, options: .DataWritingAtomic, error: &err) {
        print("Can't write \(err)")
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Waqar Khalid
  • 119
  • 1
  • 12
  • I have seen that post but did not help me out. – Waqar Khalid Apr 21 '16 at 20:44
  • You should post a new question with relevant code and an explanation of what issue you are having. – rmaddy Apr 21 '16 at 20:53
  • For the first error you are using the wrong `NSFileHandle` initializer. You don't have a file descriptor, you have an `NSURL`. Use the `forWritingToURL` initializer. – rmaddy Apr 21 '16 at 21:25
  • BTW - If you are going to copy the code from [another answer](http://stackoverflow.com/a/27327385/1226963), copy it properly. – rmaddy Apr 21 '16 at 21:26
  • I copied properly but there i got other error Incorrect argument labels in call (have 'forWritingToURL:error:', expected 'fileDescriptor:closeOnDealloc:') – Waqar Khalid Apr 21 '16 at 21:34
  • You should use the code from the other answer in that question since it was updated for Swift 2. – rmaddy Apr 21 '16 at 21:41
  • That code worked for me.Can you explain that code to me please – Waqar Khalid Apr 22 '16 at 18:53

1 Answers1

5

Your code looks like Swift 1 to me and even then, it's not valid Swift 1 either. If you are using Swift 2 or later, try this:

let dir = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.CachesDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last!
let fileurl = dir.URLByAppendingPathComponent("log.txt")

let string = "\(NSDate())\n"
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!

if NSFileManager.defaultManager().fileExistsAtPath(fileurl.path!) {
    do {
        let fileHandle = try NSFileHandle(forWritingToURL: fileurl)

        fileHandle.seekToEndOfFile()
        fileHandle.writeData(data)
        fileHandle.closeFile()
    } catch {
        print("Can't open fileHandle \(error)")
    }
} else {
    do {
        try data.writeToURL(fileurl, options: .DataWritingAtomic)
    } catch {
        print("Can't write \(error)")
    }
}
Code Different
  • 90,614
  • 16
  • 144
  • 163