NSCocoaErrorDomain Code=256 File couldn’t be opened
Asked Answered
C

1

15

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

Connor answered 2/3, 2018 at 9:32 Comment(0)
C
51

I find the problem is about the URL.

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

then Data(contentsOf: URL) can work fine.

Connor answered 5/3, 2018 at 2:46 Comment(3)
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." :DItol
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??Shantung
@Shantung Have u ever solved that issue :D ?Ehtelehud

© 2022 - 2024 — McMap. All rights reserved.