Basic For Programming
Are you extremely new to programing, don't need to be worry just need to know these basic terms to start learning any programimg language .
var x = 15;var name = John.getName();for (var x = 0; x < 10; x++)if (x == 20)var things = array("dog", "cat");
Now let's talk about Objective-C.
Introduction To Objective-C
Objective-C is the language that most Mac and iOS developers use to write native applications. There are alternative ways to write Mac and iOS apps .
If you're looking to build mobile websites or web apps then you don't need to learn Objective-C or Cocoa and probably don't need to go any farther in this tutorial. If you're looking to learn a new programming language and build native apps for the Mac and iOS, then this is the place for you!
- Objective-C is a superset of C programming language, which makes C object oriented.
- It was developed by Brad J. Cox at Stepstone Corporation in early 80’s, by adding Smalltalk-80 extensions to C-language.
- It was originally the main language on NextStep OS which was further adopted by Apple.
- It is used by Apple as a primary language to develop applications for MAC system and iPhone.
Objective-C, C and Smalltalk
- Objective-C is an object oriented programming language that combines features of both, C and Smalltalk.
- Objective-C extends the procedural C-language with the addition of some new keywords and compiler directives, and uses Smalltalk style for sending messages to objects.
- The extension of C language into Objective-C is made possible by a runtime library (libobjc) that is generally transparent to the developer.
- It is possible to compile any C program with Objective-C compiler and therefore, code written in C-language can be freely included in any Objective-C class.
- Syntaxes of all non-object oriented operations in Objective-C are identical to that of C-language and object oriented operations are the implementation of Smalltalk style messaging.
- Features of Objective-C
Being a derivative of C and Smalltalk, Objective-C comprises many features:
- It is an object-oriented version of C-language.It is a powerful language which is easy to learn and sophisticated to work with.
- Better understandability of code.
- Programmer can use both object oriented and procedural programming strategies as per the requirements.
- It provides open dynamic binding via Objective-C runtime library which makes it true object oriented.
- It also uses protocols for implementing multiple-inheritance.
Objective-C: Programming fundamentals:
To write an Objective-C program, it requires basic knowledge of programming in C-language and object oriented concepts. As Objective-C is primarily used by Apple, providing its own developer tool (Xcode) for developing MAC and iPhone applications, a developer can also write programs on Terminal window by using the GNU Objective-C compiler (gcc).
Data Types is Objective C :
Data type specifies the type of data a variable holds. In Objective-C, the primitive data types are same as that of C-language and some new data types are also included in its library that are as follows:
Type Description
- char A character of the local character set
- int An integer (whole numbers), e.g. 1, 2, …
- float Floating point number, e.g. 1.2, 1.7, 5.8, …
- short A short integer
- long A double precision of short
- double A double precision of float
- signed Specifies a integer value either +ve or –ve
- unsigned Specifies only +ve integer values
- Bool Used for conditions, either True(1)/False(0)
- id Used to store reference to an object
Identifiers:
An identifier is any combination of alphanumeric characters that specifies a variable, type, function or class in the program.
An identifier must start with alphabet and may include underscore and digits but with limited length, for example,
int a=5;
Here a is an integer type variable, whose identifier is a.
Note that a keyword can never be used as an identifier.
Literals:
Literals are constant values that are stored in variables.
There are 5 types of literal constants that are generally used while programming namely, Integer, Floating-point, Characters, String and Boolean values, for example, when we wrote:
a = 3;
3 is the literal constant in this statement.
Integer literals: Numerical constants that identify integer values are called integer literals, for example,
512 : Decimal value
023 : Octal value
0×2 : Hexadecimal value
Floating-point literals: Numerical constants that identify decimal and exponential values are called floating-point literals, for example,
4.5
4.5e12 : 4.5 x 1012
Characters literals: Non-numeric constants with single characters within single quotes are called character literals, for example,
‘x’
String literals: Non-numeric constants with collection of characters within double quotes are called string literals, for example,
“hello! This is a string literal”
Boolean literals: There are only two values: true and false, and these are represented as values via type bool by literals true and false.
Constants:
Constants are values that never change. Other than using literal constants while programming, we can also declare or define constants as per our requirements.
Declaring constants
Constant can be declared by using keyword- const, as prefix with the type of variable as follows:
const int aAsconstantinteger = 10;
const float bAsconstantfloat = 5.2;
Now, the values of these two variables cannot be modified as they are declared as constants.
Defining constants
Constants can also be defined by the developer with a desired name via using #define preprocessor directive as follows:
#define pi 3.14
Now, this constant pi can be used in the code directly.
Common Objects & Functions
Regardless of what your app looks like there are some objects, functions and data structures that you'll use over and over so it's important to become familiar with them.
NSString
A string is a sequence of characters, and in Objective-C, a string is an
NSString object with a lot of built-in functionality. The shorthand way of referring to a string @"looks like this" with an @ sign in front of a double-quoted run of characters. NSStringobjects can be compared to one another, converted to other encoding formats, used to hold the contents of a file or URL, changed to C-style strings, used in regular expression matches or turned into numbers.
Once you create a string it cannot be modified. This may sound odd if you're coming from PHP or JavaScript, but if you want to modify a string after it has been created, you should use
NSMutableString instead, a mutable (modifiable) subclass of NSString.
Here's an example of how you might construct an
NSURL object by using a string:NSURL *url = [NSURL URLWithString:@"http://google.com/"];
And here's an example of using a mutable string:
NSMutableString *myString = [NSMutableString stringWithString:@"Reading"]; [myString replaceOccurrencesOfString:@"Read" withString:@"Writ" options:NSLiteralSearch range:NSMakeRange(0, [myString length])];
The
NSMutableString instance method -replaceOccurrencesOfString:
withString:options:range: finds a string within a string, then replaces it with something else. Notice the call to the NSMakeRange() function, part of the Foundation framework. It returns anNSRange struct data type which has two members: location and length.
NSArray
An array is an ordered collection of things and if you're using
NSArray, those "things" all have to be objects. Just like with an NSString, once it's created it cannot be modified unless you useNSMutableArray instead. Here's an example of making an array:NSArray *myArray = [NSArray arrayWithObjects:@"Fish", @"Dogs", nil];
When creating an
NSArray using the +arrayWithObjects: class method, the list of objects needs to be nil-terminated or it won't compile. It's not very intuitive at first, but it's important to remember to add it at the end of your list of objects or your code will crash and burn.
Organizing objects into an array is useless if you never do anything with your new collection. Here are some examples of what you can do with a filled array.
id myObject = [myArray objectAtIndex:3]; [myArray makeObjectsPerformSelector:@selector(runTowardsTheMoon)]; NSUInteger howMany = [myArray count];
The
-objectAtIndex: instance method returns an id, a generic object in Cocoa. Since you can hold any type of object you want in an NSArray, when you access an object it gives you back a generic object type. It doesn't know what types of objects are being stored. Also, in the second example, what's a @selector? A @selector in Objective-C is basically a method signature. In this example, we're telling each object in the array to call their -runTowardsTheMoon instance method. In the third example the method returns anNSUInteger, a Foundation framework primitive type that's an unsigned integer, that is, an integer that's greater than zero.
NSDictionary
Almost all languages have the concept of a data structure that holds key-value pairs. Perl and Ruby have hashes, Python has dictionaries, PHP has associative arrays and Objective-C has the
NSDictionary class. The Foundation framework provides us with NSDictionary(and its mutable brother NSMutableDictionary) to hold key-value pairs where all keys and values have to be objects. Let's define some dictionaries:NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:@"aObj", @"aKey", @"bObj", @"bKey", nil]; NSArray *objs = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil]; NSArray *keys = [NSArray arrayWithObjects:@"Blue", @"Green", @"Yellow", nil]; NSDictionary *anotherDict = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
In the first example we're calling the class method
+dictionaryWithObjectsAndKeys:which has to be nil-terminated just like with some NSArray methods. In our object listing at the end we alternate object, key, object, key until we're done with all the pairs.
In the second example we're creating two different
NSArray objects — one to hold the keys and another to hold the objects — and then we pass these two arrays into theNSDictionary class method +dictionaryWithObjects:forKeys: to build our dictionary. Again, notice that we're not using +alloc or -init to manually allocate memory for these dictionaries. This means we don't have to worry about their memory or calling -releasewhen we're done using them.
A common use of
NSDictionary is within an array: each element in the array is anNSDictionary object. For example, if you're building a Twitter app, the information for each tweet could sit in an NSDictionary, then each of these dictionaries is kept in order within an NSArray.// First, find the dictionary in the 4th position in the array. // Then, access the object paired with the key "tweet_text" NSString *status = [[myArray objectAtIndex:4] objectForKey:@"tweet_text"];
Organizing
NSDictionarys within an NSArray is a simple way to store some structured data.
NSNumber
If you want to store a primitive number in an
NSArray or NSDictionary you'll have to package it up into an NSNumber object first. NSNumber has a number (ha!) of convenient class methods so you'll rarely need to manage the memory yourself.NSNumber *myNumber = [NSNumber numberWithInt:326]; int myInt = [myNumber intValue];
You can get the primitive value of an
NSNumber object just as easily as you can store it.
NSLog()
NSLog() is a C function to send output to the console, useful when debugging your application.NSArray *myArray = [NSArray arrayWithObjects:@"Yo", @"Hey", nil]; NSLog( @"My array: %@", myArray ); int myInt = 2011; int anotherInt = 2012; NSLog( @"My int: %d and another int: %d", myInt, anotherInt );
Writing static text out to your log isn't that useful, so
NSLog() lets you output variables as well. To include a variable in your output, you need to know what type of variable it is because NSLog() has different format specifiers based on the variable type. For example, an array is an object so you'd use %@. The specifier for a plain int is %d. These format specifiers are "stand-ins" for the actual value of the variable listed at the end of the function call. Apple provides the full list of string format specifiers to be used in NSLog()so make sure to always use the right one.