Your first foray into iPhone programming, and you seem to be confused by the syntax. That’s fine, Objective-C can be intimidating at times, and it’s surely more so for beginners! Don’t fret, I’ll explain what your basic data types will do here, in this post. It’s very important that you know these things, as they’re the root for every app’s function. Let’s get started, shall we?
Strings? What are those?
Strings, called NSStrings in Xcode, are objects that hold alphanumeric characters and stores them in use for various elements, such as labels and text boxes. This is the simplest data type, as it’s just plug and play. This is the basic syntax for creating a string in a class:
|
1 2 |
NSString *string = [[NSString alloc] init]; string = @"Hello world!"; |
The first line of code tells us to create the object and store it in the memory for use later. The second line is telling the app to assign our choice of words and numbers to the value to use in our choice of objects and elements. This is normally used to apply itself to a label, add to an array or dictionary, and insert into a text box. You can simply skip the allocation line and just assign the variable an alphanumeric string if you’d like, as well.
Integers? Floats? Doubles? This is mind numbing!
The difference between an integer, float, and double can be quite head scratching, but this is why I’m here: to explain this as simply as possible. Integers are your basic, non decimal numbers. They’re used for booleans, switches, case statements, and things that don’t require any level of precision to run.
There are different types of integers, “short” being the least capacitive, “int” carrying a standard capacity, “long” carries a larger capacity, and “long long” has the highest capacity of the integer family. Floats are precision decimal numbers. On top of calculating real numbers, they have precision for up to 7 digits to the right of the decimal point. This is useful for apps that require some form of precision, but not extremely accurate precision.
Doubles are more precise, in the sense that they can calculate 15 digits to the right of the decimal point. This is useful for stuff like cryptologic calculations and almost-precise GPS points on a map. There are also long doubles, which use a 64-bit space to calculate the values. More information on these types can be found by referring to the Wikipedia page for double-precision floating point here. Always remember to make use of as many 32-bit space numbers as you can, as it becomes non-precise on Apple A6 and older processors.
Arrays? Dictionaries? What’s the difference?
Arrays (called NSArrays) are an index of objects that you save to it directly. Arrays can be made and added to by doing so:
|
1 2 3 4 5 |
NSString *string = @"Test String"; NSString *anotherString = @"Another Test String"; NSMutableArray *array = [[NSMutableArray alloc] init]; [array addObject:string]; [array addObject:anotherString]; |
In this example, we made two strings, and added them to the newly generated array. But wait Darran, what does the “mutable” bit in the code mean? Glad you asked. The mutable trait in NSDictionary and NSArray means you can make changes to the array as you see fit. It’s not static, it’s dynamic. Which means objects can be added on the fly. A regular array does not have the “addObject:” method on it, so therefore it’s only useful for saving data to. So now that you’ve saved your string to the array, how can we get the string from it for another time? Here’s how:
|
1 2 3 |
NSString *resultString; resultString = [array objectAtIndex:0]; NSLog(resultString); |
Here, we made another string to store our fetched value, which in this case would be the first string we added, “Test String”. The NSLog puts the result out in Xcode’s console for you to see. NSLog is very useful for tackling issues that you might not know the root of. The “objectAtIndex:” portion requires an integer to pull from.
If you overstep the boundaries of the array, it will throw an exception and crash your app, so always keep in mind how many objects are in your array. This can be easily accessed by NSLogging “[array count];” and seeing how many objects are inside. Always remember that zero is the first object in the array, one is the second, and so on and so forth. Now onto making a dictionary (known as NSDictionary):
|
1 2 3 4 5 |
NSString *keyStringOne = @"A test value"; NSString *keyStringTwo = @"Another test value"; NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:keyStringOne forKey:@"first"]; [dict setObject:keyStringTwo forKey:@"second"]; |
Here, we created two strings, just like we did for the array above. And we created a mutable dictionary to add the objects to. You can see how we added the objects to the dictionary in the last two lines, but this is what separates dictionaries and arrays from each other: dictionaries use Key-Value Coding, which means you use the string you set as the key to pull a value from the dictionary. So to pull a value from the dictionary, it’s a little different:
|
1 2 3 |
NSString *resultDictionary; resultDictionary = [dict objectForKey:@"first"]; NSLog(resultDictionary); |
Here, we called the “first” key to get the value from the dictionary, which is “A test value”. Then, just like the NSArray, we logged it into the console to see what we called. Many of your REST APIs use JSON, which is KVC compliant. We’ll cover that down the road, but dictionaries and arrays are vital to the function of an online-integrated application. Please note that in a dictionary, you should always send the key’s value to it’s proper data type, be it an integer, float, or string.
Conclusion
These are basic data types that you should know when making your application. They are vital to data storage and management of information in the app. They’re also useful for displaying things in an app that you have pre-generated or pulled from an online source. Later on, we’ll teach you how to make applications using this information and how to properly structure the application to maximize it’s efficiency when doing these things. Thanks for coming along, and stay tuned for more tutorials from myself here.

Thanks for mhs.