12

The file were created in old project in Objective-C.

NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataForWrite];
[archiver encodeObject:dictVer forKey:@"cityVersionDict"];
[archiver finishEncoding];
BOOL flag = [dataForWrite writeToFile:path atomically:YES];

I want to read this file in new project in Swift4.0 by the code like this:

do {
    let data = try Data.init(contentsOf: path)
} catch {
    print(error)
}

then catch error: Error Domain=NSCocoaErrorDomain Code=256 "The file “cityVersionDict.archiver” couldn’t be opened." UserInfo={NSURL=/Users/sam/Library/Developer/CoreSimulator/Devices/51CD0088-EE62-4ED0-8660-4C6486BC7823/data/Containers/Data/Application/5740ADE1-4930-4968-B86F-7E2F5F99F5F8/Library/Caches/cityVersionDict.archiver}

In Objective-C ,this file can be read normally .I already double check the path is right. but still catch error. please help, thanks

sam chi wen
  • 621
  • 6
  • 10

1 Answers1

42

I find the problem is about the URL.

The URL must use init(fileURLWithPath: String) to created,

then Data(contentsOf: URL) can work fine.

Mojtaba Hosseini
  • 47,708
  • 12
  • 157
  • 176
sam chi wen
  • 621
  • 6
  • 10
  • 6
    LOL! Just spent an hour trying to change the permissions of my file. Thanks for this :D Filed under "Things I will never forget again." :D – Fogmeister Apr 19 '18 at 10:43
  • 1
    But I am getting Media url as "assets-library://asset/asset.MP4?id=F65960BD-9AD8-4BBC-8C17-12345F10EB74E&ext=MP4". I think this is the problem and not able to figure out why? Any help?? – Mansuu.... Dec 06 '18 at 05:38