NSNumbers stored in NSMutableArray - objective-c

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.

Related

checking whole boolean of NSMutableArray

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.

Searching for a string within an array for an NSRange

I am trying to search for a string within an array, but I only want to search the last five objects in the array for the string.
I have been fiddling with every parameter I can find on NSRange to no avail.
I would post some example code, but I can't even get out the line I need, whether its through introspection, enumeration, or just some NSRange call that I missed.
If your array elements are strings that you searched for, you can directly check the array as follows:
if ([yourArray containsObject:yourString])
{
int index = [yourArray indexOfObject:yourString];
if (index>= yourArray.count-5)
{
// Your string matched
}
}
I like indexesOfObjectsWithOptions:passingTest: for this. Example:
NSArray *array = #[#24, #32, #126, #1, #98, #16, #67, #42, #44];
// run test block on each element of the array, starting at the end of the array
NSIndexSet *hits = [array indexesOfObjectsWithOptions:NSEnumerationReverse passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
// if we're past the elements we're interested in
// we can set the `stop` pointer to YES to break out of
// the enumeration
if (idx < [array count] - 5) {
*stop = YES;
return NO;
}
// do our test -- if the element matches, return YES
if (40 > [obj intValue]) {
return YES;
}
return NO;
}];
// indexes of matching elements are in `hits`
NSLog(#"%#", hits);
Try this :-
//Take only last 5 objects
NSRange range = NSMakeRange([mutableArray1 count] - 5, 5);
NSMutableArray *mutableArray2 = [NSMutableArray arrayWithArray:
[mutableArray1 subarrayWithRange:range]];
//Now apply search logic on your mutableArray2
for (int i=0;i<[mutableArray2 count];i++)
{
if ([[mutableArray2 objectAtIndex:i] isEqualToString:matchString])
{
//String matched
}
}
Hope this helps you!

how to get a single element from an array by comparing with a string value in xcode?

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");
}
}

Duplicate element elimination from NSMutableArray

I have an NSMutableArray. I have synthesized it as well. I am adding objects to it from a method by xml extraction of some file names etc. Now in my array there is the occurrence of same elements multiple times. I need to eliminate the multiple objects and get only unique ones. How can this be done? I tried the code below but then there arise some errors.
NSString *AName = [CompleteFileName_string1 substringWithRange:ArtistRange];
[array_artist addObject:AName];
for(int i=0; i < [array_artist count]; i++)
{
if([[array_artist objectAtIndex:i] isEqualToString:[array_artist objectAtIndex:i+1]])
{
[[array_artist objectAtIndex:i+1]=NULL];
}
else
{
}
}
EDIT*** i need to eliminate by ignoring case sensitivity too...
Go With NSSet
Take a tempArray as golbally
When you are adding objects from XML you have to just check
NSString *upCaseString=[stringFromXML uppercaseString];
if(![tempArray containsObject:upCaseString]) {
[array_artist addObject:stringFromXML];
[tempArray addObject:upCaseString];
}
if([[array_artist objectAtIndex:i] &&[array_artist objectAtIndex:i+1] caseInsensitiveCompare:#"True"] == NSOrderedSame])
{
Add element
}
Just simply check if the array contains the object:
if(![yourArray containsObject:stringFromXML])
{
[yourArray addObject:stringFromXML]
}
What about:
NSArray *out = [NSSet setWithArray:in].allObjects;
?
Use Following Code..
First Add All values in array_artist
int i1=0;
tempArray = [NSMutableArray alloc] init];
while (i1 < [array_artist count]) {
if ([tempArray containsObject:[array_artist objectAtIndex:i1]) {
//Do not Add
}
else {
//Add object
[tempArray addObject:array_artist:i1];
}
i1++;
}

Compare strings in array with another string

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;
}
}