How to compare two ID - ios

I have two Ids
85816465-FA7B-48B1-8AD3-7FB0A1B6C011 - 85816465-fa7b-48b1-8ad3-7fb0a1b6c011
As you can see, they are almost the same, but there is difference )
85816465-FA7B-48B1-8AD3-7FB0A1B6C011 this code i'm compile by this code
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
NSString * uuidString = (__bridge NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
CFRelease(newUniqueId);
after this insert it into database (Postgres) and database converts it onto this
85816465-fa7b-48b1-8ad3-7fb0a1b6c011
When i'm selecting this inserted Id and trying to compare it with old, Xcode gives me that they are not equal ...
any suggestions?

when you are comparing the strings convert them to Uppercase if that is the only diffrence using the method
uuidString=[uuidString uppercaseString];

Please try to use this one ...I hope it may help you
NSString *str1 = #"85816465-FA7B-48B1-8AD3-7FB0A1B6C011";
NSString *str2 = #"85816465-fa7b-48b1-8ad3-7fb0a1b6c011";
str1 = [str1 stringByReplacingOccurrencesOfString:#"-" withString:#""];
str2 = [str2 stringByReplacingOccurrencesOfString:#"-" withString:#""];
if( [str1 caseInsensitiveCompare:str2] == NSOrderedSame )
NSLog(#"ITS EQUAL");
else
NSLog(#"ITS NOT EQUAL");

Try this
NSRange r = [udidstring1 rangeOfString:udidstring2 options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
NSLog(#"Both UDID are same");
}
or you can try this
if ([udidstring1 caseInsensitiveCompare:udidstring2]==NSOrderedSame) {
NSLog(#"Both UDID are same");
}

Related

ABRecordCopyCompositeName and CFBridgingRelease crash issue

I am developing IOS application using AddressBook.
Here is my code what I used.
I am getting crash issue on substringWithRange function.
What is the crash reason?
Thank you.
NSString * sort_name = CFBridgingRelease(ABRecordCopyCompositeName(person));
if (sort_name != nil) {
[self Make_Sorting_Name:sort_name];
- (NSDictionary *)Make_Sorting_Name:(NSString *)sort_name {
NSString * sort_char = [[NSString stringWithString:[sort_name substringWithRange:NSMakeRange(0, 1)]] uppercaseString];
NSCharacterSet *nonDigits = [NSCharacterSet letterCharacterSet];
BOOL containsNonDigitChars = ([sort_char rangeOfCharacterFromSet:nonDigits].location == NSNotFound);
}
The ABRecordCopyCompositeName function might return nil or empty string sometimes. So the case needs to be checked:
NSString *sort_char = #""; //or another specific character for sorting
if (sort_name != nil && sort_name.length > 0){
sort_char = [[NSString stringWithString:[sort_name substringWithRange:NSMakeRange(0, 1)]] uppercaseString];
}

Remove particular String from NSArray not Object

I am new in IOS development.I am getting phone numbers in NSArray and this is something like this.
9429564999,
9428889239,
7878817072,
"+919408524477",
9909951666,
9879824567,
"+91 8469-727523",
"94-28-037780",
"+918460814614",
55246,
"8866-030880",
"95-37-223347",
"+919574777070",
"+917405750526",
Now i want to remove - and +91 from NSArray List not whole object.How to do this Please someone help
for (NSString *number in arrayName) {
number = [number stringByReplacingOccurrencesOfString:#"+91"
withString:#""] mutableCopy];
number = [number stringByReplacingOccurrencesOfString:#"-"
withString:#""] mutableCopy];
[arrayNameNew addObject:number];
}
Try like this :)
Happy Coding.
for (NSString *phoneNum in phoneNumArray) {
phoneNum = [phoneNum stringByReplacingOccurrencesOfString:#"+91" withString:#""];
phoneNum = [phoneNum stringByReplacingOccurrencesOfString:#"-" withString:#""];
}
NSArray *phoneNoList = [[NSArray alloc]initWithObjects:#"9429564999",#"9428889239",#"7878817072",#"+919408524477",#"9909951666",#"9879824567", #"+91 8469-727523",#"94-28-037780",#"+918460814614",#"55246",#"8866-030880",#"95-37-223347",#"+919574777070", #"+917405750526", nil];
NSMutableArray *noArr = [[NSMutableArray alloc]init];
for (NSString *no in phoneNoList) {
NSString *temp1 = [[no componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];
if (temp1.length > 10) {
NSString *temp=[temp1 substringFromIndex:2];
[noArr addObject:temp];
}
else
[noArr addObject:temp1];
}
NSLog(#"Arr Data : %#",noArr);
Try this, definitely helps to you.
Try this
for (NSString *str in arrayNumbers) {
NSString *stringWithoutNineOne = [str stringByReplacingOccurrencesOfString:#"+91" withString:#""];
NSString *stringWithoutSpace = [stringWithoutNineOne stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *stringWithoutdash = [stringWithoutSpace stringByReplacingOccurrencesOfString:#"-" withString:#""];
[refinedArr addObject:stringWithoutdash];
}
Remove special characters from array.
for (NSString *strPhoneNo in arrayPhoneNo) {
[self removeSpecialCharacter:strPhoneNo];
}
and
-(NSString*)removeSpecialCharacter:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"+91" withString:#""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
// remove extra special charecter if needed.
//mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
//mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
//mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
return mobileNumber;
}
if NSArray not update then use NSMutableArray insted of NSArray.
You can also try this....
Getting the last 10 digits of your given phone numbers.....if your requirement is fixed then try this single line code
for (NSString *string in arrayNumbers) {
NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-10, 0)];
[newArray addobject:trimmedString]
}

Split NSString from first whitespace

I have a name textfield in my app, where both the firstname maybe a middle and a lastname is written. Now I want to split these components by the first whitespace, the space between the firstname and the middlename/lastname, so I can put it into my model.
For example:
Textfield Text: John D. Sowers
String 1: John
String 2: D. Sowers.
I have tried using [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] firstObject]; & [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lastObject];
But these only work if have a name without a middlename. Since it gets the first and the last object, and the middlename is ignored.
So how would I manage to accomplish what I want?
/*fullNameString is an NSString*/
NSRange rangeOfSpace = [fullNameString rangeOfString:#" "];
NSString *first = rangeOfSpace.location == NSNotFound ? fullNameString : [fullNameString substringToIndex:rangeOfSpace.location];
NSString *last = rangeOfSpace.location == NSNotFound ? nil :[fullNameString substringFromIndex:rangeOfSpace.location + 1];
...the conditional assignment (rangeOfSpace.location == NSNotFound ? <<default value>> : <<real first/last name>>) protects against an index out of bounds error.
Well that method is giving you an array with all the words split by white space, so then you can grab the first object as the first name and the rest of the objects as middle/last/etc
NSArray *ar = [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *firstName = [ar firstObject];
NSMutableString *rest = [[NSMutableString alloc] init];
for(int i = 1; i < ar.count; i++)
{
[rest appendString:[ar objectAtIndex:i]];
[rest appendString:#" "];
}
//now first name has the first name
//rest has the rest
There might be easier way to do this, but this is one way..
Hope it helps
Daniel
I think this example below I did, solves your problem.
Remember you can assign values from the array directly, without transforming into string.
Here is an example:
NSString *textField = #"John D. Sowers";
NSArray *fullName = [textField componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]];
if (fullName.count)
{
if (fullName.count > 2)
{
NSLog(#"Array has more than 2 objects");
NSString *name = fullName[0];
NSLog(#"Name:%#",name);
NSString *middleName = fullName[1];
NSLog(#"Middle Name:%#",middleName);
NSString *lastName = fullName[2];
NSLog(#"Last Name:%#",lastName);
}
else if(fullName.count == 2)
{
NSLog(#"Array has 2 objects");
NSString *name = fullName[0];
NSLog(#"Name:%#",name);
NSString *lastName = fullName[1];
NSLog(#"Last Name:%#",lastName);
}
else
{
NSString *name = fullName[0];
}
}
I found this to be most robust:
NSString *fullNameString = #"\n Barnaby Marmaduke \n \n Aloysius ";
NSMutableArray *nameArray = [[fullNameString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
[nameArray removeObject:#""];
NSString *firstName = [nameArray firstObject];
if(nameArray.count)
{
[nameArray removeObjectAtIndex:0];
}
NSString *nameRemainder = [nameArray componentsJoinedByString:#" "];
Bob's your uncle.

How to replace special characters in NSString with unknown index

I am trying to replace some characters with unknown index like this(i need to replace this Ă,Ŏ,Ĭ,ă,ŏ,ĭ and my input nsstring can be anything):
(void)repairText:(NSString *)textToRepair{`
NSString *pom = textToRepair;`
int pomNum = [pom length];
NSLog(#"Input nsstring: %#",pom);
for (int a = 0; a<pomNum; a++) {
NSString *pomChar, *pomChar2;
pomChar = [pom substringFromIndex:a];
pomChar2 = [pomChar substringToIndex:(1)];
NSLog(#"Char to repair: %#",pomChar2);
if ([pomChar2 isEqual: #"Ă"] || [pomChar2 isEqual:#"Ŏ"] || [pomChar2 isEqual:#"Ĭ"] || [pomChar2 isEqual:#"ă"] || [pomChar2 isEqual:#"ŏ"] || [pomChar2 isEqual:#"ĭ"]) {
if ([pomChar2 isEqual:#"Ă"]) {
NSLog(#"Wrong big a");
}
if ([pomChar2 isEqual:#"Ŏ"]) {
NSLog(#"Wrong big o");
}
if ([pomChar2 isEqual:#"Ĭ"]) {
NSLog(#"Wrong big i");
}
if ([pomChar2 isEqual:#"ă"]) {
NSLog(#"Wrong small a");
}
if ([pomChar2 isEqual:#"ŏ"]) {
NSLog(#"Wrong small o");
}
if ([pomChar2 isEqual:#"ĭ"]) {
NSLog(#"Wrong small i");
}
} else {
NSLog(#"Good");
}
}
pom = [textToRepair stringByReplacingOccurrencesOfString:#"•" withString:#" kulka "];
pom = [textToRepair stringByReplacingOccurrencesOfString:#"¥" withString:#" jen "];
pom = [textToRepair stringByReplacingOccurrencesOfString:#"£" withString:#" libra "];
pom = [textToRepair stringByReplacingOccurrencesOfString:#"€" withString:#" euro "];
[self synthesize:pom];
}
But I am having trouble with 'if'. If anyone know about this, please help in this regard.
NSString *str=#"ĂdsdaĬsd";
str=[str stringByReplacingOccurrencesOfString:#"Ă" withString:#""];
str=[str stringByReplacingOccurrencesOfString:#"Ĭ" withString:#""];
NSLog(#"%#",str);
O/p :dsdasd
NSString has functions to do that for you. dataUsingEncoding:allowLossyConversion: is the method you need.
From the documentation:
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)flag
If flag is YES and the receiver can’t be converted without losing some information, some characters may be removed or altered in conversion. For example, in converting a character from NSUnicodeStringEncoding to NSASCIIStringEncoding, the character ‘Á’ becomes ‘A’, losing the accent.
Sample code:
NSString *str = #"á, é, í, ó, ú, ü, ñ";
NSData *asciiStringData = [str dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *finalString = [[NSString alloc] initWithData:asciiStringData
encoding:NSASCIIStringEncoding];
The final string will be : a, e, i, o, u, u, n
NSString * textToRepair = #"Your String";
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:#"•" withString:#"kulka"];
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:#"¥" withString:#"jen"]
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:#"£" withString:#"libra"];
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:#"€" withString:#"euro"];;
and now textToRepair is your output string with changes.
You can used this:
NSString * myString = #"Hello,";
NSString * newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
NSLog(#"%#xx",newString);
I hope this is used full to you.

IOS caseInsensitiveCompare

I have the code below that compares two values and returns if match is found, the problem is that it is case-sensitive, I googled around and found the method caseInsensitiveCompare, please help me run this program with caseInsensitiveCompare method, I am lost.
NSString *listOfnames = #"person, person1, person2, person3";
NSString *name = #"person2";
NSRange match = [listOfnames rangeOfString:name];
if(match.location == NSNotFound ){
NSLog(#"Person not found!");
}else{
NSLog(#"Found you");
}
Use the rangeOfString:options: rendition with the NSCaseInsensitiveSearch option:
NSString *listOfnames = #"person, person1, person2, person3";
NSString *name = #"PERSON2";
NSRange match = [listOfnames rangeOfString:name options:NSCaseInsensitiveSearch];
if(match.location == NSNotFound ){
NSLog(#"Person not found!");
}else{
NSLog(#"Found you");
}

Resources