Objective-C: How to get data from selected row in table? - objective-c

I need to retrieve a string from data from a selected row in a table to compare it to a string.
I call
NSArray *values = [self.tableArrayController selectedObjects];
To get the objects at the selected row. However, values only has a count of objects of 1, and everything seems to be one big, long string of all my values. I cannot search through the values because I won't always know what to search for. I need a specific value (of which they are organized by columns).
I was hoping that the array would be of a certain count and I could retrieve the string based on the index of the object for the string.
How should I get all the different values (organized by columns) for a selected row?
EDIT:
I populate a NSDictionary (withmore values than that, this is just an example). The string value is the column identifier. The object is the object I'm adding that I want to appear on the table. This part below already works as I want it to:
NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:
object1, #"column",
object2, #"column2",
nil];
[self.tableArrayController addObject:dict];

Another way to get data from your table is to look at the "NSTableView" methods "selectedRow" & "selectedColumn".
With that information, you can get at the value you want directly via your array controller or you can use the NSTableViewDataSource method "tableView:objectValueForTableColumn:row:" to pass you back the NSString value of whatever it is that you're trying to get at.

Related

Is NSMutableDictonary always sorting the keys in alphabetical order?

I have created a NSMutableDictonary.
The problem is that I am getting all the keys sorted into an alphabetical order. Why is it like this?
I have just noticed this today (never checked this before). Is this the actual implementation of NSMutableDictonary? Is there any way I can get the key/values in the order I have declared?
Below is the code in the screenshot:
- (IBAction)OnBtnSubmit:(id)sender {
NSString *strUid=[[NSUserDefaults standardUserDefaults] objectForKey:#"UID"];
NSString *picData=[self imageToNSString:ProfileImage.image];
NSString *strComment=_commentTxt.text;
NSString *strUnit=_unitTxt.text;
NSMutableDictionary *parameters =[NSMutableDictionary dictionaryWithObjectsAndKeys:strUid,#"uid",strComment,#"comment",strUnit,#"unit_donate",#"hi",#"image",nil];
NSLog(#"parameters : %#",parameters);
My concern is, can I get the key/values in the order I have declared?
Like below:
NSMutableDictionary *parameters =[NSMutableDictionary dictionaryWithObjectsAndKeys:strUid,#"uid",strComment,#"comment",strUnit,#"unit_donate",#"hi",#"image",nil];
{
uid = 85;
comment= test;
unit_donate = 1;
image=hi;
}
I Need this exact order as on the web end there is an array that stores the value for all these key. I need to make sure image will always be in the 4th position.
But I'm curious to know, I have logged the dictionary that I have created; why it is showing in alphabetical order? Is this the actual order that my dictionary holds or is there any other reason?
Yes, It will be like that. But whatever the order is it should not concern with your logic as you are always going to fetch value baesd on key not the based on order.
I am pretty sure that dictionaries are not keeping track of the input order. How are you outputting the dictionary, looping through keys or just printing the dictionary?
If you know the order you want to retrieve the objects, you can create your own version of the keys array and loop through that to pull out the objects from the dictionary in your desired order
For more details do check how dictionary works

NSManagedObjects with like properties

I have a UITableView that displays the value of a property called name of an NSManagedObject using CoreData. I have it working by just using a basic NSFetchRequest and then displaying the value of name in the UITableViewCell's textLabel.
However, many of the NSManagedObject's have the same name value, so I get duplicates in my table. How can I filter it so that I only have one of each name value?
Thanks for any help.
You can configure your fetch request to only return distinct values but that required that you return dictionaries instead of managed objects. Since you are asking for dictionaries you will have to specify what values to return.
You can see my answer to avoid duplicate results on Core Data fetch.
In short:
request.resultType = NSDictionaryResultType;
request.propertiesToFetch = [NSArray arrayWithObject:#"name"];
request.returnsDistinctResults = YES;

taking out objects from NSMutableArray

I have an array which contains objects some may be same and some are different.
How can I take each same objects and different objects separately ?
Below is the array
NSMutableArray *items = [[NSMutableArray alloc]
initWithArray:[NSArray arrayWithObjects:#"rat", #"rat", #"cat",#"Lion", #"cat", #"dog", #"dog", nil]];
I want to have four arrays which will contains these items :
First array with two rats
2nd array with two cats
3rd array with one lion
4th array with two dogs
What could be the best way to take the objects out ? Identical object should be placed in same array.
Here's a general answer:
Put the array into an NSCountedSet - that will store each object and a count of the number of times it has been added.
Then - for each object in this counted set create an array with that object repeated according to the count of each object.
It will work in your case, because you are using static strings, which are going to be the same if they are the same string. This will take more work if you are using custom objects.
But the real question we have to ask is why you need to create these repetitive structures. If we could know what you are doing with it, we could give you better advice about how to go about it. For example, if you just need to keep a running count of the number of each type of object you have, you could just use the NSCountedSet directly (it descends from NSMutableSet, so it is already mutable) and not bother with creating the arrays.

Merge NSMutableArray with a NSArray, filtering the duplicates

I have two arrays, an NSMutableArray and an NSArray. The NSMutableArray is the "store", it stores results from a source of NSArrays. Every 5 minute, a new NSArray comes in and the data needs to be filtered and sorted.
Sorting by date is pretty easy, so I managed to get the NSArray sorted by NSDate. Sorting the other array is not necessary, as it would only cause confusion with the user.
What I want to do: the NSArray has a lot of different objects that all respond to -[object name], returning an NSString. The NSArray needs to be merged into the NSMutableArray, only adding new objects.
The merging itself is no problem, but performance is. The NSMutableArray can contain up to 3000 items, and the NSArray can contain up to 250 items, although usually only 5 or 6 of these have to be merged into the NSMutableArray.
So, my question is: how do you merge two arrays in Objective-C, filtering the duplicates, without iterating (250*3000) times?
Tom
Edited to clarify something
The "duplicate" objects are objects that are duplicate to the user but not to the code. They have the same name, but not the same address.
More clarification: #"value" != #"value" // true
Is name a property of the objects being stored in the arrays? If so, you could use a fairly simple NSPredicate to filter the immutable array before adding the results to the mutable one. Here's an example:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"NONE name == %#.name", mutableArray];
resultsArray = [immutableArray filteredArrayUsingPredicate:predicate];
[mutableArray addObjectsFromArray:immutableArray];
How about this:
[mutable removeObjectsInArray:newArray];
[mutable addObjectsFromArray:newArray];
It isn't the fattest, but is easy to implement :)
Edited to remove some stupidity (left plenty, though)
A couple of options:
Remove all matching objects from the NSMutableArray using removeObjectIdenticalTo. This requires iterating through the smaller array, but as you note they're commonly small. Then,
Add all of the items from the new array using addObjectsFromArray
Or... well, it actually might be faster to instead:
Iterate through the new array looking for matches with indexOfObjectIdenticalTo, using addObject to add in non-matching objects.
Costly either way, but doable.
I would probably start by creating a new mutable array which contains the contents of your NSMutableArray and NSArray. Then, sort the new array based on the name property and then run through the array once, only pulling out the unique items.
Can you use NSSet and NSMutableSet instead? That could help deal with the duplicates issue.
Edit:
Based on your comments, you could use an NSSet to check for object membership quickly, in addition to your array. It'd require a bit more memory, but if you don't mind that, it could allow you to check really fast. You'd have your NSMutableArray backing store, and then an NSSet to keep track of object membership. You'd maintain the invariant that the NSMutableArray does not contain duplicates. You could use code like this:
// Assume that arrayStore is an NSMutableArray * instance variable
// Also, storeSet is an NSMutableSet * ivar
- (void)addObjectsFromArray:(NSArray *)data
{
for (id item in data) {
if (![storeSet member:item]) {
// Will have to keep arrayStore sorted somehow
[arrayStore addObject:item];
[storeSet addObject:item];
}
}
}
You only have to iterate through the NSArray. I'm not sure how NSSet is implemented off the top of my head, but checking for membership won't be an O(n) operation like it is for an unsorted array.
It's not the most efficient method, but it works well with what you already have in place, with minor modifications.
There are likely many ways to dramatically improve performance, but to be able to suggest any, we really need to know more about what the objects in the arrays "are": what do they represent? How are they being used? (For example, are the items in the store array being displayed in a table view?)
NSMutableDictionary, NSMutableSet, etc. could be combined with NSMutableArray to organize and implement the model in an efficient manner.
For example, let's say we know the object represents a person: MDPerson. A person has a gender, a date of birth, a name, a unique id, and a set of attributes that can change. Given this higher level understanding of what the object represents, we know that 2 people are equal only if their unique ids are the same (in other words, 2 different people can have the same name, gender, and date of birth). Let's say that your main NSMutableArray is made up of a list of 3000 people. The incoming array is made up of 500 people which are already in the main NSMutableArray. A few of these 500 people instances might have "updated" attributes, which means that their instance in the main array needs to be updated with that info.
Given that understanding, it's clear that the main list should be implemented as an NSMutableDictionary rather than an NSMutableArray. In the dictionary, the person's unique id would be the key, and their person instance would be the value for the key. You could then loop through the incoming array of 500 persons only once:
// main dictionary is called personIDsAndPersons
for (MDPerson *person in incomingPersons) {
MDPerson *existingPerson = [personIDsAndPersons objectForKey:[person uniqueID]];
// if nil, the person doesn't exist
if (existingPerson) {
// update the existing person's attributes
[existingPerson setUniqueAttributes:[person uniqueAttributes]];
}
}
Again, without knowing more of the details or having a higher level understanding of what the objects are, we're really just shooting in the dark.
You mention that 2 items are only the same if they have the same name. So, does that mean that each item in the main array of 3000 objects each have a unique name? If so, you could use an NSMutableDictionary to allow access to the objects in an efficient manner by having the keys in the dictionary be the name and the values be the object instance. You could then use a separate NSMutableArray that's used merely for display purposes: it allows an ordered, sorted organization of the same objects that are stored in the NSMutableDictionary. Remember that when you add an object to an array or a dictionary, normally you're not creating a new copy, you're just retaining the existing object.

Add missing objects to create an ordered collection

The subject is vague because I'm not sure how to articulate in one sentence what I want.
Here goes:
I have an NSArray of NSDictionaries. Each NSDictionary represents one day of the calendar year. Each NSDictionary has a key "date" with a value of NSDate. There should be 365 NSDictionary items in the array. The dictionary is created by a server that I don't control, and it sometimes is missing as many as 100 days.
I need to ensure the array has 365 dictionaries, each one day later than the next.
I currently sort the array by date, iterate through it, copying the NSDictionaries from the current array to a new array. While so doing, I compare the current Dictionary's date value with the date value for the next dictionary. If there is more than one day between the two dates, I add enough new dictionaries to the new array to cover those missing days (and set their dates accordingly), then continue through.
Since the dates are supposed to ordered, I wonder if there is not already a mechanism in the framework or language that I can use to say "Here is an array, and this keypath is supposed to be consecutive. Find and create the elements that are missing, and here's a block or method you can use to initialize them".
Something about my method just feels poorly implemented, so I turn to you. Thoughts?
Thanks.
The way you did it sounds perfectly sane, and there is nothing to my knowledge that will do it automatically in the base framework.
This code will sort them.
NSArray *dates; // wherever you get this...
NSArray *sortedDates = [dates sortedArrayUsingComparator:^(id obj1, id obj2)
{
return [[obj1 valueForKey:#"date"] compare:[obj2 valueForKey:#"date"]];
}];
As for creating the missing entries, you'll have to do that yourself.
You don't need to do the sort:
Create an array with 365 (or 366) placeholder dictionaries (you can possibly use the same one for all slots, or use NSNull)
iterate through the passed in array and figure out which day each of the dictionaries is for. Place each dictionary in its rightful slot in your array.