Currently I am able to make array of Alphabets like below
[[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
Knowing that is available over
[NSCharacterSet uppercaseLetterCharacterSet]
How to make an array out of it?
Best Answer
The following code creates an array containing all characters of a given character set. It works also for characters outside of the "basic multilingual plane" (characters > U+FFFF, e.g. U+10400 DESERET CAPITAL LETTER LONG I).
For the
uppercaseLetterCharacterSet
this gives an array of 1467 elements. But note that characters > U+FFFF are stored as UTF-16 surrogate pair inNSString
, so for example U+10400 actually is stored inNSString
as 2 characters "\uD801\uDC00".Swift 2 code can be found in other answers to this question. Here is a Swift 3 version, written as an extension method:
Example:
(Note that some characters may not be present in the font used to display the result.)