C is a statically typed language. This means that variables must be declared with a type and cannot change through the lifetime of the program. In a dynamicly typed language such as Ruby or Javascript, variables can hold data of any type over their lifetime. For example, in Javascript:
> var dyanmicVar; undefined > dynamicVar = "a string"; 'a string' > typeof dynamicVar; 'string' > dynamicVar = 2; 2 > typeof dynamicVar; 'number'
Now lets try the same thing in C
NSString *staticVar = @"a string"; staticVar = 2;
This should generate a compilre error Implicit conversion of
'int' to 'NSString *' is disallowed with ARC. The first
major difference here is that there is a program called the
compiler that is attempting to enforce type correctness. While
this can be irritating at first, static typing can also help catch
errors in your programs. For example, in the Javascript program
above, what if some part of the program was expecing
dynamicVar to be a string. You wouldn't know there was an issue until
actually executing the program. And then, you would have to back up
to the location where dynamicVar was assigned a non-string.
Dynamic vs. static typing is one of those issues in programming language design that people feel strongly about. Ultimatley we don't have a choice since Objective-C was chosen for us, so lets learn to take advantage of the warnings and errors given by the compiler.
short, int, long, NSInteger
short holds smaller numbers
than int, which is smaller
than long. NSInteger is a type provided
by Apple that is an integer of the ideal size for the current
platform, be it iOS or OS X.
float, double, CGFloatdouble is capable of storing higher
precision numbers than float, but uses up double the
storage space. CGFloat is the decimal type used by the iOS Core
Graphics framework.
NSDate *today = [NSDate date];struct
typedef struct {
float latitude;
float longitude;
int visits;
} Location;
Elements of a struct are accessed using dot notation.
Location theFactory; theFactory.latitude = 42.963107; theFactory.longitude = -85.669407; theFactory.visits = 42;Structs are used frequently in iOS development. Try looking up NSRect in the XCode documentation. NSRects are used to represent the location of a user interface element
BOOLYES
or NO.
That's all there is. With nothing but numbers, words, and collections of the two, we can describe most any concept you want.
Before we can use a variable to store data, it must be delcared with both a name and a type.
int anInteger; float aDecimal; NSString *aString; // "string" is programmer talk for text.
Unlike int and float which are built-in C
types, NSString is actually a class specific to
Objective-C. The variable aString will hold a pointer
to an instance of NSString. If you are unfamiliar with
words like class, object, and instance, don't worry too much now, we
will cover them in detail in the next section. For now, just know
that if you need a variable to hold some text, you delcare it of
type NSString *.
Now that we have some variables, we should store data in them. A single
equal sign = is used to store the value on the right side
into the variable on the left side.
int anInteger; float aDecimal; NSString *aString; // "string" is programmer talk for text. anInteger = 5; aDecimal = 2.7182; aString = @“The Deliverator belongs to an elite order, a hallowed subcategory.”;
This example both declares several variables, and then assigns values to them.
It is possible to combine both the declaration and assignment of a variable into the same line.
int anInteger = 5; float aDecimal = 2.7182; NSString *aString = @“The Deliverator belongs to an elite order, a hallowed subcategory.”;