Now I have a 1-dimonsion NSArray, each element of which is a NSMangedObject with a NSDate Attribute,
for example
an event Entity has an date(NSDate) attribute and name(NSString) attribute.
So the initilized array should be like this:
(NSArray) Events: event1, event2, event3,...,nil
Now, I need to group these objects by their date so that I can make this array the data source of a sectioned tableView.
And I just need the elememts in a same section have the same date, here the date means year,month,day, and with no hour or minute.
So the grouped NSArray should be a 2-dimension array, such as:
(NSArray) Events: day1, day2, day3, day4,...,nil;
and here day1,day2... are also a NSArray, and inside of them are really event objects. like this:
(NSArray) day1: event1, event2, event3, ..., nil;
So how can I realize this ?
I guess I can not use NSFetchedResultsController, since actually I have to fetch several entities, each of which has a NSDate attribute, and put them together to be grouped by date.
and I believe there's no problems about fetching,
I jus wonder how can I transfer the fetched 1-dimension array into the date-based 2-dimension array that I need?
Hoping someone to help and thanks very very much for answering!
You can use a custom NSValueTransformer to convert your entities' dates to ranges (or days). From there, you can use bindings with that if you like.
Related
I have an array of objects that has a member that is type Date, and I'm trying to sort the whole array by Date, and it's not sorting correctly.
This is the code I'm using, the name of the array is alarms and the name of the member type Date is time.
alarms.sort(by: { $0.time.compare($1.time) == .orderedAscending })
and whenever I sort it it just doesn't work correctly, and I'm testing it by printing all the values in a for loop.
Can someone help me with the syntax for this?
The compare is a NSDate function. With Date you can just use < operator. For example:
alarms.sort { $0.time < $1.time }
Having said that, compare should work, too, though. I suspect there's some deeper issue here, that perhaps your time values have different dates. You might only be looking at the time portion, but when comparing Date objects, it considers both the date and time. If you only want to look at the time portion, there are several ways of doing that, for example, look at the time interval between time and the start of the day:
let calendar = Calendar.current
alarms.sort {
let elapsed0 = $0.time.timeIntervalSince(calendar.startOfDay(for: $0.time))
let elapsed1 = $1.time.timeIntervalSince(calendar.startOfDay(for: $1.time))
return elapsed0 < elapsed1
}
There are lots of ways to do this, but hopefully this illustrates the idea.
My data looks like:
objA date1 objnumber2
objB date2 objnumber1
objC date2 objnumber4
objD date2 objnumber3
objE date1 objnumber7
objF date3 objnumber6
objG date1 objnumber5
I am looking for all the objects which are from the Nth last date. The result objects need to be sorted using objnumber (this should be easy using NSSortDescriptor).
So if I specify N=1 (most recent date), I should get [objF] only. (date3 is most recent)
If N=3 (oldest date), I should get sorted [objA, objG, objF]
The sorting part is easy.
My question is do I really need to firstly search for the latest date (using combination of sortdescriptor and nsfetchrequest searchLimit) in the entire data? Then do a second search to find all objects from that date (using nspredicate) and sort it?
Or is there a better way to perform this type of search? How would you generalize this for Nth date instead of last date? That would be a big performance hit no??
NOTE that the date is not known beforehand.
Edit2: okay this is even more complicated since I am using NSDate. So pretty much all the objects have unique dates lol. Gotta throw nsdateformatter in there in the mix too :(
I figured out a solution to my problem.
Instead of using the date, I added a new Int64 NSNumber attribute. Everyday the obj is added, all the objects are tagged with that number for that particular day.
For retriving all objects from Nth day, I firstly do a "fetchlimit 1" nsfetchrequest for that number in a sorted fetchrequest. That gives me the last number.
Now for the Nth last objects, I subtract N from the last number. Then I perform a "nspredicted" nsfetchrequest for all objects with that number attribute. Then I simply just sort the result array.
This resolves my question :D
my app is in the appstore and after updating to ios 8 and testing the app on my new iphone 6 the app doesnt work correctly.
ive already found the problem: i stored 3 dimensional arrays in nsuserdefaults and its giving me the error:mutating object sent to immutable....
so i can forget to store 3d arrays in nsuserdefaults, because i can`t really change those.
My idea was to store all the data in core data. GREAT!
But i gotta learn!
So....
I have got 5 attributes like you can see on the image:
when pushing an add-button all of the 5 attributes get saved by core data in the sqlite file.
The attribute "datum" of type date comes from a datepicker,so i can select a date. the other attributes are hours,minutes and some note - strings.
so, i want to look my tableview like this:
2014 <--- sectiontitle with year
-October <--- rows cell.text
-November <--- rows cell.text
2015 <--- sectiontitle with year
-January <--- rows cell.text
-May <--- rows cell.text
In this example i made entries for october+november in 2014 and january+may in 2015.
This means, this tableview is an overview to all the data,that i want to be filtered in the year and month.
what is the best approach to do this.
is there an easier way than: fetching the date, putting the dates into an array, setting dateformatter and find non-equal dates to get the years and the months?
because, when pushing a cell, for example october in section 0, a detailview opens and i want to display all entries i made in october in 2014.
What is your advice? Please help!!!
If user defaults is sufficient for your needs, I'd stick with it but convert the data to something mutable when you read it. You can convert an immutable property list to a mutable one using CFPropertyListCreateDeepCopy. As the name implies, it's a deep copy, and you can have it convert mutable containers and/or leaves to mutable equivalents.
If you read from user defaults into NSDictionary *userDefaultsData, convert with
NSDictionary *mutableData = CFPropertyListCreateDeepCopy(NULL, userDefaultsData, kCFPropertyListMutableContainers);
That will recursively copy the entire thing, converting NSDictionary to NSMutableDictionary and NSArray to NSMutableArray as it goes.
Thanks again Tom, you saved me from much work!!!
In appdelegate i changed...
stdPickerArray= [[NSMutableArray alloc] initWithArray:[def objectForKey:#"gesamtStdArray"]];
to...
stdPickerArray= [[NSMutableArray alloc] initWithArray:[def objectForKey:#"gesamtStdArray"]];
stdPickerArray = CFBridgingRelease(CFPropertyListCreateDeepCopy(NULL, (CFPropertyListRef)stdPickerArray, kCFPropertyListMutableContainers));
awesome!!!
I have an NSMutableArray with the latest 30 days records fetched from a DB.
Each record consists of a string and a creation date.
Now I want those records to feed an UITableView that will have a section for each different day and one cell per section simply showing how many records exist for each day.
Well, it's been hours of thinking and I'm not able yet to figure out how to do it. All the questions I've found on stackoverflow are related to core data, but I don't use it in my app.
Any help will be appreciated.
//after fetching data,write below code<br>
int cArray[30]; //create it as global array.cArray stores number of records that fall in particular date. if cArray[j]=10, it means for j'th date, there are 10 records.
// initialise all to zero.
for (int i=0; i<30; i++) {
cArray[i]=0;
}
NSArray *myArray;// my array stores your records.
[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Record *rec = (Record*)obj;
cArray[(int)(rec.creationDate)]++;
}];
And later you can use cArray to show number of records that fall in a particular date.
Hope this helps.
I have implemented my data model similar to this case. I have an array of the NSDateComponents for each day and have a dictionary which keys is the date components that is stored in the array and values is the arrays that is store the data for each day. This approach is working pretty well for me.
You can group your data according to date. Then you can store grouped array on a dictionary and the key will be the date and this key will be your table header and the value will be the list of data at that date. I hope you got this approach.
I've got an array of NSDates and I'd like to grab the largest NSDate from the array. While i could always sort them and grab the first/last, is there a way to do this with KeyValueCoding or some other quick one liner kind of way? I know that I could use something like valueForKeyPath#"#max.date" if the objects had a date property, but what if the objects are dates themselves??
thanks
You can use,
NSDate *maxDate = [dateArray valueForKeyPath:#"#max.self"];
This will give you the largest date from array. You dont have to sort the array before doing this.
From the documentation,
The #max operator compares the values of the property specified by the
key path to the right of the operator and returns the maximum value
found. The maximum value is determined using the compare: method of
the objects at the specified key path. The compared property objects
must support comparison with each other. If the value of the right
side of the key path is nil, it is ignored.
Note that #max will do compare: and then will find out the max value.
Agree with #SeanDanzeiser. To be more specific, here's a ~70 byte one-liner:
// if dateArray is the array of dates ...
NSDate *maxDate = [[dateArray sortedArrayUsingSelector:#selector(compare:)] lastObject];