Can't compare the values in if method - objective-c

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)

Related

Merge Sorting in Objective C

I am trying to implement merge sort in objective -C.
This is a similar question asked in the following link , did not find it answered so creating a new question.
Merge sort in Objective-C
This is what I have tried ,
-(NSArray *)mergeSort:(NSArray *)unsortedArray {
if ([unsortedArray count] < 2)
return unsortedArray;
long mid = [unsortedArray count] / 2;
NSRange left = NSMakeRange(0, mid);
NSRange right = NSMakeRange(mid, [unsortedArray count] - mid);
NSArray *rightArray = [unsortedArray subarrayWithRange:right];
NSArray *leftArray = [unsortedArray subarrayWithRange:left];
NSArray *resultArray = [self merge:leftArray andRight:rightArray];
return resultArray;
}
-(NSArray *)merge:(NSArray *)leftArray andRight:(NSArray *)rightArray {
NSMutableArray *result = [NSMutableArray array];
int right = 0;
int left = 0;
while (left < [leftArray count] && right < [rightArray count]) {
NSComparisonResult comparisonResult = [leftArray[left] compare:rightArray[right]];
if (comparisonResult != NSOrderedDescending) {
[result addObject:[leftArray objectAtIndex:left++]];
} else {
[result addObject:[rightArray objectAtIndex:right++]];
}
/*if ([[leftArray objectAtIndex:left] intValue] < [[rightArray objectAtIndex:right] intValue]) {
[result addObject:[leftArray objectAtIndex:left++]];
//left++;
} else {
[result addObject:[rightArray objectAtIndex:right++]];
//right++;
}*/
}
NSRange leftRange = NSMakeRange(left, [leftArray count] - left);
NSRange rightRange = NSMakeRange(right, [rightArray count] - right);
NSArray * newRight = [rightArray subarrayWithRange:rightRange];
NSArray * newLeft = [leftArray subarrayWithRange:leftRange];
newLeft = [result arrayByAddingObjectsFromArray:newLeft];
return [newLeft arrayByAddingObjectsFromArray:newRight];
}
Kindly let me know if anyone has any other approaches for merge sort.
I dont understand why do you people want the long way.. Even though there are already easy way of doing this...
I made one myself hope this will help you..
- (NSArray *)arrayMergeSort:(NSArray *)targetArray
{
if (targetArray.count < 2)
return targetArray;
long midIndex = targetArray.count/2;
NSArray *arrayLeft = [targetArray subarrayWithRange:NSMakeRange(0, midIndex)];
NSArray *arrayRight= [targetArray subarrayWithRange:NSMakeRange(midIndex, targetArray.count - midIndex)];
return [self arrayMerge: [self arrayMergeSort:arrayLeft] : [self arrayMergeSort:arrayRight]];
}
For arrange merge:
- (NSArray *)arrayMerge:(NSArray *)arrayLeft :(NSArray *)arrayRight
{
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
int i = 0, j = 0;
while (i < arrayLeft.count && j < arrayRight.count)
[resultArray addObject:([arrayLeft[i] intValue] < [arrayRight[j] intValue]) ? arrayLeft[i++] : arrayRight[j++]];
while (i < arrayLeft.count)
[resultArray addObject:arrayLeft[i++]];
while (j < arrayRight.count)
[resultArray addObject:arrayRight[j++]];
return resultArray;
}
And using it like:
//Sample array
NSArray *activeArray = #[#101,#201,#301,#121,#11,#123,#21,#14,#32,#76,#89,#987,#65];
NSLog(#"arrayMergeSort %#",[self arrayMergeSort:activeArray]);
Output would be:
And also this bubble sort if you needed this:
- (NSArray *)arrayBubbleSort:(NSArray *)targetArray
{
NSMutableArray *resultArray = [targetArray mutableCopy];
for (int k = 0; k < resultArray.count; k++)
{
for (int l = 0; l < resultArray.count; l++)
{
if ([resultArray[k] intValue] < [resultArray[l] intValue])
{
[resultArray exchangeObjectAtIndex:k withObjectAtIndex:l];
}
}
}
return resultArray;
}
Hope i've helped you.. Cheers..
You've made a simple mistake. Merge sort works my splitting the array, sorting to the two halves, then merging the results.
Your mergeSort: method does the split, doesn't sort the two halves, and then calls merge: to merge the two (unfortunately unsorted) halves.
Before calling merge: you need to make recursive calls to mergeSort: to sort the two halves - this is the simple step you missed out.
I'm guessing this in a learning exercise, so no code, but you're almost there (fix it and it does work).
BTW Once you've fixed it you might want to think about why you don't need to create new arrays for the split part (but its far easier to create a new array for the merges).
HTH

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.

Objective-C loop through array

I have code similar to this
if (count == 0)
{
[array1 addObject:#"No Items"];
}
else
{
int x;
for (x = 0;x <= count; x++)
{
[array1 addObject:[itemsArray objectAtIndex:x];
NSLog(#"%#",array1);
}
}
itemsArray has numbers in it (0-40). My expected result is:
1
2
3
...
However it actually does:
1
1,2
1,2,3
1,2,3,4
1,2,3,4,5
...
Why does this happen? If possible, I'd also like to ask for an example to use fast enumeration for this situation (if it suits for this).
Thanks in advance.
You are NSLoging the whole array, not the current index of array1. What you are seeing logged is what you've coded - to log what you are expecting, change NSLog(#"%#",array1); to NSLog(#"%#",[array1 objectAtIndex:x]);
To confirm add the following after your assignment loop:
for (NSObject* o in array1)
{
NSLog(#"%#",o);
}
Use NSLog(#"%#", [array1 objectATIndex:x]);
if (count == 0)
{
[array1 addObject:#"No Items"];
}
else
{
int x;
for (x = 0;x <= count; x++)
{
[array1 addObject:[itemsArray objectAtIndex:x];
NSLog(#"%#", [array1 objectATIndex:x]);
}
}

How to search an array cyclically?

This is more a curiosity question than a pressing one. This question is looking for a better way to do the following, meaning without using two for loops.
I have an NSArray *array of NSStrings and a method -(BOOL)isGoodString:(NSString *)string. I want to jump into the array at a random spot and find the first good string, wrapping around the end if necessary. However, it may be that there is no good string, so I need to know that as well. Here's the current implementation:
-(NSString *)randomGoodString {
int N = [array count]
int start = arc4random() % N;
for (int j=start; j<N ; ++j) {
if isGoodString([array objectAtIndex:j]) {
return [array objectAtIndex:j];
}
}
for (j=0; j<start ; ++j) {
if isGoodString([array objectAtIndex:j]) {
return [array objectAtIndex:j];
}
}
return #"";
}
Any suggestions? Efficiency would be nice, but since this really is more for curiosity, anything that works in finite time would be nice to hear about.
Eliminate your second search loop by using a modulus:
-(NSString *)randomGoodString {
int N = [array count]
int start = arc4random() % N;
for (int j=0; j<N ; ++j) {
index = (j+start)%N;
if isGoodString([array objectAtIndex:index]) {
return [array objectAtIndex:index];
}
}
return #"";
}

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