Java
|
Objective C
|
What the?!
|
| - |
id |
Just remember: everything is an id. Any object, any … anything. |
Class (concept) (defined by a class) .class Files use .java extension. |
Class (concept) (defined by an interface and an implementation, see class structure below) class method Interface files use the .h extension, implementation files use the .m extension. |
Wanna know the class? do a [myObject class] |
| Interface |
Protocol Files use the .h extension. |
One adopts a protocol rather than implement an interface. |
| this, super |
self, super |
Easy. |
| null |
nil |
lol |
| Field |
Property (and instance variable) |
One declares an instance variable (optional), then declares the @Property and finally specifies how it is implemented (@synthesize, @dynamic). Easy as. |
Call / Invoke myObject.init() |
Send Message [myObject init] or myObject.init() (quite unconventional though) |
That is how one uses a type’s methods. This gets more complicated, look up Selectors in the context of sending messages. |
Subclass class A extends BaseClass |
Subclass, Category, Extension @implementation A : BaseClass (CategoryOMG ) |
You see, things are not as simply as you thought. A class can not only be extended but also mutilated modified via Categories and Extensions. An Extension is just an anonymous Category. Simple!
When defining a category you must import the class’s interface file. |
| abstract |
- |
N/A |
| public, protected, private |
same |
After specifying @public (or another) you can just list all the instance variables that have the same visibility. No need to do it for each variable. Cool eh?
One mention goes to package which cares about how many bits your platform has. google for this (search box available above) |
static method, non-static method static void doSmth(){} void printName(){} |
class method, instance method + (void) doSmth; - (void) printName; |
That is just the method declaration in the objc column. The implementation uses brackets after the method name to group the method’s code, just like Java. |
| public void setOrigin(int x, int y); |
- (void) setX:(int)x andY:(int)y;
Default return type is id. |
AndY is just a label for the second argument. Note that the colons and the labels are part of the method name. |
| Object |
NSObject |
This isn’t necessarily the Class that everything extends from. There is also NSProxy and you can also write your own. Feeling brave already? |
| toString() |
description |
The NSObject implementation returns the name & address of the object. Sounds familiar? |
| import |
#import |
One can reference just the class not include the whole thing. See comment below. |
Class structure: import java.lang.*;
class MyClass extends BaseClass implements Comma,Separated,List,OfInterfaces { //fields and constants private String name;
//method definitions public String getName(){ return this.name; } } |
A class requires an Interface and an Implementation.
#import “BaseClass.h” @Class AClassThatsBeingMentioned, AnotherClassLikeThat;
@interface MyClass : BaseClass <Protocol1, Protocol2> { //instance variables NSNumber *number; }
//method declarations - (void) printThatNumber(); @end
//and then don’t forget the implementation
#import “MyClass.h”
@implementation MyClass //method definitions - (void) printThatNumber(){ //can you figure this one out? } @end |
Too much can be said and surely you will be asking me questions anyway.
Still: @Class is used when no instances are created / used in your implementation but rather parameter names are of that particular type. Is is simply a low-weight #import. |
| Interface
interface A extends AnotherInterface, YetAnotherInterface{ void foo(); void foo2(); [...] } |
Protocol Specified using @Protocol
@Protocol A <AnotherProtocol, YetAnotherProtocol>
//required by default - (void) fooBar;
@optional - (void) foo; - (void) foo2; @required - (void) bar; - (void) bar2;
@end |
So much better…
Can incorporate another protocol just like Interfaces can extend other interfaces.
Methods are @required (by default) or @optional. Use these to group the mandatory and the optional methods or your interface protocol.
You can declare objects that adopt a protocol: id <MyProtocol> myObject;
And last, you can forward reference a protocol: @protocol ForwardProtocol; @protocol MyProtocol - foo(id <ForwardProtocol> myObject); @end Confused? It’s okay, you’ll never use this anyway. (Unless you don’t have a Java shaped brain of course but an objc one.) |
| MyClass myObject = new MyClass(); |
MyClass *myObject = [[MyClass alloc] init];
or for non static typing: id myObject = [[MyClass alloc] init]; |
These are virtually equivalent. I suggest you use static typing if you want your compiler to be able to tell you if some method does not exist in the type that you are using, for example. |
object’s fields initialization { [...] }
and for static fields static { }
/* Honestly, when did you last use this rather than a constructor with arguments? */ |
- init - initWithBlah - init…
and for Class initialization
+ (void) initialize; |
NSObject’s init returns self. Therefore all classes should define an init method to initialize their instance variables.
The runtime will call initialize before using a class object. I am a little bit confused myself but I believe them. |
| java.lang java.util etc |
NS, CG, CF |
NS = NextStep CG = Core Graphics CF = Core Foundation
unless I am wrong of course. |
| instanceof |
Defined in NSObject:
isMemberOfClass – actual class isKindOfClass – is or inherits from (instanceof) respondsToSelector – implements method conformsToProtocol – implements (“conforms” to protocol) |
Cool!
Responds to selector means that the method exists in the class passed as argument while conforms to protocol means that ALL the methods in the protocol. I suspect that a YES response is returned if the methods defined by the protocol exist in the type passed as argument even if the type does not conform to the protocol. |
| synchronized |
@synchronized() |
No synchronized keyword for methods. A mutex is required. Self is a possible candidate, another good one can be _cmd as long as the method name is unique. Who is _cmd? Google in the search box above. |
| Exception |
NSException
@try; @catch; @finally and @throw are the keywords. |
You can throw other objects, not necessarily part of the NSException inheritance tree.
Once an exception is caught it can be re-thrown by using @throw without having to specify the argument. |
| !!!
Invoking non-existent methods. Say they have been deprecated and removed from the API. |
forwarding
- (void) forwardInvocation: (NSInvocation *) theInvocation { if ([delegateObject respondsToSelector: [theInvocation selector]]){ [theInvocation invokeWithTarget:delegateObject]; } else { [super forwardInvocation:theInvocation]; } }
Here’s what just happened: a method that does not exist on our “self” was called and the runtime forwarded the call to the method I wrote above. It could’ve freaked out but it didn’t, it kept its calm and got my kudos. The original call message is obviously wrapped inside the NSInvocation. Brilliant!
Note: the NSObject’s forwardInvocation doesn’t do much. Soz. |
This is so cool, it should be a mandatory feature in all modern programming languages.
Can replace @deprecated, multiple inheritance (I can almost hear your cry), surrogate objects, proxies, delegates and more. It is up to you to confuse the crap out of all those that try to steal your job.
Piece of advice: consider overriding respondsToSelector, isKindOfClass, etc to make it look like your surrogate objects actually belong in the type hierarchy. That will simply make you irreplaceable. |
| Remoting | RPC |
Remote Messaging
Type qualifiers: - oneway – to specify that the call is asynchronous - in – the relevant data is being passed in the message - out – the argument is used to return info by reference - inout – in and out together. duh! inout is the default too except for const arguments for which in is the default obviously. - bycopy – return / pass a copy of the object rather than a pointer to an address. Must ensure that the receiver is aware of the type (in its address space). - byref – !bycopy and is the sad default.
Example: - (oneway void) goOutsideAndEnjoyTheSun; - setStuff:(in struct things *) aThing;
|
Normally all these qualifiers are to be used in the protocol that defines the remote messaging, not in the classes that you implement. They are implementation guidelines more than anything.
New to pointers? Then trust me: out and inout only make sense for pointers. |
| @deprecated |
@interface SadClass - sadmethod __attribute__((deprecated)); @end |
|
| Spring Framework, Log4j, Maven, Axis, Acegi |
LOL, ROFL, ZOMG |
I just thought it would be funny |