Код: Выделить всё
@implementation DecodeFile
- (NSString * _Nullable)readFile:(nonnull NSURL *)selectedFile {
NSString *utf8 = [[NSString alloc] initWithContentsOfURL:selectedFile encoding:NSUTF8StringEncoding error:nil];
if (utf8) { return utf8; }
NSString *ascii = [[NSString alloc] initWithContentsOfURL:selectedFile encoding:NSASCIIStringEncoding error:nil];
if (ascii) { return ascii; }
return nil;
}
@end
Код: Выделить всё
print(try? String(contentsOf: selectedFile, encoding: .utf8))
Подробнее здесь: [url]https://stackoverflow.com/questions/79333775/swift-ascii-string-fails-but-works-in-objective-c[/url]
Мобильная версия