A while ago I wrote on my blog about a solution to one of the most common questions asked by Objective-C programmers which is “How can I create string enumerations?”. Well, the solution that I’ve given has immediately become one of the top subjects that attracts developers to my blog, as I can see in my stats. I thought I should now take it to a whole other level and get rid of the limitations that I had presented in the old solution, and come up with a fresh perspective.
The following video is the result of my work on this subject. I hope you’ll enjoy watching it.
As you know, my recent book is now published, titled “iOS 7 Programming Cookbook”. You can purchase it here:
All the source codes written for this book are now available on Github at the following location:
If you have any questions, please let me know.
So you have written an Objective-C class and you would like to mark it as unavailable so that others won’t be able to instantiate it?
The solution is simple. In the header file of your class, place the following code:
__attribute__((unavailable("Your message to the developer goes here")))
So here is an example of a class header file that uses this LLVM extension:
__attribute__((unavailable(“This class is unavailable. Please use the YourClass class instead”)))
@interface MyClass : NSObject
Once a programmer attempts to use this class, they will see something similar to this in Xcode (Click to Enlarge the photo):
Click to Enlarge
In this video, I will teach you a really cool way of filtering through an NSArray, using predicates.
Note: I have written a newer and better solution to this problem in a new video, which you can watch by clicking here.
Sooo a lot of programmers think enumeration items cannot be strings, and they are right, BUT, there is a BUT. You have to understand that C strings that are made out of 4 characters, each of which is 1 byte long, constitute a memory address that is 4 bytes long, or just the equivalent of int, NSInteger or whatever you want to call it.
So if you put a value such as ‘Good’ for the enumeration item, in fact, the compiler will translate the values of ‘G’, ‘o’, ‘o’ and ‘d’ as their numerical values and will generate a hexadecimal value and put it as the integral value of the enumeration item.
Let’s say we want to say “Good”, or “Nice” all in enumeration items in C. Here is the example, this code runs fine in Objective-C for iOS or OS X as well:
The output of this is the string “dooG” printed to the screen if you run this on an iPhone device for instance. The reason is the bytes are obviously reversed as the string ‘Good’ has the letter ‘d’ as the lowest byte and that ends up being at the first byte of the string so we end up with “dooG”. That’s not good though, is it? So we just have to swap the bytes around to get the proper string:
Easy peasy, aye? The highlighted line is very important. That line swaps the order of bytes in the integer. Also note that I am using the calloc function as it will not only allocate the memory but also set the byte values to 0x00 in the memory for us so we don’t have to do it manually or with another procedure.