📜 ⬆️ ⬇️

Shocking Objective-C for Java Programmers

What is this article about?


Two facts. There are many Java programmers in the world. Objective-C popularity is growing. Conclusion: A Java programmer who studies Objective-C is not uncommon. If you know the key differences between languages, you can effectively use existing knowledge of Java and start writing in Objective-C faster.

I started with C, I have been writing Java for the last 15 years, sometimes switching to C ++, Python, Perl, watching Scala with interest. And now Objective-C.

From each trip there are usually a few stories about the most amusing differences between "us" and "them." Without pretending to be complete, I’ll tell you about the features of Objective-C, which especially surprised me as a newcomer from Java. Later, I noticed that misunderstanding or rejection of these features generates Java code written in the Objective-C syntax, whereas it is much more natural to write just Objective-C code. Language is not only syntax, it is primarily idioms. Once again I emphasize - this is not a guide, but tourist notes. If they seem interesting - I will continue to talk about my observations in subsequent articles.

We send, not call, messages, not methods


Java:
object.method(param);

Objective-C:
[object method:param];


Java , Objective-C — . . -, , ( , , , Java ), (selector), -. , , .

, Java , (warning). — . — .

?


Java:
public Feature addFeature (String name, String value, boolean isOptional);

Objective-C:
-(Feature*) addFeature :(NSString*)name withValue:(NSString*)value asOptional:(BOOL) optional:


Objective-C “” . , , addFeature:withValue:asOptional:

SEL selector = @selector(addFeature:withValue:asOptional:);

, , , 16 . 16 .

: ,



Java

Objective-C:
@interface NSString (LetsAugment)
-(BOOL) isGoodActor;
@end


, NSObject. (category). LetsAugment, isGoodActor NSString. NO (false Java), , YES (true) , . — :

if ([@”Steven Seagal” isGoodActor]) NSLog (@”Yeah!”) else NSLog (@”What's wrong with you?”);

, , .

, -, . , , . . -, .

:


Java:
MyTao tao = null;
int howManyHands = tao.clap();

Objective-C:
MyTao* tao = nil;
int howManyHands = [tao clap];


, , NullPointerException , , . — howManyHands 0. 1, , .

. nil (null Java), (nil, 0, NO ..). , .
Java: ()

class Profile {
int getVersion() {
...
}

class Feature {
Profile getProfile(String name) {
...
}

class Phone {
Feature getFeature(String name) {
...
}

class PhoneFactory {
static Phone designAndProduce(String model) {
...
}
}

...
int version = 0;
Phone phone = PhoneFactory.designAndProduce(“iphone5”);
Feature btFeature = phone.getFeature(“bt”);
if (btFeature != null) {
Profile profile = btFeature.getProfile(“a2dp”);
if (profile != null) {
version = profile.getVersion();
}
}



Objective-C:

@interface Profile : NSObject
@property (readonly) int version;
@end

@interface Feature : NSObject
- (Profile*) getProfile:(NSString*)name;
@end

@interface Phone : NSObject
+ (Phone*) designAndProduce:(NSString*)name;
-(Feature*) getFeature:(NSString*)name;
@end
...

Phone* phone = [Phone designAndProduce:@"iphone5"];
Feature* btFeature = [phone getFeature:@"bt"];
Profile* profile = [btFeature getProfile:@"a2dp"];
int version = profile.version;



Objective-C Java . [phone getFeature:@«bt»] nil, version 0.

« »? nil , , , . (getter/setter). , Objective-C (- profile.version?) — (property), .

')

Source: https://habr.com/ru/post/138441/


All Articles