Crash Course in Objective-C

download Crash Course in Objective-C

of 40

  • date post

    22-Apr-2015
  • Category

    Education

  • view

    1.525
  • download

    1

Embed Size (px)

description

A crash course in Objective-C.

Transcript of Crash Course in Objective-C

  • 1. Computer Science Large Practical: Crash Course in Objective-C Stephen Gilmore School of Informatics Friday 12th October, 2012Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 1 / 33
  • 2. Acknowledgements We are following Appendix C of Beginning iOS 5 Application Development Wei-Meng Lee John Wiley & Sons, Inc. 2012 www.it-ebooks.infoStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 2 / 33
  • 3. Objective-C sources Objective-C source code les are contained in two types of les: .h header les .m implementation les Thus a project could contain a le called SomeClass.hStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 3 / 33
  • 4. Directives If you observe the content of the SomeClass.h le, you will notice that at the top of the le is typically an #import statement: #import @interface SomeClass : NSObject { } @end The #import statement is known as a preprocessor directive. (NS stands for NeXTStep, the project which created Objective-C.)Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 4 / 33
  • 5. Importing your own header les To import a header le from within your project, you use the quote characters, as in the case of the SomeClass.m le: #import "SomeClass.h" @implementation SomeClass @endStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 5 / 33
  • 6. Classes To declare a class, you use the @interface compiler directive, like this: @interface SomeClass : NSObject { } This is done in the header le (.h), and the class declaration contains no implementation. The preceding code declares a class named SomeClass, and this class inherits from the base class named NSObject.Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 6 / 33
  • 7. Classes To implement a class declared in the header le, you use the @implementation compiler directive, like this: #import "SomeClass.h" @implementation SomeClass @end This is done in a separate le from the header le. In Objective-C, you dene your class in an .m le. Note Note that the class denition ends with the @end compiler directive.Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 7 / 33
  • 8. Mutual recursion If your class references another class dened in another le, you need to import the header le of that le before you can use it. To prevent circular inclusion, Objective-C uses the @class compiler directive as a forward declaration to inform the compiler that the class you specied is a valid class. You usually use the @class compiler directive in the header le; and in the implementation le, you can use the @import compiler directive to tell the compiler more about the content of the class you are using.Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 8 / 33
  • 9. Mutual recursion example // SomeClass.h #import @class AnotherClass; // forward declaration @interface SomeClass : NSObject { // an object from AnotherClass AnotherClass *anotherClass; } @endStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 9 / 33
  • 10. Mutual recursion example // SomeClass.h #import @class AnotherClass; // forward declaration @interface SomeClass : NSObject { // an object from AnotherClass AnotherClass *anotherClass; } @end // AnotherClass.h #import @class SomeClass; // forward declaration @interface AnotherClass : NSObject { SomeClass *someClass; // using an instance of SomeClass } @endStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 9 / 33
  • 11. Class instantiation To create an instance of a class, you typically use the alloc keyword to allocate memory for the object and then return it to a variable of the class type: SomeClass *someClass = [SomeClass alloc]; In Objective-C, you need to prex an object name with the * character when you declare an object.Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 10 / 33
  • 12. Primitive types If you are declaring a variable of primitive type (such as oat, int, CGRect, NSInteger, and so on), the * character is not required. Here are some examples: CGRect frame; // CGRect is a structure int number; // int is a primitive type NSString *str; // NSString is a classStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 11 / 33
  • 13. The id type Besides specifying the returning class type, you can also use the id type, like this: id someClass = [SomeClass alloc]; id str; The id type means that the variable can refer to any type of object; hence, the * is implied.Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 12 / 33
  • 14. Fields Fields are the data members of objects. For example, the following code shows that SomeClass has three elds anotherClass, rate, and name: // SomeClass.h #import @class AnotherClass; // forward declaration @interface SomeClass : NSObject { // an object from AnotherClass AnotherClass *anotherClass; float rate; NSString *name; } @endStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 13 / 33
  • 15. Access Privileges By default, the access privilege of all elds is @protected. However, the access privilege can also be @public or @private. The following list describes the various access privileges: @private Visible only to the class that declares it @public Visible to all classes @protected Visible to the class that declares it and inheriting classesStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 14 / 33
  • 16. Public elds To make the rate and name visible outside the class, modify the SomeClass.h le by adding the @public compiler directive: #import @class AnotherClass; // forward declaration @interface SomeClass : NSObject { AnotherClass *anotherClass; @public float rate; @public NSString *name; } @endStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 15 / 33
  • 17. Public elds We can now access the elds rate and name directly (using the -> operator). SomeClass *someClass = [SomeClass alloc]; someClass->rate = 5; // rate is declared public someClass->name = @"Wei-Meng Lee"; // name is public Although we can access the elds directly, doing so goes against the design principles of object-oriented programmings rule of encapsulation. A better way is to encapsulate the two elds we want to expose in properties, as we will see later.Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 12th October, 2012 16 / 33
  • 18.