I would like to know how to compare a string with an array, i.e., if my array list has {"abc", "pqr", "xyz"} and the new string lets say "mno" is typed, it should compare with my previous array list. How can I do this? Thanks in advance.
Look at the NSArray documentation...
BOOL hasString = [your_array containsObject:your_string];
System:
if ([yourArray containsObject:yourNSString])
{
NSLog(#"Bingo!");
}
Manual:
for (int i = 0 ; i < [yourArray count] ; i++) {
if ([yourNSString isEqualToString:[yourArray objectAtIndex:i]]) {
NSLog(#"Bingo!");
break;
}
}
for(int i=0; i<[myarray length]; ++i) {
if([myarray[i] isEqualToString:#"mno"])
NSLog("Equal");
else NSLog("Not Equal");
}
Here is a working (tested) method,
-(BOOL)checkStingInArray: (NSString *)aString arrayWithStrings:(NSMutableArray *)array
{
if ( [array containsObject: aString] ) {
NSLog(#" %# found in Array",aString );
return YES;
} else {
NSLog(#" %# not found in Array",aString );
return NO;
}
}
Related
I have an array that I am filling with boolean values in the following code.
for(int i = 0; i < 15; i++){
int checkVal = [(NSNumber *)[__diceValue objectAtIndex:i] intValue];
if(checkVal == matchVal){
[_diceMatch replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:y]];
}
}
Whats the shortest way to write a conditional to check the array "_diceMatch" for all true values?
If your array can only contain the values "true" (#YES) or "false" (#NO)
then you can simply check for the absence of #NO:
if (![_diceMatch containsObject:#NO]) {
// all elements are "true"
}
NSUInteger numberOfTrueValues = 0;
for (NSNumber *value in _diceMatch) {
if ([value boolValue]) {
numberOfTrueValues++;
}
}
Shortest way? maybe not. Easiest way? Yes
- (BOOL)isDictMatchAllTrue {
for (NSNumber *n in _dictMatch) {
if (![n boolValue]) return NO;
}
return YES;
}
or you don't like writing loop
NSSet *set = [NSSet setWithArray:_diceMatch];
return set.count == 1 && [[set anyObject] boolValue];
Note: first version return YES when array is empty but second version return NO.
You can add
if (_dictMatch.count == 0) return YES; //or NO
to fix it.
Can't understand that the object is nil:
if ([self indexFromObjectProperty:UUID])
{}
else
{}
The problem is that indexFromObjectProperty can be 0 but I need to check the situation when there is no such element in array.
-(NSInteger)indexFromObjectProperty:(NSString *)property
{
NSInteger iIndex;
for (int i = 0; i < [items count]; i++)
{
if([property isEqualToString:[[items objectAtIndex:i] objectForKey:#"UUID"]])
{
iIndex = i;
}
}
return iIndex;
}
How can I solve this?
To your Q: You can initialize iIndex with NSNotFound. Then you compare to it.
Additionally: NSPredicate (and some other suggestions leading too far away from the Q.)
When you create the iIndex, set the value to some default:
NSInteger iIndex = NSNotFound;
Then you can check it in your if:
if ([self indexFromObjectProperty:UUID] != NSNotFound)
i have written the following code to check anagram want to know is this perfect & is there any better way to implement the same in objective C
-(BOOL) findAnagram :(NSString *) string1 :(NSString *) string2
{
int len = string1.length;
if (len != string2.length)
{
return false;
}
for (int i=0; i < len; i++)
{
int h = 0;
int q = 0;
for (int k = 0; k < len ; k ++)
{
if ([string1 characterAtIndex:i] == [string1 characterAtIndex:k])
{
h++;
}
if ([string1 characterAtIndex:i] == [string2 characterAtIndex:k])
{
q++;
}
}
if (h!=q)
{
return false;
}
}
return TRUE;
}
A better performing version than yours, which is a O(n ^ 2) algorithm, is a O(n) algorithm:
BOOL anagrams(NSString *a, NSString *b)
{
if (a.length != b.length)
return NO;
NSCountedSet *aSet = [[NSCountedSet alloc] init];
NSCountedSet *bSet = [[NSCountedSet alloc] init];
for (int i = 0; i < a.length; i++)
{
[aSet addObject:#([a characterAtIndex:i])];
[bSet addObject:#([b characterAtIndex:i])];
}
return [aSet isEqual:bSet];
}
You want to know if two strings contain exactly the same characters? Easiest way would probably be to sort both of them and compare the sorted version.
Another way would be to count the number of appearances of each letter (how many As, how many Bs, and so forth), then compare those counts.
(Note: The second way is just a variation of the first one, it's one efficient way to sort a string)
It looks fine to me. But the code style is slightly odd. I would write it like this:
- (BOOL)isStringAnagram:(NSString *)string1 ofString:(NSString *)string2 {
int len = string1.length;
if (len != string2.length) {
return NO;
}
for (int i=0; i < len; i++) {
int h = 0;
int q = 0;
for (int k = 0; k < len; k++) {
if ([string1 characterAtIndex:i] == [string1 characterAtIndex:k]) {
h++;
}
if ([string1 characterAtIndex:i] == [string2 characterAtIndex:k]) {
q++;
}
}
if (h != q) {
return NO;
}
}
return YES;
}
The main issue I have is with the method name. While it's possible to have parameters that have nothing before them in the name, it is not advisable. i.e. you had findAnagram:: as the name whereas I've used isStringAnagram:ofString:.
This is an implementation on #zmbq suggestion of sorting and comparing.
You should consider the requirements of deleting spaces and being case insensitive.
- (BOOL)isAnagram:(NSString *)leftString and:(NSString *)rightString {
NSString *trimmedLeft = [[leftString stringByReplacingOccurrencesOfString:#" " withString:#""] lowercaseString];
NSString *trimmedRight = [[rightString stringByReplacingOccurrencesOfString:#" " withString:#""] lowercaseString];
return [[self stringToCharArraySorted:trimmedLeft] isEqual:[self stringToCharArraySorted:trimmedRight]];
}
- (NSArray *)stringToCharArraySorted:(NSString *)string {
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0 ; i < string.length ; i++) {
[array addObject:#([string characterAtIndex:i])];
}
return [[array sortedArrayUsingSelector:#selector(compare:)] copy];
}
called like this
BOOL isAnagram = [self isAnagram:#"A BC" and:#"cba"];
Check the following method which check Anagram strings.
-(BOOL)checkAnagramString:(NSString*)string1 WithAnotherString:(NSString*)string2{
NSCountedSet *countSet1=[[NSCountedSet alloc]init];
NSCountedSet *countSet2=[[NSCountedSet alloc]init];
if (string1.length!=string2.length) {
NSLog(#"NOT ANAGRAM String");
return NO;
}
for (int i=0; i<string1.length; i++) {
[countSet1 addObject:#([string1 characterAtIndex:i])];
[countSet2 addObject:#([string2 characterAtIndex:i])];
}
if ([countSet1 isEqual:countSet2]) {
NSLog(#"ANAGRAM String");
return YES;
} else {
NSLog(#"NOT ANAGRAM String");
return NO;
}
}
Another run of the mill algorithm:
- (BOOL) testForAnagramWithStrings:(NSString *)stringA andStringB: (NSString *)stringB{
stringA = [stringA lowercaseString];
stringB = [stringB lowercaseString];
int counter = 0;
for (int i=0; i< stringA.length; i++){
for (int j=0; j<stringB.length;j++){
if ([stringA characterAtIndex:i]==[stringB characterAtIndex:j]){
counter++;
}
}
}
if (counter!= stringA.length){
return false;
}
return true;
}
how to get a single element from an array by comparing with a string value.I have a string in a textfield.I want to compare that textfield string with an array.And i want to get that single element form that array.
If you have an NSArray of NSString's and you just want to see whether or not the text field string is in the array you can use:
NSString *textFieldString; // Contents of my text field
NSArray *myArray; // Array to search
BOOL stringMatches = [myArray containsObject:textFieldString];
If you instead want to know the index of the string in the array use:
NSUInteger index = [myArray indexOfObject:textFieldString];
If index == NSNotFound the array does not contain the text field string.
Use compare: method.
for (int i=0; i<[yourArray count]; i++) {
NSString * string = [yourArray objectAtIndex:i];
if ([textfield.text compare:string]) {
NSLog(#"yes");
break;
}
}
I think it will be helpful to you.
Use isEqualToString: method for campare two String.
for (int i=0; i<[array count]; i++) {
NSString * string = [array objectAtIndex:i];
i if (string isEqualToString:textField.text)
{
NSLog(#"Equal");
}
else
{
NSLog(#"Not Equal");
}
}
use this :
for (NSString * string in yourArray) {
if ([string isEqualToString:textField.text])
{
NSLog(#" They are equal");
}
else
{
NSLog(#" They are not");
}
}
I'm trying to store numbers in a NSMutableArray, but when I check it, all I get is garbage.
-(void)storeIntoArray:(int)indexInt
{
NSNumber *indexNum = [[NSNumber alloc] initWithInt:indexInt];
[last100 insertObject:indexNum atIndex:prevCount];
prevCount++;
if(prevCount > 100)
{
prevCount = 0;
cycledOnce = YES;
}
}
-(BOOL)checkArray:(int)indexInt
{
for(int i = 0;i < [last100 count];i++)
{
NSLog(#"Stored: %d",[last100 objectAtIndex:i]);
if (indexInt == (int)[last100 objectAtIndex:i]) {
NSLog(#"Same entry, trying again");
return YES;
}
}
return NO;
}
When I check, I'll get values back like Stored: 110577680 and just various garbage values.
You're printing the NSNumber object address, not its value. Try using intValue. Or, if you want to print the object directly, use %# rather than %d.
NSLog(#"Stored: %d",[last100 objectAtIndex:i]);
Should be:
NSLog(#"Stored: %#",[last100 objectAtIndex:i]);
NSNumber is an object not an integer.