How to extract first n characters of an NSString

We can use substringWithRange for this, which requires us to define a range: NSString *something = @”123456789″; NSRange myRange = NSMakeRange(2, 3); something = [something substringWithRange:myRange]; // something is now 345 You create a range by specifying where the substring should start (the first character is 0, like in an array), then specify how many […]

How to create your own Data Type using typedef

With typedef you can specify custom variable types. Instead of having a variable such as int, you could create your own – such as yourInt: // create a typedef of int typedef int yourInt; yourInt yourValue = 57; NSLog(@”Your value is %i”, yourValue); Now you have your own variable type called youInt which behaves just […]

How to create an Enumeration (enum)

An Enumeration (enum) allows you to create a collection of custom values. These can be more meaningful than simply using numbers. Imagine you want to track drink sizes, such as small, medium and large. You can remember to just use the numbers 1, 2 and 3 for each size – but Enumeration makes it easier […]

How to create an NSIndexPath and specify its components

On occasion we need to create an NSIndexPath manually, with components we specify (such as a row or a section). There’s a method for that: indexPath:forItem:inSection. Here’s how you create an indexPath for row 0, section 0: NSIndexPath *indexPath = [[NSIndexPath alloc]init]; indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; The method takes two integers. You can even […]

Hilarious Xcode Error Message

Xcode_iconI’ve been working on an iOS App recently which deals with several date methods. Usually when something goes wrong Xcode displays very dry messages such as “Array out of bounds” or something rather unhelpful (like the complete stack output with no clue as to what actually went wrong).

I was accidentally passing nil into an NSDateComponents method – and instead of crashing (which is what I would have expected), Xcode displayed this super funny message in the log console:

[__NSCFCalendar components:fromDate:toDate:options:]:

toDate cannot be nil

I mean really, what do you think that operation is supposed to mean with a nil toDate?

An exception has been avoided for now. A few of these errors are going to be reported with this complaint, then further violations will simply silently do whatever random thing results from the nil.

It made me smile 🙂

Read more