Learning to embed Unicode into Strings

Unicode Characters

Swift comes out of the gate supporting all kinds of languages and symbols.  And it has been my relutance to learn this bit of the ecosystem that is now haunting me.  Things could have gone much smoother had I learned to use the Strings powerful unicode features a year ago.

I've made several mistakes along this journey and now with 20-20 hindsight, looking back... a hint could have saved me.  So here is some hints:

For a custom Font - you must "install" the font into your Xcode project and into the App bundle to gain access to the font inside the App.  To mistakenly install the Font into your MacOS but not into the project - well, that leads to the curious case of the font performing as expected in the Xcode Playground - but not in the Simulator's version of your App.  I did this - you, should learn from my mistake


Easy String Intropolation of Unicode

In many articles and even Apple Documentation you may see Unicode represented in currly brackets inside a string.  This format is a nice shorthand for embedding a earth image in the string.
"A nice earth image would look great here: \u{0001F30D}"

These unicode character's worked well in my Playground example.

But then refused to work in the Xcode project.  Now after lots of changes and experiments... they are working.

For a while I was thinking the shorthand of  "\u{1F30D}" (not using all 8 numbers for UInt32 was the problem).  But in current testing for this article - it's not true.

That belief caused me to write a lot of extra code (multiple by 20 characters) to use the code:
let value_five: UInt32 = 0x1D2C5     // magic
let five  =  String(UnicodeScalar(value_five)! ) 

In order to get around the intropolation not working.  I think there are just lots of moving non-synchronous parts.

The original mistaken belief...

My original problem - the Font not being installed in the App caused be to create a work around for Unicode not working.  This (mistake) caused me to use SF Symbols for the Kaktovik numerals.
Then I learned one could embed the SF Symbol (an Image really) into the String inside a Text and the embedded symbol would be scalled along with the font.  So that path seemed to be a good solution.  Then I wanted to use Unit Testing to validate the calculator.  But the symbols had no value - like real Integers so the unit testing did not work.  Next - use the UI Test... but the symbols do not have labels - so add them with the accessibility framework.  When adding an accessibilityIdentifier to the SF Symbol inside a Text the Text is modified and no longer a Text - it mutates to become a ModifiedContent object.  But this object does not have the concatenation behavior of a Text object.

This little Catch-22 problem lead me to create the workaround MyTextContainer - a poor implementation of a viewBuilder that would display multiple SF Symbols in an Array storage of KSymbols.

Keep in mind that all of this is to work around the fact that I did NOT properly install the Kaktovik Font in the first place.

But now to Test it!

Once I rework all my work arounds... I may get back to some simpler code - but I still do not trust my calculator without some good test.

How am I going to test...  I will discover this...

Unique Identifiers

One issue I've encountered is the StaticText identifier - I'm not getting unique identifiers and the XCUIElement doesn't like non-unique IDs.

But - here's my code for working with this issue.  Allowing me to validate the math is giving the correct answers and the equation is as expected.  It took lots of work, many wrong turns and problems to get to the point of being able to test the Calculator.

Now to write lots of tests!