Swift 4 in a Bad Mood, 1

As always, when new version of Swift comes out, I read through the manual and generate questions for review. So here we go with Strings. As you know, Strings are once again Collection types, so no more .characters, for which we may all be thankful.

Using the triple-quote

let x = """
he said she said
and then more
and then still more
"""

Notice, green recruit, that it’s the BOTTOM triple quote that determines where indentation starts. You can add indents, you an add \ to join a line together.

No, gentle soul, it is not String. It is SubString.

They share StringProtocol.

Just as you say. You are wiser than you look.

Did you see what I did there? It’s not always copied behind the scenes. It’s often passed by reference and a copy is only performed if you’re about to mutate the String. You get mucho performance gains because of this evil little trick. A sleight of hand, if you will. A bit of the ol’ razzle-dazzle three card monte.

It’s not really evil.

No, that would be too obvious. You have to use the

aString.append(“C”)

method.

I think you mean string interpolation. It just means plugging one string into another using variables. Just like with C and other languages.

You sure can, sweet cheeks. Just cast that sucker into a String and hey presto.

Do you want me to? Would you like me to put up a trigger warning so you don’t get a rash or something?

I do. I spend long winter nights grieving about them.

Well you managed *that* one all right. It’s a sequence of one or more unicode characters that results in a single, that means one and one only, human readable character.

Well, sort of. YOU don’t but the .count property does.

NO. It returns a count of human readable values, but to do that it has to read over every single blessed scalar in the String, so THINK before you use it, okay? Use your head for something besides a hatrack.

Only in your dreams ,sweet pea. I can think of other forms of relief that are faster and more efficient. NSString length doesn’t count human readable values, it counts 16 bit code units in a UTF 16 string representation.

Who says I get away with it?

You really live in your own little universe, don’t you? And you don’t listen. Index also has to iterate over every Unicode scalar in a string.

NO YOU CAN’T. An index doesn’t translate into a physical position in an array. No integers.

It does. I can see your mistake driving at me like a mack truck. Here it comes.

And you mowed down Granny, who was just quietly trying to cross the road to by something at the fruitstand. NO. endIndex gets the position AFTER the last character in the String.

let i2 = message.index(before: message.endIndex)

Oh, isn’t it JUST? Let’s have a highball at Wimbledon season next. I’ll look for you.

At long last they have found the dream of brotherhood and true equality. They’re equal.

Remember before:, after: and offsetBy: when using Index. They are your friends, if it’s possible for you to have friends.

Oh it does, it does! You can use before, after and offsetBy on any Collection type, like Array, Set or Dictionary.

Just as you say. You use insert and remove. If you’re inserting a character, just pass in the character. If you’re inserting a String you have to use a contentsOf: label.

Qua? Showing off the schoolboy Latin? Poser. You can test as you would expect with == and !=.

There’s a lot more than that you don’t know, oh wizard of language. The phrase is canonically equivalent. It means a string may be represented in two cases by different underlying Unicode BUT when translated to human readable, the underlying Unicode don’t make no never mind. The humans see the same letters and marks that mean so very much to them. So very, very much.

If I were you I’d just ask someone. But if you really feel like you have to use Swift, you can use hasPrefix and hasSuffix. I have no doubt you will make a raging success of it.

In your case it’s more a case of ‘I wouldn’t.’ or ‘someone should stop you.’ I can’t imagine what you would want to do with it that was within your abilities. However, if you just can’t resist, you can use the utf8, utf16 and utfScalars properities. The last one returns a 21-bit unit that’s equivalent to UTF32. That will be enough to get you into the sort of trouble you probably won’t get out of. But the nice thing is, you probably will never know it.

I can’t print what I tell them here. DO NOT HIT THEM, it will get you in trouble. Tell them this: it’s a job. It doesn’t require passion. It requires competence, insight, intelligence, discipline, confidence and ability. Passion is what you need if you’re not very good and you want people not to notice.

More Neanderthal DNA markers than 95 % of 23andMe customers. Don’t give me your pro-Cro-Magnon propaganda.