Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCocoaErrorDomain Code=256 File couldn’t be opened

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

like image 478
sam chi wen Avatar asked Mar 02 '18 09:03

sam chi wen


1 Answers

I find the problem is about the URL.

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

then Data(contentsOf: URL) can work fine.

like image 172
sam chi wen Avatar answered Oct 20 '22 02:10

sam chi wen