Could someone please help me construct a realm query to search for a video(s) of a particular entryid value, for extra points a second query that would find both videos by their entryids ?
Thank you!
Video {
nid = 1;
video_title = OK;
video_description = Description {
value = Description;
};
video_kaltura = Kaltura {
entryid = 0_abababa3;
mediatype = 1;
};
}
Video {
nid = 2;
video_title = OK;
video_description = Description {
value = Description;
};
video_kaltura = Kaltura {
entryid = 0_cdcdcdc3;
mediatype = 1;
};
}
I've tried things like:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"video_kaltura['entryid'] CONTAINS[c] '0_abababa3'"];
RLMResults *test = [Video objectsWithPredicate:pred];
and their variations to no avail.
NSPredicate uses key paths to traverse properties (object.someProperty.otherProperty).
Based on your example data, you'd want a predicate like video_kalutra.entryid == '0_abababa3'.
I have a record of 100 Electronic items with Categories in database, with every request I load 20 records in Table View. I want to merge 2 arrays into 3rd array
E.g.
1) NSMutableDictionary *prevResultSet;
2) NSMutableDictionary *newResultSet;
3) NSMutableDictionary *finalArray;
I tried using addEntriesFromDictionary but it overwrites the duplicate key instead of merging.
_finalArray = [[NSMutableDictionary alloc] init];
[_finalArray addEntriesFromDictionary:_prevResultSet];
[_finalArray addEntriesFromDictionary:_newResultSet];
_prevResultSet = {
LED = (
{
rate = "25,000";
type = Sony;
},
{
rate = "25,000";
type = Samsung;
},
);
LCD = (
{
rate = "15,000";
type = Samsung;
},
{
rate = "15,000";
type = Sony;
},
);
_newResultSet = {
LCD = (
{
rate = "15,000";
type = LG;
},
{
rate = "15,000";
type = Onida;
},
);
Current Output: (After using addEntriesFromDictionary:)
_finalArray = {
LED = (
{
rate = "25,000";
type = Sony;
},
{
rate = "25,000";
type = Samsung;
},
);
LCD = (
{
rate = "15,000";
type = LG;
},
{
rate = "15,000";
type = Onida;
},
);
Expected Output:
_finalArray = {
LED = (
{
rate = "25,000";
type = Sony;
},
{
rate = "25,000";
type = Samsung;
},
);
LCD = (
{
rate = "15,000";
type = Samsung;
},
{
rate = "15,000";
type = Sony;
},
{
rate = "15,000";
type = LG;
},
{
rate = "15,000";
type = Onida;
},
);
Thanks in advance...
Check for each key in newResultSet if it exists and merge or add the array.
NSMutableDictionary *finalDictionary = [prevResultSet mutableCopy];
[newResultSet enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSArray *array = finalDictionary[key];
if (array)
finalDictionary[key] = [array arrayByAddingObjectsFromArray:obj];
else
finalDictionary[key] = obj;
}];
Well, this is expected.
From the Documentation, addEntriesFromDictionary tells that:
If both dictionaries contain the same key, the receiving dictionary’s
previous value object for that key is sent a release message, and the
new value object takes its place.
You need to use setObject to add each object to the dictionary.YOu need to loop through the keys of one dictionary and add it to the final dictionary.
Even setObject tells the same:
The key for value. The key is copied (using copyWithZone:; keys must
conform to the NSCopying protocol). If aKey already exists in the
dictionary, anObject takes its place.
You cannot have two same keys in the dictionary. All keys in the dictionary are unique.
If you still want to have the same key-value in the dictionary, you must use a different key.
For example, you have two dictionaries with following values:
NSDictionary *dict1=#{#"hello":#"1",#"hello2" :#"2"};
NSDictionary *dict2=#{#"hello":#"1",#"hello2":#"2",#"hello3":#"1",#"hello6":#"2",#"hello4":#"1",#"hello5" :#"2"};
NSMutableDictionary *mutableDict=[NSMutableDictionary dictionaryWithDictionary:dict1];
for (id key in dict2.allKeys){
for (id subKey in dict1.allKeys){
if (key==subKey) {
[mutableDict setObject:dict2[key] forKey:[NSString stringWithFormat:#"Ext-%#",key]];
}else{
[mutableDict setObject:dict2[key] forKey:key];
}
}
}
and by the end of this loop, your new mutable dictionaries will have the follwoing key-values:
{
"Ext-hello" = 1;
"Ext-hello2" = 2;
hello = 1;
hello2 = 2;
hello3 = 1;
hello4 = 1;
hello5 = 2;
hello6 = 2;
}
As you can see, hello, and hello2 keys are renamed as Ext-hello1, Ext-hello2. form the dict1, and you still have all the dict2 values added to your mutable dict.
IF you don't want to add a new key, then you can add the values into an arrya and add that array to the dictionary. YOu can modify the for-loop to:
for (id key in dict2.allKeys){
for (id subKey in dict1.allKeys){
if (key==subKey) {
NSMutableArray *myArr=[[NSMutableArray alloc]init];
[myArr addObject:dict1[subKey]];
[myArr addObject:dict2[key]];
[mutableDict setObject:myArr forKey:key];
}else{
[mutableDict setObject:dict2[key] forKey:key];
}
}
}
And now you will have the values merged into an array:
{
hello = (
1,
1
);
hello2 = 2;
hello3 = 1;
hello4 = 1;
hello5 = 2;
hello6 = 2;
}
In this way, the number of keys will be same, and the values for the same key will be added as an array.
I have a json array that I'm trying to extract data from. The array was created using NSJSONSerialization. Here is what the json array looks like from NSLog([jsonArray debugDescription]);:
{
properties = (
{
ID = 12345;
PropertyName = "McDonalds";
key = 00112233445566778899aabbccddeeff;
},
{
ID = 12346;
PropertyName = "Taco Bell";
key = 00112233445566778899aabbccddeef0;
},
{
ID = 12347;
PropertyName = "Burger King";
key = 00112233445566778899aabbccddeef1;
}
);
success = 1;
totalCount = 3;
}
I need to extract each ID and each Property name and dump the values into separate arrays. How can I do this?
You can use Key-Value Coding:
NSArray *ids = [jsonArray valueForKeyPath:#"properties.ID"];
NSArray *propertyNames = [jsonArray valueForKeyPath:#"properties.PropertyName"];
Is there a way to directly access an inner-Dictionary of an outer Dictionary in Objective-C? For example, I have key to an object which is part of inner dictionary, Is there any way to directly access object from that key.
GameDictionary {
Indoor_Game = { "game1" = chess; "game2" = video_Game; "game3" = poker; };
OutDoor_Game = { "game4" = cricket; "game5" = hockey; "game6" = football; };
};
I have a key "game4", but I don't know in which dictionary object of this key is present, currently I have to search in each dictionary for object, the code which I am using is:
NSString* gameName = nil;
NSString* gameKey = #"game4";
NSArray* gameKeys = [GameDictionary allKeys];
for (int index = 0; index < [gameKeys count]; index ++) {
NSDictionary* GameType = [GameDictionary objectForKey:[gameKeys objectAtIndex:index]];
if ([GameType objectForKey:gameKey]) {
gameName = [GameType objectForKey:gameKey];
break;
}
}
Is their any easy way to access directly to the inner dictionary instead of for loops.
valueForKeyPath looks like what you want.
[GameDictionary valueForKeyPath:#"OutDoor_Game"]
//would return a dictionary of the games - "game4" = cricket; "game5" = hockey; "game6" = football;
[GameDictionary valueForKeyPath:#"OutDoor_Game.game4"]
//would return cricket
https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html
I have a structured array like the one below
(
{
id = 0;
name = "A";
tables = (
{
comment = "";
id = 0;
name = "T1";
},
{
comment = "";
id = 1;
name = "T2";
},
{
comment = "";
id = 4;
name = "T3";
}
);
},
{
id = 1;
name = "B";
tables = (
{
comment = "";
id = 5;
name = "T1";
},
{
comment = "";
id = 6;
name = "T2";
}
);
}
)
Given that I know the value of id key of one of the dictionaries (let's take id=6), how can I get its parent dictionary or to be more precise the value of name key (in this case B).
I've tried using predicate strings but the problem is that the parent dictionary has also a key named id, so I get the wrong results.
EDIT: id keys are unique (and it is also not necessary that ids of the elements in the first dictionary have lower intiger values than the ones in the dictionaries with higher indexes)
Try this, where array is your structured array
NSDictionary *parentDict=[[NSDictionary alloc]initWithDictionary:nil];
NSString *name=[[NSString alloc]init];
NSArray *table=[[NSArray alloc]initWithArray:nil];
for (NSDictionary * dict in array) {
table=[dict objectForKey:#"tables"];
for (NSDictionary *childDict in table) {
if ([[childDict objectForKey:#"id"]isEqualToString:#"6"]) {
//if your id value is int then use ==6 to check
parentDict=dict;
name=[dict objectForKey:#"name"];
return;
}
}
}