This question already has answers here:
Objective c doesn't like my unichars?
(3 answers)
Closed 9 years ago.
I'm trying to add some specific characters to my
unichar rusLetter [] = { 'Ж', 'Й', }
I want actually to add all letters from russian alphabet. Using above line of code, i get an error:
Character too large for enclosing character literal type
Any ideas how to fix that? And maybe there is an easier way to add all letters, not to type all of them.
Thanks
the ' literal (single quote) means a char and that is too small to hold the symbols.
In ObjC use an NSString to hold it
NSString *rusLetters = #"ЖЙ";
unichar c1 = [rusLetters characterAtIndex:0];
unichar c2 = [rusLetters characterAtIndex:1];
Related
This question already has answers here:
Finding out whether a string is numeric or not
(18 answers)
Closed 7 years ago.
How can I check, if searchView contains just numbers?
I found this:
if newText.isMatchedByRegex("^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$") { ... }
but it checks if text contains any number. How can I do, that if all text contains just numbers in Swift?
Here is the solution you can get all digits from String.
Swift 3.0 :
let testString = "asdfsdsds12345gdssdsasdf"
let phone = testString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
print(phone)
you can use "^[0-9]+$" instade "^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"
This will accept one or more digits, if you want to accept only one digit then remove +
This question already has an answer here:
escape % in objective c
(1 answer)
Closed 8 years ago.
I want to write sql query in NSString, the query is:
WHERE fa_name LIKE '%anyValue%'
I wrote the following code
NSString *whereFamilyName = [NSString stringWithFormat:#"fa_name LIKE '\%%#\%'", typedFamilyName];
but it doesn't escape the % and the output is:
fa_name LIKE '%#'
To Escape a % simply write two %%.
NSString *string = [NSString stringWithFormat:#"fa_name LIKE '%%%#%%'", #"val"];
This question already has an answer here:
Unicode not converted when displayed
(1 answer)
Closed 9 years ago.
How would you add superscript inside an NSLocalized string?
I'm trying to write a superscript 2, if I do it like this, it works:
[title setText:[NSString stringWithFormat:#"CO\u00B2 %#",NSLocalizedString(#"c04View01_title", #"Title for current page")]];
But if I add the superscript to the localized string, it doesn't work, it just interprets that as 5 characters:
"c04View01_title" = "CO\u00B2 PROGRAMMERS";
[title setText:NSLocalizedString(#"c04View01_title", #"Title for current page")]];
The problem happens, when the string with the superscript is between strings, so I need to split the string in two parts, but in some languages the superscripted string ends up at the end of the sentence.
Try using an upper-case 'U' for the backslash-escape, as per Apple's documentation:
"c04View01_title" = "CO\U00B2 PROGRAMMERS";
You can also just put the character directly in the strings file, un-escaped. There is no need to backslash-encode most characters.
This question already has answers here:
What does the # symbol represent in objective-c?
(5 answers)
Closed 9 years ago.
I'm really new to Objective-C but I'm experienced in C/C++, so a few things look strange to me. I'm reading a tutorial that shows the basics of Objective-C and the author warns me about forgetting to include the # before my string. So, for instance
"Hello" vs #"Hello"
However, the author doesn't explain why the # is required or what its syntactical meaning is. So that's what I'm asking now, what does the # do?
That's a NSString literal. Without the #, you'd be creating a char *, which is not an object like NSString.
Objective-C is a super set of C, so you should know "hello" is a C string (const char *).
#"hello" is something different, it is NSString *. NSString is ObjC object, that you can send message to it and use like object. You can't do it with C string.
for example, to get length from these two string:
NSString *objcStr = #"hello";
int len = [objcStr length];
const char * cStr = "hello";
len = strlen(cStr);
The use of # designates features that are unique to Objective C and are not in raw C. In this case, if you want an Objective C NSString object as a literal, you use # otherwise you end up with a different type of character string that is not an Objective C object but rather a raw C entity.
In one of my UIView classes, I have the UIKeyInput protocol attached to gather input from a UIKeyboard. I'm trying to figure out what ascii character is being used when the space button is pushed (it's not simply ' ' it's something else it appears). Does anyone know what this asci character is or how I can figure out what ascii code is being used?
To look at the value for each character you can do something like this:
NSString *text = ... // the text to examine
for (NSUInteger c = 0; c < text.length; c++) {
unichar char = [text characterAtIndex:c];
NSLog(#"char = %x", (int)char); // Log the hex value of the Unicode character
}
Please note that this code doesn't properly handle any Unicode characters in the range \U10000 and up. This includes many (all?) of the Emoji characters.
If you really need to know what character (or code point) it actually is, use the CFMutableString function CFStringTransform()
That enables you to use transformation argument kCFStringTransformToUnicodeName to get the human readable Unicode name for example or Hex-Any to get escaped Unicode code point.
Otherwise you can do the the unichar approach to simple get the code point.