Aнгел
Aнгел

Reputation: 1429

How to know if a file is encoded?

I have two types of files in iCloud's Docs folder:

  1. Text files
  2. Encoded files with NSCoding protocol

Both types uses .txt extension. How can I know whether a file was encoded?

Since I don't know whether a file is a text file or an encoded file, first I try to open any file as if it were encoded, hoping that any error will be caught on the open's completion handler:

var doc = MyCloudDocumentType1(fileURL: url)
doc.openWithCompletionHandler { (success: Bool) -> Void in
    if success {
        println("Doc. opened")
    }
}

When UIDocument tries to load the content of the file, I get an exception when trying to unarchive the text file:

override func loadFromContents(contents: AnyObject!, ofType typeName: String!, error outError: NSErrorPointer) -> Bool {
    if (contents as NSData).length == 0 {
        return false
    }
    NSKeyedUnarchiver.unarchiveObjectWithData(contents as NSData)
    return true
}

the exception is fired by NSKeyedUnarchiver.unarchiveObjectWithData(contents as NSData), indicating that there is an invalid argument:

'NSInvalidArgumentException', reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive

This exception makes sense, since I'm trying to decode a text file (a file that isn't encoded). The problem is that I can't catch the exception in swift, and by the time I know that the file is not an encoded one, is too late to do something.

How can I know whether a file was encoded? What's the best approach to detect if a file is encoded and thus do the encoding?

Thanks!

****** SOLUTION ******

Based on comments from @AlainCollins, I read the first 4 bytes of an encoded file as this would be my magic number:

var magicNumber = [UInt](count: 4, repeatedValue: 0)
data.getBytes(&magicNumber, length: 4 * sizeof(UInt))

Then, I compared the first 4 bytes of each file in Docs folder against this magic number.

Upvotes: 1

Views: 412

Answers (1)

Alain Collins
Alain Collins

Reputation: 16362

If you really want to know what format the file is in, check the magic number: http://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files

If you just want to survive the error, follow AJGM's try/catch advice.

Upvotes: 1

Related Questions