So as you mentioned that I shouldn't construct URL from
Bundle.main.url, I am not very clear about it.
Ah, confusion! I interpreted
Bundle.main.url literally and thus assumed you were constructing a URL based of the bundle’s base URL (I forgot that this property is actually called
Bundle.main.bundleURL). You can avoid this confusion in future by referring to methods via their full signature, for example,
url(forResource:withExtension:).
I found that the Data(contentsOf:) still reports that the file
cannot be found.
OK, that’s an important point.
Third, since the affirmative module is entered in the if judgment,
then a valid URL should have been constructed.
Agreed. I didn’t realise you were constructing
lunarPath via
url(forResource:withExtension:).
Honestly, I’m struggling to think of what else might be causing this. To test my normalisation theory I ran the Chinese component of your URL through this code:
Code Block | func p(_ s: String) { |
| debugPrint(Data(s.utf8) as NSData) |
| } |
| let s = "天文算法" as NSString |
| p(s.precomposedStringWithCanonicalMapping) |
| p(s.precomposedStringWithCompatibilityMapping) |
| p(s.decomposedStringWithCanonicalMapping) |
| p(s.decomposedStringWithCompatibilityMapping) |
This proved that normalisation isn’t an issue in that all three strings printed:
Code Block | <e5a4a9e6 9687e7ae 97e6b395> |
I then put this code into a small test project:
Code Block | let lunarPath = Bundle.main.url (forResource: "lunar", withExtension: "js", subdirectory: "天文算法")! |
| let d = try! Data(contentsOf: lunarPath) |
| print(d.count) |
along with a bundled text file called
天文算法/lunar.js. This code works on 14.4.2 but fails on my 10.3.3 test device.
Weird.
I then added this to the code to see what the path looks like:
Code Block | print(lunarPath.absoluteURL) |
It printed this:
Code Block | file:///var/containers/Bundle/Application/EDF12AD5-8E5A-49D1-A6ED-00002718A1A4/Test678374.app/lunar.js |
which is obviously broken because the
天文算法 path component is completely missing.
If I rename the
天文算法 directory to something ASCII (like
tianwen-suanfa), and change the code to match, it works. So this problem is tied to the directory name, and probably to the characters in that name.
IMPORTANT I striped the diacriticals from the pinyin because they are subject to the normalisation issues I mentioned earlier.
So, what’s your next step? If I were in your shoes I’d simply rename the
天文算法 directory to something ASCII and then move on with your life. To quote my first post:
My general advice is that you not use non-ASCII characters for the
names of files within your bundle. It should work but there are some
gnarly edge cases.
Clearly you’ve found another one of these gnarly edge cases.
Alternatively, if you’d like me to dig deeper into this, you could open a
DTS tech support incident. That’ll allow me to spend more time researching this. However, the result of this research might be that a) this is a Just a Bug™, and b) the best way to work around it is to rename your
天文算法 directory to something ASCII, which is why I’m recommending that up front.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"