Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String length change after save in Core Data

So here is a problem.

I have a string

    Белый Клык-0.fb2

NSString method length return 16

After save string in Core Data (backend - sqlite)

NSString method length return 17, but visually string stay the same

    Белый Клык-0.fb2

And obviously method isEqualToString: return NO

After spent a lot of time in experiments, i am fugure out that problem is this letter:

    й

Removing this letter solve problem.

But it is keeping driving me crazy, why something like that is happening?

Here workaround that works, but dont satisfy me:

  1. stringByReplacingPercentEscapesUsingEncoding: - need to convert string right in and after db query
  2. transliterate whole string - kinda hack

And here workaround that dosnt works:

  1. stringWithUTF8String
  2. Converting escaped UTF8 characters back to their original form

Please help me understand what is going on with string after save in Core Data.

And there is more elegant solution that i did?

like image 657
Wert1go Avatar asked Oct 22 '22 16:10

Wert1go


1 Answers

The issue might be related to unicode normalization. So Coredata seems to store the string decomposed (so й counts for 2 - one for the letter and one for the accent) and this is why you get the difference in length. If you try to decompose your original string before comparing it to what Coredata returns, it should work:

[yourOriginalString decomposedStringWithCanonicalMapping]

Now, the reason behind this is beyond my field of expertise. I constantly use coredata for managing my models and have worked multiple times with Greek / Russian strings and never had such an issue. If anyone can expand on this and shed some light I would be also very interested in the subject.

like image 174
Alladinian Avatar answered Nov 04 '22 02:11

Alladinian