This is my JSON :
collections = {
items = (
{
nom = "automne-hiver-2012-2013";
},
{
nom = carbon;
},
{
nom = colors;
});
};
}
I access to my json like this:
int j=0;
NSString *collections=self.dictionaryMenu[#"collections"][#"items"][j][#"nom"];
I'm looking for count all the key "nom" who are in my JSON.
I try :
for (objectKey in self.dictionaryMenu.allKeys) {
count2 += [[self.dictionaryMenu objectForKey:objectKey] count];
NSLog(#"Anzahl: %d", count2);
}
NSString *counter = [NSString stringWithFormat:#"COUNT MENU (%d)", count2];
NSLog(#"%#",counter);
Unfortunately, it sends me 1 ( i think that's because there is only 1 "collections" in the JSON).
How can i count all the key "nom" ?
Try this:
NSArray *items = self.dictionaryMenu[#"collections"][#"items"];
NSUInteger count = 0;
for (NSDictionary *item in items) {
if (item[#"nom"]) {
count++;
}
}
Related
Here is dictionary which in inside an array,
array With Dict : (
{
id = 1;
name = Arjun;
},
{
id = 2;
name = Karan;
},
{
id = 3;
name = Ajit;
},
{
id = 4;
name = Prashant;
},
{
id = 5;
name = Sushant;
}
)
When I will select any 'value', I want to fetch the 'key' associated with that value.
for example :
Suppose I selected 'Prashant' and I want its 'id' i.e 4.
How to get 'key' from 'value'?
NSString *myName = #"Prashant";
for (NSDictionary *dict in array) {
if ([dict[#"name"] isEqualToString:myName]) {
NSLog(#"%#", dict[#"id"]);
break;
}
}
for (NSDictionary*dic in array)
{
NSString * name = dic[#"name"];
if([name isEqual:currentSelectedName])
{
NSString * id = dic[#"id"];
NSLog(#"id is : %#",id);
}
}
You can loop thru the Array and find the matching entry - value you are looking for. Then get its key.
This Question has been asked many times, I've gone through each an every solution, also tried to implement but none of them worked for me.
Problem : Getting this error : Can't do a substring operation with something that isn't a string while filtering NSArray using NSPredicate.
Coding Stuff : By this way, I'm trying to filter my array
NSArray *array = [[NSArray alloc]initWithArray:dataArray];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"name BEGINSWITH[c] %#", searchText];
allProductData = [[array filteredArrayUsingPredicate:resultPredicate]mutableCopy]; //Error is here
Now there are two array's`s where this is working for 1st array and same code is not working for 2nd array.
Array 1: Working fine for this array.
(
{
"_id" = 59483833710f853221c90a92;
attributes = (
{
key = Atributos;
value = "-";
}
);
name = "Ades Soya Bebida Chocolate 946 Ml";
price = "-";
uri = "http://res.cloudinary.com/cloudimp/image/upload/u3kgmm8bx5lsmmacbnrl.jpg";
},
{
"_id" = 5948383f853221c90a94;
attributes = (
);
name = "Ades Soya Bebida Chocolate 200 Ml";
price = "";
uri = "http://res.cloudinary.com/cloudimp/image/upload/hhafrnwocqoamhhievoy.jpg";
}
)
Filter Key: name = "Ades Soya Bebida Chocolate 946 Ml";
Array 2 : Filter is not working for this array.
(
{
image = "http://images.qliktag.com/image/upload/w3tb9dutynou1hlooexn.gif";
name = Abuelita;
products = (
{
"_id" = 594c030f124244325;
attributes = (
);
name = "Abuelita chocolate granulado";
price = "";
uri = "http://images.qliktag.com/image/upload/iiqfuiilstvksdycnfbc.jpg";
},
{
"_id" = 5931c73303f737086;
attributes = (
);
name = "ABUELITA Chocolate";
price = "";
uri = "<null>";
},
{ //Crashing app for this while reading this structure.
"_id" = 5931c73303f737086;
attributes = (
);
name = (
{
language = #"en"
value = "SOYA"
},
{
language = #"de"
value = "SOYA Bebida"
}
);
price = "";
uri = "<null>";
}
);
}
Note: In both the array name value is different.
Finally I want to filter name
name1 = "Ades Soya Bebida Chocolate 946 Ml"
name2 = Abuelita; //Crashing app for this while filtering.
Array is changing dynamically, it looks similar to this...
example-1 : App is crashing, with this error : Can't do a substring operation with something that isn't a string.
ContactName[0] "ABC"
ContactName[1] = XYZ
ContactName[2] = (
{
language = #"en"
value = "PQR"
},
{
language = #"de"
value = "QWERTY"
}
)
example-2 : App is working fine
ContactName[0] = "ABC"
ContactName[1] = "XYZ"
ContactName[2] = "PQR"
So, how should I filter such array...???
I would suggest -[NSPredicate predicateWithBlock:].
Test the type of product[#"name"]. If the type is a NSString use -[NSString hasPrefix:]. If the type is NSArray find the language that equals NSLocale.currentLocale.languageCode, then compare then use -[NSString hasPrefix:] on value.
- (void)testBeginsWithSearchText {
NSArray<NSDictionary *> *data =
#[#{#"image": #"http://images.qliktag.com/image/upload/w3tb9dutynou1hlooexn.gif",
#"name": #"Abuelita",
#"products": #[#{#"_id": #"594c030f124244325",
#"attributes": #[],
#"name": #"Abuelita chocolate granulado",
#"price": #"",
#"uri": #"http://images.qliktag.com/image/upload/iiqfuiilstvksdycnfbc.jpg"},
#{#"_id": #"5931c73303f737086",
#"attributes": #[],
#"name": #"ABUELITA Chocolate",
#"price": #"",
#"uri": #"<null>"},
#{#"_id": #"5931c73303f737086",
#"attributes": #[],
#"name": #[#{#"language": #"en",
#"value": #"SOYA"},
#{#"language": #"de",
#"value": #"SOYA Bebida"}],
#"price": #"",
#"uri": #"<null>"}]}];
NSString *searchText = #"SOYA";
id beginsWithSearchText = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *product, NSDictionary *bindings) {
if ([product[#"name"] isKindOfClass:[NSString class]]) {
return [product[#"name"] hasPrefix:searchText];
} else if ([product[#"name"] isKindOfClass:[NSArray class]]) {
NSUInteger index = [product[#"name"] indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [obj[#"language"] isEqualToString:NSLocale.currentLocale.languageCode];
}];
return index != NSNotFound && [product[#"name"][index][#"value"] hasPrefix:searchText];
} else {
return NO;
}
}];
NSArray *result = [data.firstObject[#"products"] filteredArrayUsingPredicate:beginsWithSearchText];
XCTAssertEqual(result.count, 1);
XCTAssertEqualObjects(result.firstObject[#"_id"], #"5931c73303f737086");
}
I have MutableArray like this...
restaurants = ({
cuisines = ({
"cuisine_id" = 1;
"cuisine_name" = Indian;
},
{
"cuisine_id" = 2;
"cuisine_name" = SriLanka;
}
);}
{ cuisines = ({
"cuisine_id" = 1;
"cuisine_name" = Chinese;
},
{
"cuisine_id" = 2;
"cuisine_name" = Afghani;
}
);}
{cuisines =({
"cuisine_id" = 1;
"cuisine_name" = Chinese;
},
{
"cuisine_id" = 2;
"cuisine_name" = Afghani;
}
);}
)
And I'm Filtering Restaurants Array With multiple values of cuisine_name
For Example if I have a,
Nsarray like this cuisine_name (
Indian,
Srilankan,
Chinese,
)
I want to filter my Restaurants array on basis of each object of cusine_name.
At present I'm doing like this..
NSPredicate *predicateCuisines = [NSPredicate predicateWithFormat:#"(cuisines.cuisine_name = %#)",[cuisineTypes valueForKey:#"cuisine_name"]];
[self.arrRestaurantList filterUsingPredicate:predicateCuisines];
But it filtering Using description...For Restaurant Array which contains whole three values(Indian,Srilankan,Chinese) but i want to filter with each object of cuisine_name.
Any help will really appreciated..
plz use this
NSArray* arrtu4 = #[#{#"cuisines" : #[#{#"cuisine_id" : #1,#"cuisine_name" : #"Indian"},#{ #"cuisine_id" : #2, #"cuisine_name" : #"SriLanka"}]},#{ #"cuisines" : #[#{#"cuisine_id" : #1,#"cuisine_name" : #"Chinese"},#{#"cuisine_id" : #2,#"cuisine_name" : #"Afghani"}]},#{#"cuisines" :#[#{#"cuisine_id" :#1,#"cuisine_name" : #"Chinese"},#{ #"cuisine_id" : #2,#"cuisine_name" : #"Afghani"}]}];
NSMutableArray *arrryfilted = [NSMutableArray array];
NSArray *arrayCountrys = #[#"Chinese",#"SriLanka",#"Indian"];
for (int i = 0; i<arrtu4.count; i++)
{
NSArray *filterArray2 = [[arrtu4 objectAtIndex:i]valueForKey:#"cuisines"];
NSArray *filterArray3 = [filterArray2 filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return [arrayCountrys containsObject:[evaluatedObject valueForKey:#"cuisine_name"]] ;
}]];
[arrryfilted addObjectsFromArray:filterArray3 ];
}
NSLog(#"%#",arrryfilted);
output:
2016-08-20 16:44:59.856 coredataReltaion[6762:152672] (
{
"cuisine_id" = 1;
"cuisine_name" = Indian;
},
{
"cuisine_id" = 2;
"cuisine_name" = SriLanka;
},
{
"cuisine_id" = 1;
"cuisine_name" = Chinese;
},
{
"cuisine_id" = 1;
"cuisine_name" = Chinese;
}
)
I know this question has been asked many times but not got the solution or i might not understood that's why posting the question.
for (int web=0; web<[web_arr count]; web++) {
AdditionalBookmark *web_book=[[AdditionalBookmark alloc]init];
UITextField *txtNam = (UITextField*)[self.view viewWithTag:1100+web];
NSString *txtName = txtNam.text;
UITextField *txtLin = (UITextField*)[self.view viewWithTag:1200+web];
NSString *txtLink = txtLin.text;
if ((txtName && txtName.length > 0)||(txtLink && txtLink.length > 0))
{
web_book.additionalBookmark_Name = txtName;
web_book.additionalBookmark_Link= txtLink;
web_book.contactDetails_Id=self.contactDetailsinfo.contactDetailsId;
web_book.additionalBookmark_Id=[[[web_arr objectAtIndex:web] valueForKey:#"WeblinkId"] intValue];
[dictWebLinks setObject:txtName forKey:#"WeblinkName"];
[dictWebLinks setObject:txtLink forKey:#"WeblinkUrl"];
[arrForWebLinks addObject:dictWebLinks];
}
[db updateIntoAdditionalBookmarkWithObject:web_book];
}
[dictUpdate setObject:arrForWebLinks forKey:#"Weblink"];
The problem am facing is this suppose there are two items in the array
Printing description of web_arr:
<__NSArrayM 0x170e4db60>(
{
WeblinkId = 9;
WeblinkName = Hdhd;
WeblinkUrl = "ie.comh";
},
{
WeblinkId = 10;
WeblinkName = Hd;
WeblinkUrl = "nd.com";
}
)
what i am getting is below:
Printing description of arrForWebLinks:
<__NSArrayM 0x170e4db30>(
{
WeblinkName = Hd;
WeblinkUrl = "nd.com";
},
{
WeblinkName = Hd;
WeblinkUrl = "nd.com";
}
)
dictionary is replacing the value for same key holding the latest value only, how can i mange to save the values in same format as in web_arr.
Any help is appreciable.
You are adding the same dictionary to the array over and over:
[dictWebLinks setObject:txtName forKey:#"WeblinkName"];
[dictWebLinks setObject:txtLink forKey:#"WeblinkUrl"];
[arrForWebLinks addObject:dictWebLinks];
You need to create a new one each time:
[arrForWebLinks addObject:#{
#"WeblinkName" : txtName,
#"WeblinkUrl" : txtLink
}];
(and remove dictWebLinks).
I am using this code: https://github.com/valentinlietz/MySQL-Connect to use MySQL queries in objective c. I use Count(*) to check if this row already exists.
I used this code to NSLog(); this dictionary. Code:
for(NSArray *row in response.responseDictionary){
NSLog(#"%#", row);
}
Result:
{
"" = "<null>";
"COUNT(*)" = 0;
}
But when I use [response.responseDictionary objectForKey:#"COUNT(*)"]; It's returning Unrecognized selector sent to instance.
Update:
Row:
Row: ( { "" = "<null>"; "COUNT()" = 0; } )
Dictionary:
Row: { "" = "<null>"; "COUNT()" = 0; }
I haven't worked with that project, but from https://github.com/valentinlietz/MySQL-Connect/blob/master/MySQL.m it looks to me that response.responseDictionary is the result
of converting the JSON response of the script https://github.com/valentinlietz/MySQL-Connect/blob/master/mysql.php to a Foundation object.
And the code
while($row = mysql_fetch_array($query))
{
for($i = 0; $i < 50; $i++) {
$arr2[$fields[$i]] = $row[$fields[$i]];
}
array_push($return_arr, $arr2);
}
echo json_encode($return_arr);
in that PHP script shows that the response is an array of dictionaries.
So if I am correct, the name and type of the responseDictionary property is badly chosen, because it is not an NSDictionary, but an NSArray where each object is an NSDictionary,
and the following should work:
NSArray *responseArray = (NSArray *)response.responseDictionary;
NSLog(#"count = %#", [[responseArray objectAtIndex:0] objectForKey:#"COUNT(*)"]);