Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Saturday, September 28, 2013

Simple iOS NSDate Format Example

So, you want to convert an NSDate to NSString, eh?

No problem. With the help of our trusty NSDateFormatter class, we can turn any date into a string with a few keystrokes.

// Get today's date
NSDate *myDate = [NSDate date];
// Create an iOS date formatter and set the pattern
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// Convert the NSDate to NSString
NSString *dateString = [formatter stringFromDate:myDate];

Wow, that was easy! This is a fun trick that comes in handy all the time, especially when you want to display a nicely formatted timestamp, or when you want to send date information over the wire to your API endpoints.

Read More

Friday, September 27, 2013

Compound Predicates (Multiple Where Clause Params) in iOS Core Data

So, you want to select an entity row from an iOS Core Data table using multiple where-clause-like parameters, eh?

"Predicates" (don't you love it when languages and platforms abstract their terminology away to the extreme?)... predicates are basically parameter equivalents in a WHERE clause.

select * from users where is_profitable_customer = 1

That statement in yellow there, that's a "predicate" {to be said in a snooty tone}. And this:
select * from users where is_profitable_customer = 1 AND calls_me_at_3am = 0

...well, that's what is referred to as a "compound predicate".

Ok, enough of this... lets see some code. If I want to select from a Core Data table with multiple where-clause parameters, I do this:

// Dim up a fetch request for our entity: UserModel, 
// which lets just assume exists
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"UserModel"];

// Our first predicate is gonna help us find the 
// customers that make us money
NSPredicate *profitableCustomerPredicate = [NSPredicate predicateWithFormat:@"is_profitable_customer = %d", 1];

// Our second predicate is gonna filter out the customers 
// that make life difficult.
NSPredicate *annoyingCustomerFilterPredicate = [NSPredicate predicateWithFormat:@"calls_me_at_3am = %d", 0];
// And we're going to make "Predicate Soup" combining 
// them into one WHERE statement
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubPredicates:@[profitableCustomerPredicate, annoyingCustomerFilterPredicate]];
// And assign this NSCompoundPredicate (which is just 
// an NSPredicate after all) to the request
request.predicate = compoundPredicate;

// Sigh... sort descriptors, Core Data won't function 
// without them. I call that a FLAW in Core Data!
NSSortDescriptor *sortDescr = [NSSortDescriptor sortDescriptorWithKey:@"is_profitable_customer" ascending:YES];

request.sortDescriptors = [NSArray arrayWithObject:sortDescr];

// Lets go fishing! Fetch that data... gives us back 
// an array of our Entities.
NSArray *matches = [context executeFetchRequest:request error:&error];

And there you have it. 

BEST PRACTICE ALERT! Of course, you can use single predicates, but why not just always use compound ones so they're already coded up the way you need them when you're ready to add more statements to your where clause. Do yourself this favor at least.

Read More

Thursday, September 26, 2013

Programmatically Setting Font-Size In iOS

So, your heart's desire is to adjust the font-size of a label or button programmatically in iOS, eh?

Lets say your design calls for a fixed-size box into which you should place some content. Naturally, you reach for a UILabel, but then you realize that you need to accommodate varying amounts of text inside of it. So, you start reaching for a UIWebView, but then you're like, "Hmmm ... setting the font-size of this thing programmatically shouldn't be too hard... lets ask the Googles."

Well, friend, the Googles have lead you to the right corner of the web. For here is the answer you seek (probably):

Now, first things first, I want to do things all right and proper, so I define my font-sizes as constants in my header file (you should too!):

#define font_size_for_num_digits_1 100.0f
#define font_size_for_num_digits_2 80.0f
#define font_size_for_num_digits_3 60.0f
#define font_size_for_num_digits_4 40.0f
#define font_size_for_num_digits_5_and_up 20.0f

Now, I want to create a nice method for determining which font-size I should use in various conditions. In my case, my field contains numbers, and while it would be just as easy to make this method handle it as strings, I'll leave the exercise to the reader. But, if you're really hurting, reach out to me in the comments and I'll help you. Anyways:

  int val = 0.0f;
  // "Egads, if-else statements offend my OOP Spidey-Senses"
  // Well, mine too... but iOS doesn't like relative comparisons in their 
  // switch statements, so what's a guy to do?
  if (num < 10){
    val = font_size_for_digits_1;
  } else if (num >= 10 && num < 100){
    val = font_size_for_digits_2;
  } else if (num >= 100 && num < 1000){
    val = font_size_for_digits_3;
  } else if (num >= 1000 && num < 10000){
    val = font_size_for_digits_4;
  } else if (num >= 10000 && num < 100000){
    val = font_size_for_digits_5;
  return val;

So, somewhere later in my code, say in a setter for my label (or buttons) text value, I do thusly:

float size = [BigNumberCell getFontSizeForNumber:num];
[[self myLabel] setFont:[UIFont systemFontOfSize:font_size]];

There's an important note to make here before you haul off uninformed like so many people before you... this right here: [UIFont systemFontOfSize:font_size] has the awesome effect of PRESERVING YOUR CURRENT FONT... so that you don't need to monkey with fontWithName.

Did you hear that last part? {sigh} You're probably gone already and you'll either be confused or you'll take it for granted.

If you're still here, good on ya, see you next time on the Googles!

Read More

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates