开发者

How do I find out why file I/O operations fail?

开发者 https://www.devze.com 2022-12-08 19:01 出处:网络
Several methods I\'ve used to write to files return a BOOL signifying their success, e.g. NSDictionary:

Several methods I've used to write to files return a BOOL signifying their success, e.g.

NSDictionary:

- (BOOL) writeToFi开发者_开发百科le:(NSString *)path atomically:(BOOL)flag

NSKeyedArchiver:

- (BOOL) archiveRootObject:(id)rootObject toFile:(NSString *)path

Somehow, this returned NO on one of my tester's devices. That's great (well, not really), but is there anyway to find out why these operations might have failed?


The modern way is to create NSData from the (root object|dictionary), then write that data to the desired path using writeToFile:options:error: or writeToURL:options:error:. These methods return an NSError object by reference, which contains a description of what happened, including (in at least some cases) a user-presentable error message.

To create data representing a dictionary, use NSPropertyListSerialization (if the dictionary and all of its contents, all the way down, are a valid property list) or the archiver (if not).

0

精彩评论

暂无评论...
验证码 换一张
取 消