How do I identify NSDictionary is empty or not - ios

I need help.
I want to show data if there is data in dictionary else I want to show No-Data screen if there is no value in dictionary. But problem is like this that I'm getting count as 1. Though there is no value in dictionary.
As you all are trying to guess this one is duplicate. But my question is little-bit different. In this dictionary I have multiple key-value pairs. So I can't find for one key, that's why I'm just trying to get count of it's.
NSLog(#"%lu",(unsigned long)dict.count); //<-- It prints : 1
NSLog(#"%#", dict); //<----It will print below structure with no data
//Output :-
(
{
}
)
So how do I will identify like this dictionary to show it is empty. Though I tried but it is going count as 1.

According to the output dict is not a dictionary ({}), it's an array (()) containing one empty dictionary.
To check if there is a dictionary in the array which is not empty you could use (I renamed dict to array)
if (array.count > 0 && array[0].count > 0) { // will be evaluated to true if the dictionary is not empty

Related

How do I store a maximum amount of 5 objects in a NSMutableArray?

I am creating an array of NSdictionarys and putting that array in NSUserDefaults. I then put the array data in a recently added tableview. I am trying to get it so it would store the 5 most recent objects added to that array. How can I go about this? Below is my code:
- (IBAction)searchButton:(UIButton *)sender {
[_jobDic setObject:_jobField.text forKey:#"jobRecent"];
[_jobDic setObject:_locationField.text forKey:#"locationRecent"];
[_recentJobArray addObject:_jobDic];
[_recentDefaults setObject:_recentJobArray forKey:#"recentJobs"]
[_recentDefaults synchronize];
}
Assuming you are referring to the _recentJobArray, you could do this:
if (_recentJobArray.count == 5) {
[_recentJonArray removeObjectAtIndex:0]; // remove oldest
}
[_recentJobArray addObject:_jobDic];
If you keep your array sorted in newest-to-oldest order then maddy's solution will work well. Otherwise you'll need to save a date key/value pair in each dictionary, and then loop through the array looking for the oldest item. The code might look something like this
NSDictionary *oldest;
for (NSDictionary *aJobDict in _recentJobArray)
{
NSDate *dateOfThisDict = aJobDict[#"date"]; //Assumes each dict has a key #"date"
if (!oldest || [oldest[#"date"] compare: dateOfThisDict] == NSOrderedDesending)
oldest = aJobDict;
}
[_recentJobArray removeObject: oldest];
[_recentJobArray addObject: newDict];

Dictionary key-value using with array

I have an array and it has lots of dictionary's keys it comes from API. My array as follows
Dictionary keys array :
NSArray *arr = #[#"01", #"02", #"03"];
Dictionary with key-value pairs
NSDictionary *dic = #{#"01": #"Hero", #"02" : #"Enemy", #"03" : #"Boss"};
Basically i want to match array values corresponding to dictonary keys without using array. I found a solition about that but I don't want to use for-loop for every cell(I have a lots of cell). My solution is like that
for(NSString *item in arr) {
[convertedArr addObject:[dic valueForKey:item]];
}
NSLog(#"%#", [convertedArr componentsJoinedByString:#","]);
Asumme have an array like this (1,2,3) and dictionary looks like {1 = "a", 2 = "b", 3 = "c"} I just want to give an array and it should return dictionary values like this ("a","b","c")
Anybody should give me better approach without using array? Thanks.
You can replace your for-loop by
NSArray *convertedArr = [dic objectsForKeys:arr notFoundMarker:#""];
which is at least less code. (The notFoundMarker: is added for all keys
in the array which are not present in the dictionary. Your code would crash
in that situation.)
It might perform slightly better because it is a library
function. But I doubt that the difference is big, because in any case a dictionary
loopup is required for all keys in arr.

How to check for 0 objects in the value field of an NSDisctionary

I have a NSDictionary (MyDictionary) which is currently returning "0 objects" for the
[mylist] key:value pair.
MyDictionary:
{
mylist = (
);
}
How can I check if the value for the key contains 0 objects?
I've tried the following without success:
if([MyDictionary objectForKey:#"mylist"] !=[NSNull null]){} //evaluates to false
if([MyDictionary valueForKey:#"mylist"] !=[NSNull null]){} //evaluates to false
NSNull is not the same thing as nil.
NSArray *myList = MyDictionary[#"mylist"];
if (myList.count == 0) {
//empty or nil!
}
Your dictionary has one key-value pair, where the value is an empty NSArray. Note that an empty NSArray is not the same as an NSNull object, nor is it even a nil pointer. It is in fact, a valid pointer to an NSArray object that just happens to have nothing in it.
To determine that there are no elements in the array, you must first extract the array from the dictionary, and then check the count of the array to determine whether it has any elements. This can all be done in one line of code like this
if ( [MyDictionary[#"mylist"] count] == 0 )
// array is empty
The value is an array. To check if an NSArray is empty, you can check whether its length is 0.

ios - deserialized json data as a two-level dictionary. how to make it an array with dictionaries?

I am deserializing data into a dictionary, but the result is a nested dictionary so it looks like:
{"0":{"key1":"value1","key2":"value2"},
{"1":{"key1":"value1","key2":"value2"},
{"2":{"key1":"value1","key2":"value2"}}
I want to use this in a tableview so how would I go about turning that dictionary into an Array where each objet in the array is a dictionary with the value pairs?
I want to be able to step through the array, and reference the value pairs for each row of the array.
Thanks!
for (NSDictionary *dict in outerDictionary.allValues) {
[tableArray addObject:dict];
}
Indeed, or simpler:
tableArray = outerDictionary.allValues;

NSPredicate filter array but return only one field of an object

I'm sure this question has been asked but I don't know what to search on.
I have array of Message objects with the following fields {selected[BOOL], messageText[STR]}. I want to filter this array to get only the objects with selected=TRUE. So far so good.
NSArray *messagesFiltered = [self.fetchedResultsController.fetchedObjects filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"selected == TRUE"]];
However, I don't need the objects themselves in the returned array, I need an array of the messageText strings. How to modify predicate to return only the messageText strings and not the whole object?
That's not really the job of a predicate. Predicates are only for filtering, not modifying, the array you apply them to.
However, there's a fun little trick: the method -valueForKey: will apply the kind of transform you want to your array. Just call it with the messageText key that you want:
NSArray *messagesFiltered = [self.fetchedResultsController.fetchedObjects filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"selected == TRUE"]];
NSArray *realMessages = [messagesFiltered valueForKey:#"messageText"];
On an array, -valueForKey: asks each element for a value for the given key, then returns an array of everything the elements returned. (Any element that returns nil for the key you pass shows up as [NSNull null] in the resulting array.)

Resources