NSPredicate through nested array using the dictionary key - ios

Could someone help me how to predicate through the below data and retrieve the value for the key "Text". Initially i have dictionary with the below data
{
ArrayName1 = (
{
Target = "<null>";
Text = "Name1";
Value = 1;
},
{
Target = "<null>";
Text = "Name2";
Value = 2;
}
);
ArrayName2 = (
{
Target = "<null>";
Text = "Name3";
Value = 3;
},
{
Target = "<null>";
Text = "Name4";
Value = 4;
}
);
ArrayName3 = (
{
Target = "<null>";
Text = "Name5";
Value = 5;
},
{
Target = "<null>";
Text = "Name6";
Value = 6;
}
);
ArrayName4 = (
{
Target = "<null>";
Text = "somename";
Value = somevalue;
}
);
ArrayName4 = (
{
Target = "<null>";
Text = "somename";
Value = "somevalue";
}
);
}
I want the end result to be stored in the "resultarray" which should contain value of "Text" key by the search string from all the array and also want to store the corresponding value for the "Value" key in the same array.
Thanks,
Pradeep

#Dev.RK #Cyph3r - Below are the methods i tried. #Dev.RK,#Cyph3r. I tried the below methods including compound predicate.
//Method 1
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY %K.%K CONTAINS[c] %#", #"Practice",#"Text",searchTextString];
searchArray = [[quickSearchDataDict allValues] filteredArrayUsingPredicate:predicate];
//Method 2
NSPredicate *predicateArray1 = [NSPredicate predicateWithFormat:#"(TEXT==%#)",searchTextString];
NSPredicate * predicateArray2 = [NSPredicate predicateWithFormat:#"SELF.%K.%K contains[c] %#",#"ArrayName2",#"Text",searchTextString];
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:#[predicateArray1,predicateArray2]];
searchArray = [[quickSearchDataDict allValues] filteredArrayUsingPredicate:predicate];

Try this, hope this will solved your problem-
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
for (int i=0; i<5; i++) {
NSMutableArray *ary=[[NSMutableArray alloc]init];
for (int j=0; j<2; j++) {
NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
[dic setValue:[NSString stringWithFormat:#"none"] forKey:#"Target"];
[dic setValue:[NSString stringWithFormat:#"tst%d",j] forKey:#"Text"];
[dic setValue:[NSString stringWithFormat:#"%d",j+i] forKey:#"Value"];
[ary addObject:dic];
}
[dict setObject:ary forKey:[NSString stringWithFormat:#"ArrayName%d",i]];
}
NSArray *searchary=[dict valueForKey:[NSString stringWithFormat:#"%#",#"ArrayName0"]];
NSPredicate *pre0 = [NSPredicate predicateWithFormat:#"SELF.%K contains[cd] %#",#"Text", #"tst"];
NSArray *resultArray= [searchary filteredArrayUsingPredicate:pre0];
NSLog(#"%#",resultArray);
here is the log-
1. Text==tst0
(
{
Target = none;
Text = tst0;
Value = 0;
}
)
2. Text==tst
(
{
Target = none;
Text = tst0;
Value = 0;
},
{
Target = none;
Text = tst1;
Value = 1;
}
)
and this is complete data-
{
ArrayName0 = (
{
Target = none;
Text = tst0;
Value = 0;
},
{
Target = none;
Text = tst1;
Value = 1;
}
);
ArrayName1 = (
{
Target = none;
Text = tst0;
Value = 1;
},
{
Target = none;
Text = tst1;
Value = 2;
}
);
ArrayName2 = (
{
Target = none;
Text = tst0;
Value = 2;
},
{
Target = none;
Text = tst1;
Value = 3;
}
);
ArrayName3 = (
{
Target = none;
Text = tst0;
Value = 3;
},
{
Target = none;
Text = tst1;
Value = 4;
}
);
ArrayName4 = (
{
Target = none;
Text = tst0;
Value = 4;
},
{
Target = none;
Text = tst1;
Value = 5;
}
);
}

Related

Searching in Dictionary of arrays in Objective C [duplicate]

i have a NSDictionary like:
{
"2017-05-02" = (
{
"always_valid" = 0;
date = "2017-05-02";
from = "12:00";
to = "13:00";
},
{
"always_valid" = 0;
date = "2017-05-02";
from = "12:00";
to = "12:00";
},
{
"always_valid" = 0;
date = "2017-05-02";
from = "14:00";
"hourly_rate" = 12;
to = "15:00";
}
);
"2017-05-03" = (
{
"always_valid" = 0;
date = "2017-05-03";
from = "12:00";
to = "13:00";
}
);
"2017-05-18" = (
{
"always_valid" = 1;
date = "2017-05-18";
from = "12:00";
to = "12:00";
}
);
}
i'm trying to apply
NSPredicate *filter = [NSPredicate predicateWithFormat:#"always_valid = \"1\""];
NSArray *alwaysvalid = [[dic allValues] filteredArrayUsingPredicate:filter];
it use to work when i had structure something like
array > dictionary
but now it's like
array > array > dictionary
by doing [dic allValues] for array.
any help what should i apply to keep it fast.
What you need to do is need enumerate your dictionary and create new filtered Dictionary.
NSMutableDictionary *filterDic = [[NSMutableDictionary alloc] init];
NSPredicate *filter = [NSPredicate predicateWithFormat:#"always_valid = 1"];
[dict enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSArray* obj, BOOL *stop) {
NSArray *filterArray = [obj filteredArrayUsingPredicate:filter];
if (filterArray.count > 0) {
filterDic[key] = filterArray;
}
}];
Try this :
NSArray *array = [NSArray arrayWithObject:dict]; // you can also do same for Name key...
NSArray *alwaysvalid = [array filteredArrayUsingPredicate:filter];

Sort Descriptor not working in ios

i used the Sort Descriptor to Sort the NSMutableArray By one & multiple Values,First i tried by to sort By Price,it sort in some other Order here is my code help me,
My
i create the Dictionary by below code and added to the NSMutableArray
for(int i=0;i<[priceArray count];i++)
{
cellDict=[[NSMutableDictionary alloc]init];
[cellDict setObject:nameArray[i] forKey:#"Name"];
[cellDict setObject:splPriceArray[i] forKey:#"Percentage"];
[cellDict setObject:priceArray[i] forKey:#"Price"];
[resultArray addObject:cellDict];
}
// To Sort in Ascending Order
NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:#"Price" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];
NSLog(#"Result %# Sorted arr %#",resultArray, sortedArray);
And Output is:
Result (
{
Name = "Black Eyed Peas";
Percentage = 0;
Price = 80;
},
{
Name = "Black Gram";
Percentage = 0;
Price = 56;
},
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Double Beans";
Percentage = 0;
Price = 95;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Green Moong Dal";
Percentage = 0;
Price = 150;
},
{
Name = "Ground Nut";
Percentage = 0;
Price = 140;
},
{
Name = "Moong Dal";
Percentage = 0;
Price = 75;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
},
{
Name = "Toor Dal";
Percentage = 0;
Price = 150;
}
) Sorted arr (
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
},
{
Name = "Ground Nut";
Percentage = 0;
Price = 140;
},
{
Name = "Green Moong Dal";
Percentage = 0;
Price = 150;
},
{
Name = "Toor Dal";
Percentage = 0;
Price = 150;
},
{
Name = "Black Gram";
Percentage = 0;
Price = 56;
},
{
Name = "Moong Dal";
Percentage = 0;
Price = 75;
},
{
Name = "Black Eyed Peas";
Percentage = 0;
Price = 80;
},
{
Name = "Double Beans";
Percentage = 0;
Price = 95;
}
)
Here The Sorted Array Sorting in some other Order I want to sort this in Ascending order by price.
It's unclear what your test data looks like - but the following snippet works as expected
NSArray *priceArray = [NSArray arrayWithObjects:#(74),#(100),#(100),#(130), nil];
NSArray *nameArray = [NSArray arrayWithObjects:#"Yva",#"Hallo", #"Adam", #"Xavier", nil];
NSMutableArray *resultArray = [NSMutableArray new];
for(int i=0;i<[priceArray count];i++)
{
NSMutableDictionary *cellDict=[[NSMutableDictionary alloc]init];
[cellDict setObject:nameArray[i] forKey:#"Name"];
[cellDict setObject:priceArray[i] forKey:#"Percentage"];
[cellDict setObject:priceArray[i] forKey:#"Price"];
[resultArray addObject:cellDict];
}
// Sort by Name
//NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:#"Price" ascending:YES];
// Sort by Name
NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:#"Name" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];
NSLog(#"Result %# Sorted arr %#",resultArray, sortedArray);
Result:
2015-07-11 12:54:54.358 ret[10480:162783] Result (
{
Name = Yva;
Percentage = 74;
Price = 74;
},
{
Name = Hallo;
Percentage = 100;
Price = 100;
},
{
Name = Adam;
Percentage = 100;
Price = 100;
},
{
Name = Xavier;
Percentage = 130;
Price = 130;
}
) Sorted arr (
{
Name = Adam;
Percentage = 100;
Price = 100;
},
{
Name = Hallo;
Percentage = 100;
Price = 100;
},
{
Name = Xavier;
Percentage = 130;
Price = 130;
},
{
Name = Yva;
Percentage = 74;
Price = 74;
}
)

Filter NSMutableArray based on dictionary key

I have array with multiple dictionarys. which contains different keys . here i have goalType each goal contains number of records i want to filter the records based on goalType . please check the format below.
Array==>( {
goalType = 1;
languagetype = 1;
soundid = 19;
status = 1;
},
{
goalType = 1;
languagetype = 1;
soundid = 20;
status = 1;
},
{
goalType = 2;
languagetype = 1;
soundid = 21;
status = 1;
},
{
goalType = 2;
languagetype = 1;
soundid = 22;
status = 1;
},
{
goalType = 2;
languagetype = 1;
soundid = 23;
status = 1;
},
)
i have wrote the below code but filtered array getting empty
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"goalType == %# && languagetype == %d && status == 1 && soundid <= 3884",
[NSNumber numberWithInt:goalType.integerValue],languageid];
[dataSource predicate];
why not use
NSArray's filteredArrayUsingPredicate:
Evaluates a given predicate against each object in the receiving array and returns a new array containing the objects for which the predicate returns true.
I don't know of a
[NSArray predicate]
selector.I'm surprised that your not getting NoMethodFound exception.
NSDictionary *theDict = #{#"goalType":#(1),#"languagetype":#(1),#"soundid":#(19),#"status":#(1)};
NSDictionary *theDict1 = #{#"goalType":#(1),#"languagetype":#(1),#"soundid":#(20),#"status":#(1)};
NSDictionary *theDict2 = #{#"goalType":#(2),#"languagetype":#(1),#"soundid":#(21),#"status":#(1)};
NSDictionary *theDict3 = #{#"goalType":#(2),#"languagetype":#(1),#"soundid":#(23),#"status":#(1)};
NSMutableArray *testArray = [NSMutableArray array];
[testArray addObject:theDict];
[testArray addObject:theDict1];
[testArray addObject:theDict2];
[testArray addObject:theDict3];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"goalType == %# && languagetype == %# && status = 1 && soundid <= 3884", [NSNumber numberWithInt:1],[NSNumber numberWithInt:1]];
NSArray *filteredArray = [testArray filteredArrayUsingPredicate:predicate];
NSLog(#"%#",filteredArray);
This is the code works.

Merge the same objects inside array IOS

I have an array which contains this objects:
(
{
aOptns = (
);
fCustomDscnt = 0;
fPrcntDscnt = 0;
fPrice = 0;
fQty = 1;
iItemDayPriceId = 143;
iShiftId = 1;
sItemName = "";
sModifier = "";
},
{
aOptns = (
);
fCustomDscnt = 0;
fPrcntDscnt = 0;
fPrice = 0;
fQty = 1;
iItemDayPriceId = 143;
iShiftId = 1;
sItemName = "";
sModifier = "";
},
{
aOptns = (
);
fCustomDscnt = 0;
fPrcntDscnt = 0;
fPrice = 0;
fQty = 1;
iItemDayPriceId = 143;
iShiftId = 1;
sItemName = "";
sModifier = "";
},
{
aOptns = (
);
fCustomDscnt = 0;
fPrcntDscnt = 0;
fPrice = 0;
fQty = 1;
iItemDayPriceId = 112;
iShiftId = 1;
sItemName = "";
sModifier = "";
}
)
I need to merge the contents of array if the objects are same and modify the object inside that array in such a way that it should be like this:
(
{
aOptns = (
);
fCustomDscnt = 0;
fPrcntDscnt = 0;
fPrice = 0;
fQty = 3;
iItemDayPriceId = 143;
iShiftId = 1;
sItemName = "";
sModifier = "";
},
{
aOptns = (
);
fCustomDscnt = 0;
fPrcntDscnt = 0;
fPrice = 0;
fQty = 1;
iItemDayPriceId = 112;
iShiftId = 1;
sItemName = "";
sModifier = "";
}
)
As you can see, the entry for object with iItemDayPriceId = 143 becomes 1 only with fQty = 3.
I have tried using the code here: How to Find Duplicate Values in Arrays?
But it is only comparing 2 objects at a time.
Edit: Oops, I missed your count requirement.
Edit 2: And the fact that you wanted to mutate the given array rather the build a new one.
Depends on what you mean by distinct but since it looks like you mean to compare the contents and not the references, you should build a new array so that if the comparison is true you increment the count and otherwise you add the object.
Mutate existing array
Disclaimer: probably not my best work here.
I would appreciate if someone could suggest how to improve this because I am pretty sure it is not the best way to do this and may not even work (don't have access to an Obj-C compiler at the moment).
Foo objectA = [Foo new];
objectA.Bar = #"Bar";
Foo objectB = [Foo new];
objectB.Bar = #"Bar";
Foo objectC = [Foo new];
objectC.Bar = #"Not Bar";
NSMutableArray *array = #[objectA, objectB, objectC];
NSMutableDictionary *countDictionary = [[NSMutableDictionary alloc] init];
for (Foo *foo in [array copy])
{
bool found = false;
for (Foo *key in countDictionary)
{
if ([key isEqualTo:foo])
{
NSNumber *currentCount = [countDictionary objectForKey:key];
int currentIntCount = [currentCount intValue];
currentIntCount++;
[countDictionary setObject:[NSNumber numberWithInt:currentIntCount] forKey:key];
[array removeObject:key];
found = true;
break;
}
if (!found)
[countDictionary setObject:[NSNumber numberWithInt:1] forKey:foo];
}
}
for (Foo *realFoo in array)
{
NSNumber *countNumber = [countDictionary objectForKey:realFoo];
int countInt = [countNumber intValue];
realFoo.Count = countInt;
}
Build new array
Foo objectA = [Foo new];
objectA.Bar = #"Bar";
Foo objectB = [Foo new];
objectB.Bar = #"Bar";
NSMutableArray *array = #[objectA, objectB];
NSMutableArray *distinct = [[NSMutableArray alloc] init];
for (Foo *f in array)
{
bool found = false;
for (Foo *d in distinct)
{
if ([d isEqualTo:f])
{
found = true;
d.Count++;
}
break;
}
if (!found)
[distinct addObject:f];
}
And of course you would have to define an equality comparer for Foos, in this case for instance
-(bool) isEqualTo:(Foo *)nFoo
{
bool isEqual = false;
if ([self.Bar isEqualToString:nFoo.Bar])
isEqual = true;
return isEqual;
}
Having not dealt with Objective C in a while I should say that there may be some best-practices as to implementing equality comparison instead of rolling your own completely like this example
Use below code:
NSMutableArray* myArray =
[[NSMutableArray alloc]initWithObjects:
#"red",#"blue",#"red",#"green",#"yellow", #"33", #"33",#"red", #"123", #"123",nil];
NSOrderedSet *mySet = [[NSOrderedSet alloc] initWithArray:myArray];
myArray = [[NSMutableArray alloc] initWithArray:[mySet array]];
NSLog(#"%#",myArray);

how to find location of Id in NSMutableArray

I have NSMutableArray data as below.
(
{
Id = "-1";
NameEn = Country;
},
{
Id = 14;
NameEn = Iran;
},
{
Id = 11;
NameEn = Jordan;
},
{
Id = 5;
NameEn = "United Arab Emirates";
},
{
Id = 4;
NameEn = "Suadi Arabia";
},
{
Id = 3;
NameEn = Kuwait;
},
{
Id = 10;
NameEn = Yemen;
},
{
Id = 6;
NameEn = Oman;
},
{
Id = 12;
NameEn = Syria;
},
{
Id = 7;
NameEn = Qatar;
},
{
Id = 13;
NameEn = Lebanon;
},
{
Id = 1;
NameEn = Egypt;
},
{
Id = 8;
NameEn = "Bahrain Kingdom";
}
)
I want to find the location where Id=5.
Any idea how can I do?
I tried with below.
NSString *myCountry = [[NSUserDefaults standardUserDefaults] valueForKey:#"mCountryId"];
NSUInteger indexOfTheObject = [feedsCountry indexOfObject: myCountry];
NSLog(#"indexOfTheObject===%i==%#", indexOfTheObject, myCountry);
if (NSNotFound == indexOfTheObject) {
NSLog(#"not found...");
}
But I get output as not found... for mCountryId as 5.
Use an NSDictionary instead, and key your entries by Id; e.g.:
NSMutableDictionary* dictionary = [NSMutableDictionary new];
[dictionary setObject:#"Egypt" forKey:#"1"];
// (etc...)
EDIT: If you already have an array and can not change that, use a for loop like this:
for(NSDictionary* entry in givenArray){
NSString* key = [entry objectForKey:#"Id"];
NSString* value = [entry objectForKey:#"NameEn"];
[dictionary setObject:value forKey:key];
}
NSPredicate * filter = [NSPredicate predicateWithFormat:#"Id = 5"];
NSArray * filtered = [array filteredArrayUsingPredicate:filter];
The first object in filtered is the dictionary with Id = 5

Resources