What is differnce between NSString and NSCharacterset? - nscharacterset

I studying NSCharacterset class.
NSString and NSCharacterset look like similar class.
Who can explain difference between NSString and NSCharacterset?
When use NSCharacterset?
Thanks your help..

An NSString object represents a string of ordered characters (text). An NSCharacterSet object represents a set of characters in no particular order. It is often much quicker to determine whether a character is a member of an NSCharacterSet than an NSString. You can't use an NSCharacterSet object to hold text, at least not in any sensible way, because it does not maintain any order of characters, its use is primarily for determining whether a character exists in a set of characters.

Related

NSAttributedString & decomposedStringWithCanonicalMapping ranges

I'm running into problems with international (in this case Korean) NSString values.
The same input string is used in two different parts of the program. The first part finds a substring that needs highlighting, stores the NSString and the range for the highlighting into a database.
The second part of the program retrieves the string and displays the highlighting.
The marking part is done using an NSString that has been normalized in Unicode Normalization Form C using the precomposedStringWithCanonicalMapping method on NSString. An NSRange and an NSString are then stored into the Core Data database.
The graphical highlighting is performed by retrieving the NSRange and NSString from the database, putting the NSString into the same Form C using the same method, using this to initialize an NSMutableAttributedString and using the NSRange to set its text attributes.
At this stage, the program crashes because the NSMutableAttributedString is 80 characters long, whereas the NSString was 81 characters long..
NSAttributedString does not have a precomposedStringWithCanonicalMapping method and I assume it changes the representation internally resulting in a different encoding and thus length.
What can I do?
is the a way of forcing NSAttributedString to keep an underlying encoding?
is there a way of converting an NSRange from one encoding to another?
or is there anything else I can do?
Ok,
I did eventually find out what had happened. In one particular place in the program I mistakenly used decomposed​String​With​Canonical​Mapping rather than precomposed​String​With​Canonical​Mapping and that's where the "wrong" mapping came from.

When would you use NSNumber literal to create encapsulated character values?

I'm just going through Apple's iOS development tutorial at the moment and reading the chapter on the Foundation framework and value objects.
Just on the NSNumber class, it says:
You can even use NSNumber literals to create encapsulated Boolean and
character values.
NSNumber *myBoolValue = #YES; NSNumber *myCharValue = #'V';
I'm just wondering, when, or why, or in what scenario, might you want to use NSNumber for a character value rather than using NSString, say?
An NSNumber is useful for encapsulating primitive values to be inserted into Objective-C collection classes such as NSArray, NSSet, NSDictionary, etc.
Image a scenario where you would want to iterate over each character in an ASCII string and extract a unique set of vowels used. You can evaluate each character and add it to an NSMutableSet. To do so, you would need to encapsulate each character in an NSNumber as NSMutableSet expects an Objective-C object. This is just one example, but the concept applies to many situations where primitives need to be added into a collection.
Well, one case is where you're using KVC to set a value for a key, and the property type is char:
[object setValue:#'a' forKey:someCharPropertyName];
You can use NSNumber with characters to return its ASCII Code, so V would return 86.
I don't think many people use it that much, but you could probably use it for character validation. I think it just one of those things where Apple went, yeah, lets put that in for the heck of it.
It's really not used for much else. The #YES and #NO is the same as YES and NO, so its kinda inelegant in some places.

Objective-C: Two 'identical' strings do not match?

I have a two strings.
Once is a response from a TCP server using NSStream events, using:
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
And one is a string produced from on the fly, that should match the returned string from the NSStream.
I have NSLog both of these out, and they are identical.
I have tried to NSLog the Lengths of the strings, and one is two characters longer - Even though they are both identical in 'text' form.
Any suggestions to point me in the right direction?
I need to know if they match, as if they do, another event will be triggered to enhance and add additional functionality to my app.
Never use == to compare strings. If their contents are character-by-character identical, you can use isEqualToString to compare 2 strings. If your strings have different lengths, though, then they are not character-by-character identical.
Write a for loop that uses the method characterAtIndex to log the characters from each string 1 at a time and compare them. You might need to log the characters' integer values so you can see info about the non-printable ones.
Thanks Guys.
Fixed with #rdelmar suggestions - I didn't know this was possible in Objective-C:
NSString *trimOutput = [output stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

How to take non-english characters from UITextField and consider them as normal characters

i have a database that contains non-english words ( for those who wonders turkish letters). And i have an algorithm which compares the input with database.
So my problem is this; in my database all the strings are written with turkish characters. So lets say i have thıs element to compare heyyö. When user enters heyyo it won't find it since they are considered as different words.
My first thought was put special cases and when a non-english character found consider whether english or non-english letter ( like g with ğ or i with ı) but that means a lot of brute force.
how can i do this with elegance.
Oh and user enters this inputs from a textfield if that wasn't implied.
The removal of diacritics is called "folding." You can compare strings without regard to diacritics using the option NSDiacriticInsensitiveSearch.
[string compare:otherString options:NSDiacriticInsensitiveSearch] == NSOrderedSame
You can similarly generate a folded string using stringByFoldingWithOptions:locale:.
Note that this only removes diacritics. There are many ways that characters can "seem" the same without being the same. Turkish is somewhat notorious about this because the lowercase version of "I" is "ı" (LATIN SMALL DOTLESS I), not "i". If you're particularly dealing with Turkish, you may have to account for this.
What you can do is something like this:
NSString *input = #"heyyö";
NSData *intermediaryDataForm = [input dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *output = [[NSString alloc] initWithData:intermediaryDataForm encoding:NSASCIIStringEncoding];
That way, because the turkish letters are not part of ASCII, and you are allowing a lossy conversion, then it automatically changes 'ö' to 'o' when converted to the NSData form. Then converting it back to NSString solves the issue.

Why use NSRange on strings when there appears to be a perfectly good substring method?

I'm learning native iOS development for the first time, and I came across the struct NSRange. I come from a Java background so I don't really see the reasoning for using a range struct when you can just use substring methods that are part of the NSString class. What is the advantage of using range structs over using the non-range NSString substring methods.
Thanks!
edit:
Looks like I was considering the substring methods: substringFromIndex: and substringToIndex:.
Considering the inflexibility of these methods (ie. not being able to choose both a start AND end point) makes the range struct instances more necessary. Though I guess you could also nest those two methods to achieve the same result.
edit 2: Examples.
Non-range substring method examples:
NSString *str = #"This is a string.";
NSString *substr = [str substringToIndex:7];
NSString *substr2 = [str substringFromIndex:7];
Ranges substring method example:
NSString *substr3 = [str substringWithRange:NSMakeRange(5, 5)];
Because the range based methods offer a lot more flexibility, and they are also easily usable with all of the NSString search methods (which use ranges heavily). In general, if you're going to create a substring you need to know where to start or end and that information is likely to have come from a search, thus you have a range.

Resources