For Java developers starting iOS development, the hardest shift will probably be the Objective-C, the Apple’s language of choice. The Objective-C is extension of the standard ANSI C language, providing syntax for defining classes and methods, and other object oriented constructs. So any experience with C will help you a lot. In fact, since Objective-C is a superset of the ANSI version of the C, you’ll need to know the basics of C before learning Objective-C.
Unlike in Java, where you define and implement class in one file, in Objective-C you have two files. Same as in C, you have header files (.h) for public declarations and source files (.m) for implementation, which can hold both Objective-C and C code. You can even use C++ if you want (.mm files). The following example shows DeathStar class implemented in Java and Objective-C.
DeathStar.java
DeathStar.java
In Objective-C it would look like this:
DeathStar.h
DeathStar.m
Protocols
In the example we can see that DeathStar class extends SpaceShip and conforms to the SuperWeapon protocol. A protocol declares methods that can be implemented by a class. They simply define an interface that other objects are responsible for implementing. When a class implements methods of a protocol we say that the class conforms to that protocol. Protocols can have optional and required methods. The Objective-C protocols are something similar to Java interfaces. In fact, it’s said that Java interfaces where inspired by protocols. Protocols are used frequently to specify the interface for delegate objects. Delegate pattern is massively used in iOS development, so you should get familiar with it. Here is the declaration of the SuperWeaponprotocol:
SuperWeapon.h
And here is the Java counterpart:
SuperWeapon.java
Properties
Objective-C supports both strong and weak typing. For example:
In Objective-C object references are pointers. You have to remember to put the * in front of the variable names for strongly-typed object declarations. The id type implies a pointer. So this is quite different from Java, but if you have some experience with C, you should be familiar with this.
Declared properties are a convenience notation used to replace the declaration and, optionally, implementation of accessor methods. This is something extensively used, and very much recommended. This will also help you with the memory management, which will talk about later.
Properties are declared with @property directive, followed by and optional options, followed by a type and name. In your class implementation, you can use the @synthesize compiler directive to ask the compiler to generate the accessor methods according to the specification in the declaration.
The given attributes in the example specifies that retain should be invoked on the object upon assignment (we’ll talk more about this in memory management) and that property is nonatomic. By default properties are atomic so that synthesized accessors provide robust access to properties in a multithreaded environment (Remember, Objective-C is used also for Mac OS X development).
Those two lines of code in Java would look like this:
More about properties and declaration attributes you can read here.
Methods and Messaging
A class in Objective-C can declare two types of methods: instance methods and class methods, marked by -/+:
Class methods are like static methods in Java. You call a method by messaging an object. A message is a method signature with the parameters. Messages are dispatched dynamically. Messages are enclosed by brackets, with the object receiving the message on the left side and the message (along with any parameters required by the message) on the right.
In Java this would look like:
That’s another big syntax difference from Java. The exception are accessor methods which use standard dot syntax:
In Java:
You can also target a method by using selectors which can be useful when handling events. Selectors are like pointers to a method. This is something that Java does not have.
The Memory Management
While there is a Garbage Collector that you can use for OS X application, for iOS you must manage your memory manually. This is probably the most difficult thing to get used to for Java developers. You should try to ensure that your application does not use more memory than necessary, since memory is usually scarse resource in mobile devices. Objects should be destroyed when they are no longer needed, but it is also important that you do not destroy objects that are still used. Cocoa defines a mechanism by which you can specify when you need an object and when you have finished with it. It’s called object ownership.
Any object may have one or more owner. As long as an object has at least one owner, it continues to exist. If an object has no owners, the runtime system destroys it automatically. You only release or autorelease objects you own.
- You own any object you create (alloc, copy, new)
- You can take ownership of an object using retain
- You must relinquish ownership of objects you own when you’re finished with them (release)
- You must not relinquish ownership of an object you do not own
Tip: Class methods by convention own the returning objects, so you do not release them.
The ownership policy is implemented through reference counting or “retain count”. Each object has a retain count.
- When you create an object, it has a retain count of 1.
- When you send an object a retain message, its retain count is incremented by 1.
- When you send an object a release message, its retain count is decremented by 1.
- When you send an object a autorelease message, its retain count is decremented by 1 at some stage in the future
- If an object’s retain count is reduced to 0, it is deallocated
Tip: Use properties, they simplify the memory management.
While in Java you create an object and can forget about it (leaving it to Garbage Collector):
in Objectiv-C you have to allocate memory for it, initialize it, and destroy it after you’ve finished with it:
More about memory management you can read here:
沒有留言:
張貼留言