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)

Thursday, October 31, 2013

NSInteger to int

So, you want to convert an NSInteger to an int do you?

Somewhere there is a developer who is at the end of their tolerance for the querks of a language or an SDK. Somewhere, someone is begging for consistency in converting objects to primitives. We have NSStrings that have built-in methods for converting to primitives, so what the heck is wrong with my NSInteger class? There's no [NSInteger intValue] method!

Well, the answer is:

  NSInteger *currentPage = 42;
  int myInt = currentPage;

... apparently not that complicated.

{Sigh} For the purpose of calculations, you can treat NSIntegers as being barely anything more than 32 or 64 bit integers in disguise. 

Now go,  and bring glory to the house of your kinsmen!


Read More

Wednesday, October 23, 2013

Clearing the iOS Notification Badge

So, you want to clear the iOS notification badge, eh?


Whether your backend app is pushing notification to your device (iPhone/iPad/Mac) or you're using a third party like Urban Airship to push them (smart decision probably since push notification certificates are a PITA), you'll want to clear your notifications badge when your app is opened by your user. In order to do this, you should setup a method called "clearNotificationBadge" in a utilities class somewhere.

I have a class called Utilities.h that I put useful functions like this in, and I suggest doing the same. Just put this method in your own Utilities.h file that you create for yourself:


+(void) clearNotificationBadge{
  //
  // The badge won't update unless it changes so we have to
  // set it to a different value first before we clear it
  // out. Like this...
  //
  [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
  [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

Then, just invoke your new method in the applicationDidBecomeActive method of your AppDelegate.m file like so:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
  // Restart any tasks that were paused (or not yet started)
  // while the application was inactive. If the application
  // was previously in the background, optionally refresh the
  // user interface.
  
  // Clear any notifications
  [Utilities clearNotificationBadge];
}

Boom, now you're cleaning up the messages like a pro. It feels dirty, but only because you're working with an API that seems buggy. What's really going on is that APPL is trying to be efficient on items that cross the sandbox barrier onto your device. They don't want to have to update that UI unless values are changing. So you have to do this set/reset song and dance. Its unfortunate, but easy to overcome.

Now go, and do great things worthy of honor! ;)


Read More

Tuesday, October 1, 2013

Handling Row Selection in UITableView


So, you want to handle table row selection in a UITableView, eh?

Oftentimes you want to do something interesting when a row is selected in your table view. Such interesting things include navigating to a detail screen or displaying a popup of some kind. Today we're going to show you an example of the former. But, hold on tight to your hat, because we're going to make some bold assumptions about the way you've architected your view. 

Our assumption is that you have a UIViewController set as the class for your view and you have already registered your UIViewController class as the delegate for the table. We're also going to assume you have a NSArray property that contains a Model messages. Further, our example is going to show you what it would look like to navigate to a detail message screen, as if you were creating a mail app.

Lets see some code, shall we?


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = self.storyboard;

    Message *message = [self.messageCollection objectAtIndexPath:indexPath];

    MessageViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"MessageViewController"];
    
    [viewController setMessage:message];
    
    [self.navigationController pushViewController:viewController animated:YES];
}

In the above example, we're handling row selection, getting the object that belongs to the row and segueing to another view controller. If that seems like a lot of concepts to bite off, you're probably right. I'll expand on this more in a full UITableViewController example soon.

Well for now, there you have a snippet of code to implement a useful bit of logic. Best of luck in your coding ventures.


Read More

iOS Adding an Object to a NSArray


So, you want to add an object to a NSArray, do ya?


// This code won't do what you want, don't use it (see below)
NSArray *myArray = [[NSArray alloc] init];
[myArray add...]; // well, crap.

Collections in iOS tend to be immutable. References of type NSArray & NSDictionary (and the like) are intended to be used to pass data around and store in memory, but not change them. In this way, they are well suited for modular code design because their references don't allow for monkeying with their contents when you pass them off. They are also useful in multi-threaded applications, where things can be removed from lists as their being iterated over, which is bad. Ultimately, they encourage good code design.
    
Want an array that you can manipulate? Well what you're looking for is:
    
NSMutableArray *myArray = [][NSMutableArray alloc] init];
[myArray addObject:@"This is a string!"]; // yay, now we can add stuff!



Read More

iOS Converting NSDictionary to NSData and Back

So, you want to convert a NSDictionary to NSData and back, eh?


There are plenty of occasions where you'd want to convert a NSDictionary to NSData. In my case, its typically when I want to create a binary-type attribute on an entity and store a JSON object that I fetched from an API. So its API => JSON => NSDictionary => NSData => Core Data Entity attribute. You can't store an NSDictionary into a binary attribute directly without jumping through some serialization hoops. Here's how to convert an NSDictionary to an NSData object:


NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];

In fact, if you don't convert your NSDictionary to NSData before you assign it to the attribute, you'll get an error like: "Unacceptable type of value for attribute: property = "data"; desired type = NSData; given type = __NSDictionaryM;"

Of course, later you're gonna want to get that NSDictionary back out of your persistent store. Now you're dealing with an NSData object in your attribute, and you need to do the following to get it back into NSDictionary form:


NSDictionary *myDictionary = (NSDictionary *) [NSKeyedUnarchiver unarchiveObjectWithData:myNSData];


There, now you're able to passivate complex data structures into your table without a bunch of extra columns. Super useful when you want to save differentt types of objects in the same table without overloading your entity with attributes.



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: http://www.blindnsdate.com

Popular Posts

Designed By Seo Blogger Templates