here is my code :
NSUInteger f;
for (f = 0; f < [appDelegate.books count]; f++) {
check = [appDelegate.books objectAtIndex:f];
checkthis = check.LotteryNumber;
mystring = check.LotteryNumber;
NSLog(#"Dynamic Value: %#",mystring);
NSLog(#"Static Value: %#",checkthis);
if (checkthis == mystring) {
found = YES;
break;
}
printf("In LOOP");
}
if ( found ) {
// do found
NSLog(#"Found");
} else {
// do not found
NSLog(#"not Found");
}
//if (checkthis == mystring) {
in above line if i place checkthis on both side , its working , but when i am taking a dynamic value its not working..
i also tried like this
if(checthis isEqualToString mystring)
same problem here ....
Thanks in advance
You're using pointer comparison and not string comparison.
You should be using:
if([checkThis isEqualToString:myString]) { ...
You are using comparision by pointer the comparison you made checks memory a ddresses and will only return true if its the same object being compared, you should use [string isEqualToString:otherString] method from NSString instead
Related
I am using the condition,but it don't go to the if condition, every time goes to the else condition.
if ([[dict2 valueForKey:#"meeting_location"] isEqual:#""])
{
lbllocation.text = #"-----";
}
else
{
lbllocation.text = [NSString stringWithFormat:#"Location: %#",[dict2 valueForKey:#"meeting_location"]];
}
Output came from Webservice:-
"meeting_location" = "";
"meeting_time" = "";
"meeting_with" = "";
Use Below Code -
if ([[dict2 valueForKey:#"meeting_location"] length] == 0)
{
lbllocation.text = #"-----";
}
else
{
lbllocation.text = [NSString stringWithFormat:#"Location: %#",[dict2 valueForKey:#"meeting_location"]];
}
Hopefully this will work here.
How can I validate password for sequential value?
Eg -
if user enters 1326 or "axdf" in password then it is valid.
if use enter 1234 or "abcd" the it is invalid.
I think you can do it this way
NSString *list= #"abcdefghijklmnopqrstuvwxyz";
NSString *password= #"abz"; // Suppose this is your password
NSRange range = [list rangeOfString:password options:NSCaseInsensitiveSearch];
if (range.location == NSNotFound) {
/* Could NOT find password in list, then it is valid */
}
else {
/* Found the password in the list then it is not valid */
}
Similarly you can do it for numbers as well
the easiest way is to use strpos.
$haystack = '01234567890';
function testConsecutive($pHaystack, $pNeedle){
return strpos($pHaystack,$pNeedle) === false?false:true;
}
Oops, I thought that I was answering php code, because that was my filter. This is not php, sorry for that.
In this case, i would suggest you to try matching ASCII value of each character in the string. If the string is sequential, ASCII value of that character should increase by 1
Here is a rough idea how you can implement it, havent tested it but hope it might be helpful.
int prevAsciiCode;
NSString *string = #"12345";
for (int i = 0; i<string.length; i++) {
int asciiCode = [string characterAtIndex:i];
if (asciiCode == prevAsciiCode+1) {
NSLog(#"String invalid");
return;
}
prevAsciiCode = asciiCode;
}
A string is a sequence of chars, each char is represented by is ASCII code, so you can iterate throw the chars of the string and compare each one with its previous int value, for example:
- (BOOL)isSequence:(NSString *)string
{
char previousChar = [string characterAtIndex:0];
int wordLength = [string length];
BOOL isSequence = YES;
for (int i = 0; i < wordLength && isSequence; i++)
{
char currentChar = [string characterAtIndex:i];
if (currentChar != previousChar+1) {
isSequence = NO;
}
previousChar = currentChar;
}
return isSequence;
}
I need to remove all emoijs from a NSString.
So far i am using this NSString extension ...
- (NSString*)noEmoticon {
static NSMutableCharacterSet *emoij = NULL;
if (emoij == NULL) {
emoij = [[NSMutableCharacterSet alloc] init];
// unicode range of old emoijs
[emoij removeCharactersInRange:NSMakeRange(0xE000, 0xE537 - 0xE000)];
}
NSRange range = [self rangeOfCharacterFromSet:emoij];
if (range.length == 0) {
return self;
}
NSMutableString *cleanedString = [self mutableCopy];
while (range.length > 0) {
[cleanedString deleteCharactersInRange:range];
range = [cleanedString rangeOfCharacterFromSet:emoij];
}
return cleanedString;
}
... but that does not work at all. The range.length is always 0.
So the general question is : How can i remove a range of unicode characters from a NSString?
Thanks a lot.
It seems to me that in the above code the emoij variable is eventually an empty set. Didn't you mean to addCharactersInRange: rather than to removeCharactersInRange:?
Code:
NSString *tempPhone = [NSString stringWithFormat:#"%#", [personDict objectForKey:kPhoneKey]];
NSLog(#"NSString *tempPhone = %#",tempPhone);
Output:
NSString *tempPhone = <null>
Now I want to add if condition, for not null; I tried followings:
if (tempEmail != NULL)
if (tempPhone != Nil)
if (tempPhone != nil)
if (![tempPhone compare:#"<null>"])
if (tempPhone != (NSString*)[NSNull null])
I also Checked this Post.
None of them is working for me. Where I am going wrong??
Try the following code
id phoneNo = [personDict objectForKey:kPhoneKey];
if( phoneNo && ![phoneNo isKindOfClass:[NSNull class]] )
{
NSString *tempPhone = [NSString stringWithFormat:#"%#", [personDict objectForKey:kPhoneKey]];
NSLog(#"NSString *tempPhone = %#",tempPhone);
}
else
{
NSLog(#"NSString *tempPhone is null");
}
I think your personDict does not have an object for the key kPhoneKey, so it is returning nil. When you format nil using %#, you get the string "(null)".
id object = [personDict objectForKey:kPhoneKey];
if (object) {
NSString *tempPhone = [NSString stringWithFormat:#"%#", object];
NSLog(#"NSString *tempPhone = %#",tempPhone);
} else {
// object is nil
}
if (tempPhone != nil || [tempPhone isEqualToString:#"(null)"])
Works for me.
It's like this :
if (![tempPhone isKindOfClass:[NSNull class]] && tempPhone != nil)
if ([str_name isKindOfClass:[NSNull class]])
works for me...
I Checked the webResponse (JSON, in my case).
It was returning me the string with value:
<null>
So, The following condition worked for me:
if (![tempPhone isEqualToString:#"<null>"])
Thanks for all your time. Thanks.
Because compare method returns type NSComparisonResult which is defined as
enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
typedef NSInteger NSComparisonResult;
If the string is the same, it will return NSOrderedSame which have a NSInteger value of 0.
Thus, the following line actually means...
if (![tempPhone compare:#"<null>"]) // `tempPhone` is equals to `#"<null>"`
or in a more understandable explanation, if tempPhone value is equal to #"<null>".
You should write it as
if ([tempPhone compare:#"<null>"] != NSOrderedSame)
or
if (![tempPhone isEqualString:#"<null>"])
In case the string is having a null value. Example in NSLog.. Implement this way..
if ([StringName isKindOfClass:[NSNULL Class]]) {
}
else {
}
First we need to check string length.
if (tempPhone.length == 0)
`
{
//Your String is having empty value (E.x, (null))
}
else
{
// You having some values in this string
}`
I'm trying to retrieve data from the iPhone address book and I have some problems.
First of all, I have an array of all contacts (self.allContacts):
ABAddressBookRef abRef = ABAddressBookCreate();
self.allContacts = (NSMutableArray*)ABAddressBookCopyArrayOfAllPeople(abRef);
I also have an array of all properties (each property as string) called self.allKeys.
The crash occurs when I try to get the properties using a property from self.allKeys:
NSString *currentRecord = [[NSString alloc] init];
ABRecordRef currentRecordRef;
ABPropertyID currentFieldProperty;
currentRecordRef = (ABRecordRef)[self.allContacts objectAtIndex:i];
currentFieldProperty = (ABPropertyID)[self.allKeys objectAtIndex:j];
currentRecord = (NSString*)ABRecordCopyValue(currentRecordRef, currentFieldProperty);
The problem is that passing currentFieldProperty to ABRecordCopyValue causes a crash.
self.allContacts is an array of all contacts
self.allKeys is an array of all properties (each property as string)
When trying to retrieve a single property from ABRecordCopyValue it causes an EXC_BAD_ACCESS crash.
Thanks!
Set NSZombieEnabled, MallocStackLogging, and guard malloc in the debugger. Then, when your App crashes, type this in the gdb console:
(gdb) info malloc-history 0x543216
Replace 0x543216 with the address of the object that caused the crash, and you will get a much more useful stack trace and it should help you pinpoint the exact line in your code that is causing the problem.
- More thoughts -
If self.allKeys is indeed
"an array of all properties (each property as string)"
then you should probably get the intValue of the array object (property) since an ABPropertyID is just a typedef int32_t. Something like:
currentFieldProperty = (ABPropertyID)[[self.allKeys objectAtIndex:j] intValue];
ABRecordCopyValue(currentRecordRef, currentFieldProperty)
But we would need to see the values in self.allKeys or how it is populated to be sure.
From ABRecordRef Reference and CFTypeRef Reference
ABRecordCopyValue - Returns the value of a record property.
CFTypeRef ABRecordCopyValue (
ABRecordRef record,
ABPropertyID property
);
Parameters
record - The record containing the property in question.
property - The property of record whose value is being returned.
Return Value
The value of property in record.
And:
ABPropertyID - Integer that identifies a record property.
typedef int32_t ABPropertyID;
- And more troubleshooting ideas -
If the above is not the case, then your crash may be caused when you cast CFTypeRef to NSString * in (NSString*)ABRecordCopyValue(currentRecordRef, currentFieldProperty) so here is a little helper function that might solve that:
- (NSString*)stringValueForCFType:(CFTypeRef)cfValue {
NSString *stringValue = nil;
if (!cfValue) return nil;
CFTypeID cfType = CFGetTypeID(cfValue);
if (cfType == CFStringGetTypeID()) {
stringValue = [[(id)CFMakeCollectable(cfValue) retain] autorelease];
} else if (cfType == CFURLGetTypeID()) {
stringValue = [(NSURL*)cfValue absoluteString];
} else if (cfType == CFNumberGetTypeID()) {
stringValue = [(NSNumber*)cfValue stringValue];
} else if (cfType == CFNullGetTypeID()) {
stringValue = [NSString string];
} else if (cfType == AXUIElementGetTypeID()) {
stringValue = [[GTMAXUIElement elementWithElement:cfValue] description];
} else if (cfType == AXValueGetTypeID()) {
stringValue = [self stringValueForAXValue:cfValue];
} else if (cfType == CFArrayGetTypeID()) {
stringValue = [self stringValueForCFArray:cfValue];
} else if (cfType == CFBooleanGetTypeID()) {
stringValue = CFBooleanGetValue(cfValue) ? #"YES" : #"NO";
} else {
CFStringRef description = CFCopyDescription(cfValue);
stringValue = [(id)CFMakeCollectable(description) autorelease];
}
return stringValue;
}
Then do currentRecord = [self stringValueForCFType:ABRecordCopyValue(currentRecordRef, currentFieldProperty)]; and check to make sure self.allKeys has an object at index j and self.allContacts has an object at index i:
NSString *currentRecord = [[NSString alloc] init];
ABRecordRef currentRecordRef;
ABPropertyID currentFieldProperty;
if (self.allContacts.count > i) {
currentRecordRef = (ABRecordRef)[self.allContacts objectAtIndex:i];
if (self.allKeys.count > j) {
currentFieldProperty = (ABPropertyID)[self.allKeys objectAtIndex:j];
currentRecord = [self stringValueForCFType:ABRecordCopyValue(currentRecordRef, currentFieldProperty)];
} else {
NSLog(#"self.allKeys has no value at index (%d): %#", j, [allKeys description]);
}
} else {
NSLog(#"self.allContacts has no value at index (%d): %#", i, [allContacts description]);
}
Edit (regarding the comments):
To convert string of property name to its int value, you need to create the following (this probably is not be the correct order that they need to be in, so NSLog them first to see what order they need to be in):
NSString * const ABPropertyID_toString[] = {
#"kABPersonFirstNameProperty",
#"kABPersonLastNameProperty",
#"kABPersonMiddleNameProperty",
#"kABPersonPrefixProperty",
#"kABPersonSuffixProperty",
#"kABPersonNicknameProperty",
#"kABPersonFirstNamePhoneticProperty",
#"kABPersonLastNamePhoneticProperty",
#"kABPersonMiddleNamePhoneticProperty",
#"kABPersonOrganizationProperty",
#"kABPersonJobTitleProperty",
#"kABPersonDepartmentProperty",
#"kABPersonEmailProperty",
#"kABPersonBirthdayProperty",
#"kABPersonNoteProperty",
#"kABPersonCreationDateProperty",
#"kABPersonModificationDateProperty",
#"kABPersonAddressProperty",
// ... etc
};
- (NSString *)ABPropertyIDToString:(ABPropertyID)propertyVal {
return ABPropertyID_toString[propertyVal];
}
- (ABPropertyID)stringToABPropertyID:(NSString *)propertyString {
int retVal;
for(int i=0; i < sizeof(ABPropertyID_toString) - 1; ++i) {
if([(NSString *)ABPropertyID_toString[i] isEqual:propertyString]) {
retVal = i;
break;
}
}
return retVal;
}
Then pass stringToABPropertyID: the value from the array [self.allKeys objectAtIndex:j] and you will be returned an ABPropertyID:
currentFieldProperty = [self stringToABPropertyID:[self.allKeys objectAtIndex:j]];
I'm posting my earlier comment as an answer:
I would especially give thought to the value of currentFieldProperty - because it is taken out of a dictionary, it is an object but the function is supposed to receive an enumerated value. Maybe try
currentFieldProperty = (ABPropertyID)[[self.allKeys objectAtIndex:j] intValue];
Several problems:
ABPropertyID is not an object type. (ABPropertyID)[self.allKeys objectAtIndex:j] is clearly wrong. I'm not sure how it's even possible to be "an array of all properties (each property as string)". What string are you talking about? Where did you get this string from? If you are talking about the string of the name of the property e.g. #"kABPersonEmailProperty" then it would be pretty much impossible to get the value of it from that.
ABPropertyID values like kABPersonEmailProperty are not constants. They are variables, and they only seem to be initialized after you initialize an address book for the first time.