January 11, 2012, 4:14 pm
I’m storing birthdays in Core Data, and now I want to get a list of entities sorted by the day of their birth. However, I just cannot sort by the date, since the year of their birth would take precedence, I just need to sort by the month and day of the NSDate stored as the birth date.
The big problem that I faced was trying to find out how to get the Julian date from an NSDate. Well as it turns out, Julian date does not correctly describe what I was looking for, I needed to look for ordinal date.
After a bit of investigation, I was able to put together this category that I have hung off of NSDate in my app:
- (int)ordinalDate
{
return [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit forDate:self];
}
This, along with a sortUsingComparator of my NSMutableArray of contacts, is exactly what the doctor ordered.
BTW, Happy Birthday to the Grand Canyon National Monument, created this day in 1908, which eventually became the Grand Canyon National Park. (Yet another must see place.)
January 4, 2012, 1:52 pm
I found this one out the hard way.
If you are doing simple attribute additions when you transition from one Core Data model version to another, you can just create a new model version, set your overall data model’s default to the new version, and add the needed attributes to the entities as you need. When you fire up the app, the new fields are magically added to the table without any additional work.
However, when you want to rename an attribute, if you follow the above series of steps, the data that exists in the fields corresponding to the old attribute name is now replaced with a shiny new nil. Outstanding!
I guess I didn’t read the Apple Core Data docs well enough. Make sure that, when you add your model version and rename the attribute, make sure that you can see the Utilities view on the right side of the Xcode 4 window, click on the attribute you are renaming, click on the Data Model inspector (the third icon in the Utilities view, looks like a little database cylinder with an eye patch on it), and for the Renaming ID entry, type in the old name of the attribute. Once you do this, the data in the field should migrate forward.
Luckily, I didn’t blow away too many beta user’s data discovering this one.
BTW, a posthumous Happy Birthday to James Bond, the ornithologist whose book Ian Fleming had on his bookshelf and reportedly named his super spy character after.
November 28, 2011, 10:31 am
Just in case you were wondering how to use a UIButtonTypeInfoLight (or UIButtonTypeInfoDark) as a UINavigationItem (a la UIBarButtonItem), here is what I threw together. Put this code in your view controller’s viewDidLoad method:
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeInfoLight];
button1.frame = CGRectMake(0.0f, 0.0f, 40.0f, 32.0f);
[button1 addTarget:self action:@selector(aboutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aboutButton = [[UIBarButtonItem alloc] initWithCustomView:button1];
self.navigationItem.leftBarButtonItem = aboutButton;
[aboutButton release];
Adjust the frame to your taste, and supply the aboutButtonPressed method with an (id)sender parameter, and you are good to go.
November 23, 2011, 11:42 am
Thanks to everyone who turned out on Tuesday night for my CIDUG presentation on utilizing open source iOS projects. The presentation went a bit longer than I was hoping, largely due to the volume of demos that I had.
If you would like to download a PDF of the presentation, here is a link:
Open Source iOS Projects presentation
Also, if you would like to download a ZIP file of all of the demos that I did, here is a link for that:
Open Source iOS Projects demos
I realized this morning that what I should have done was to create a workspace, and then drag all of the individual projects into that one workspace for the demo. I have done this in the code that I uploaded, so when you unzip the demos file, just open the Demo projects.workspace file and then just use the Scheme selector at the top of the Xcode window to select which demo you want to look at or try out.
Please let me know if you have any questions or suggestions on the presentation or the demo code. I hope everyone out there has a safe and happy holiday season.
November 11, 2011, 4:01 pm
It has been a while since I have made a post, so I figured I would take this opportunity to point out that, if you are ever putting together a UITableViewCell (such as in the cellForRowAtIndexPath delegate method), there is a big difference between this:
cell.backgroundColor = [UIColor redColor];
And this:
cell.contentView.backgroundColor = [UIColor redColor];
Or, if you are creating an NSSortDescriptor, there is a big difference between this:
sortDesc = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
And this:
sortDesc = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
Happy Nigel Tufnel day and Last Binary Day Of This Century (11/11/11) to everyone!
October 14, 2011, 9:16 am
A couple of years ago, I wrote a blog post about displaying and dismissing a view controller in an iPhone application. To get rid of your view controller from within that view controller, you would simply run this line of code:
[[self parentViewController] dismissModalViewControllerAnimated:YES];
Alas, with the update to iOS SDK version 5, this no longer works. And in fact, if you spelunk through the UIViewController.h file, it does say that as of 5.0, it will no longer return the presenting view controller.
Luckily, you can just do this instead:
[self dismissModalViewControllerAnimated:YES];
BTW, happy birthday to Roger Moore, star of the excellent films “The Cannonball Run” and “ffolkes”.
September 2, 2011, 8:48 am
Are you a frustrated iOS developer working on an app that has to access the built-in address book on the device? Frustrated because you can’t find sample data to populate the address book in your Simulator, and you are too busy to create a bunch of test data?
Well, for a limited time* only, we at the Do Something Here Labs have a deal for you. For just a minimal** postage and handling charge, you too can use this sample code below to create random entries to your hearts content in your very own iOS app.
Here is the code in question. Simply drop this method into your Objective-C source code and call it in your app, and you will soon have contacts, contacts, and more contacts.
#import <AddressBook/AddressBook.h>
- (void)addressBookData
{
#define NUMBER_OF_RANDOM_ENTRIES 25
#define GENDER_OF_NAMES @"m" // use m for male or f for female
#define URL_FORMAT @"http://old.wave39.com/roster/generate.php?f=csv&g=%@&num=%d&limit=99"
NSString *urlString = [NSString stringWithFormat:URL_FORMAT, GENDER_OF_NAMES, NUMBER_OF_RANDOM_ENTRIES];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSString *stringData = [NSString stringWithUTF8String:[data bytes]];
NSString *areaCode = [NSString stringWithFormat:@"%03d", (arc4random() % 799 + 200)];
NSArray *lineArray = [stringData componentsSeparatedByString:@"\n"];
for (NSString *line in lineArray)
{
if ([line length] > 0)
{
NSArray *fieldArray = [line componentsSeparatedByString:@","];
if ([fieldArray count] == 3)
{
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();
NSString *phone = [NSString stringWithFormat:@"%@-%03d-%04d", areaCode,
(arc4random() % 799 + 200), (arc4random() % 9999)];
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
CFStringRef phoneType = (arc4random() % 2 == 0 ? kABPersonPhoneMainLabel : kABPersonPhoneMobileLabel);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, phone, phoneType, NULL);
ABRecordSetValue(person, kABPersonFirstNameProperty, [fieldArray objectAtIndex:1], nil);
ABRecordSetValue(person, kABPersonLastNameProperty, [fieldArray objectAtIndex:2], nil);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
ABAddressBookAddRecord(addressBook, person, nil);
ABAddressBookSave(addressBook, nil);
NSLog(@"Created record for %@ %@ (%@: %@)", [fieldArray objectAtIndex:1],
[fieldArray objectAtIndex:2], phoneType, phone);
CFRelease(phoneNumberMultiValue);
CFRelease(person);
CFRelease(addressBook);
}
}
}
NSLog(@"Done creating address book data.");
}
On a housekeeping note, make sure to include the AddressBook.framework library in your application, and make sure to import the AddressBook.h header file as shown. Although if you are already developing an app that accesses the address book, you probably have this covered.
Also, your device or computer needs to have internet access while the code is running, as the code accesses a simple PHP application that I did a while back to do random sports roster generation. (The PHP application will generate a maximum of 99 entries at a time, so if you want to try and have it generate 42 billion names with one call, go right on ahead and try.) OK, I probably should not be blocking the main thread with the synchronous request, but I did not have time do something other than the most quick and dirty solution possible.
But wait, there’s more.
Happy birthday to Terry Bradshaw. I’m sure that if Terry was developing an iOS app, and he needed to populate some random address book entries, this would be the code he would use. And if it is good enough for Terry, isn’t it good enough for everyone?
(* = For as long as this web site is accessible on the internet)
(** = $0.00)
September 1, 2011, 10:38 am
Maybe someone at Apple was reading my blog yesterday, because a couple of hours after I posted my lamentations over the BvP Lite approval wait, the app went into the approval process and was approved then an hour or so after that. Awesome job Apple, thanks.
Here is the link to the Batter vs. Pitcher Lite free app:
Batter vs. Pitcher Lite
And if you need to be able to analyze more than one batter and/or pitcher at a time, you would need to get the non-free Batter vs. Pitcher app:
Batter vs. Pitcher
No birthday, anniversary, or holiday wishes at this time, but I would like to say that I am totally impressed with my new 13″ MacBook Air. It seems to be more than adequate for building iOS apps, and due to the SSD drive, it actually seems to compile, build, and fire up the simulator much faster than my old MacBook Pro.
August 31, 2011, 3:21 pm
Well, I took the version 1.0 BvP app and turned this 1 batter vs. 1 pitcher app into a free app, and submitted this new app to iTunes Connect. They seem to be dragging their feet on this new app, as it has been over a week. App revisions seem to go much faster, it only took a few days when I submitted the 2.0 BvP app, which features the ability to have multiple pitchers vs. multiple batters. This is a much more useful mode for fantasy baseball or for statistical research.
BTW, Happy Birthday to John Boker. (Not the John Boker who played baseball for the Ogden Raptors, but “the” John Boker who was formerly a track star and also happens to be Ohio’s pre-eminent bacon tea connoisseur.)
August 10, 2011, 5:48 pm
So I have a table view that is built by using a custom cell that I built in Interface Builder and load up in my view controller. This UITableViewCell has a bunch of UIButton controls right off the root of the cell view in it that I need to snag the action on, and of course there can be a lot of cells in the table. In order to do this as compactly as possible, I wanted to just create one IBAction method, and funnel all button presses to that method. Then, inside the method, I would just read the tag of the sender to decide what button was pressed, and then back up one level to get the cell, ask the table for the index path of that cell, and then I would know which row to act on.
Well of course it did not work as expected the first time. For some reason, when I asked the table for the index path of the cell, it would always return a nil index path. So here is the final code that works.
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *path = [contactsTable indexPathForCell:cell];
As I studied the problem, I discovered that the first superview off the sender does not get you far enough back, you have to go back another superview.
BTW, thanks to Leo Fender, born this day in 1909, for founding a company with such awesome products.