This bug shows an inconsistency in RTFD serialization.
Bug report: #FB12302537
Xcode Version 14.3.1 (14E300c) macOS 13.4 (22F66)
- Serialize the RTFD document with images to
Data
usingNSAttributedString.rtfd(from:)
and write it to a file. - Read data from that file.
- Create a new
NSAttributedString
using.init(rtfd:)
. UseData
from #2. - Serialize it again using
NSAttributedString.rtfd(from:)
. - Compare the initial
Data
and the newData
.
There is a few bytes difference between those two. It happens only if you include some images in the RTFD.
It looks like the current time is stored for image attachements. Probably a creation/modification date? Every time I run this code the byte that differs is increased like a timer.
For the same input, we should always get the same output.
let path = URL(filePath: FileManager.default.currentDirectoryPath).appendingPathComponent("sample.data")
let oldRtfdData = try! Data(contentsOf: path)
let attr = NSAttributedString(rtfd: oldRtfdData, documentAttributes: nil)!
let newRtfdData = attr.rtfd(from: .init(location: 0, length: attr.length))!
// Even if you overwrite data, the next time it will be again different.
// try! newRtfdData.write(to: path)
let diff = zip(oldRtfdData, newRtfdData)
.filter {
if $0 != $1 { print("\($0) != \($1)") }
return $0 != $1
}
.count
print(diff)
print(oldRtfdData == newRtfdData)