Recently, in an address book project, you need to read contacts into the app memory (there are about 400 contacts in the address book) and sort them by the first letter. Here we use the built-in IOS system interface when converting Chinese characters into pinyin:
Note that self. latinstring must be a mutable string.
// 1. convert a non-string into a Latin letter cfstringtransform (_ bridge cfmutablestringref) self. latinstring, null, kcfstringtransformmandarinlatin, no); cfstringtransform (_ bridge cfmutablestringref) self. latinstring, null, kcfstringtransformstripdiacritics, no );
However, the app is very slow to start. Because the contact is being loaded and the Chinese characters are converted into pinyin. Let's take a look at the comparison results:
If direct conversion:
> 16:14:03. 988 [contact_log] contactlist. M: 87-[contactlist loadcontactfromonoclonal]: _ addressbookengine loadadressbook10.022583
It takes 10 seconds to load the file.
My practice is: Since the people in the address book will not change frequently, You Can cache the name of all people-pinyin (dictionary). In this way, you can obtain pinyin from the dictionary, it should be much faster! The result proves my conjecture:
> 16:18:47. 992 [contact_log] contactlist. M: 87-[contactlist loadcontactfromonoclonal]: _ addressbookengine loadadressbook0.641795
Loading is less than 1 second.
Using cfstringtransform to convert Chinese characters into Pinyin is time consuming!