Development from Android to iOS -- (1) Comparison of objective-c and java syntax, androidobjective-c
Development from Android to iOS -- (1), objective-c, and java syntax
Comparison
Starting from June, I started my iOS development journey because of the iOS project. As of today, I have already created two projects, I feel that iOS development is easier than Android, but the development details are the same. So if you want to develop iOS, chooseobjective-cOrswiftMy suggestion is the former, which can be better studied after swift2.0 and xcode7. Now let's take a look at the comparison between objective-c and java syntax. We hope that java can quickly master the syntax of objective-c and quickly start iOS development.
I. Object-oriented (OOP) 1. Class Definition
First, review the java class
public class Lei{}
Next, let's look at the classes of objective-c (short for objective-c. The oc language is extended and C language. It retains the. h header file and invented. m as the implementation of. h. An interface file. h and an implementation file. m can be required for each class. For example:
Lei. h file
/// Lei. h // Test // Created by zhe on 15/7/19. // Copyright (c) 2015 zhe. all rights reserved. // # import <Foundation/Foundation. h> @ interface Lei: NSObject @ end
Lei. m file
/// Lei. m // Test // Created by zhe on 15/7/19. // Copyright (c) 2015 zhe. all rights reserved. // # import "Lei. h "@ implementation Lei @ end
You may think that java is good and simple, but if you want to develop iOS, this is necessary. Furthermore, it would be better if there is a C language foundation. Language is a tool. You only need to know how to use it.
From the above code snippet, we can see:
@ Interface is used to define the header file of oc. @ implementation is used to implement the class defined by the header file.
Attributes and Methods
The attribute can be defined in two places.
1. In the. h file
@interface Lei : NSObject@property int num;@end
2. In the. m file
@interface Lei()@property int num;@end@implementation Lei@end
First, the attribute is public; second, private, and only allowed in Lei. M. These two types are often used in ViewController.
Phase definition needs to be used@propertyTo declare.
Method Definition
Define in header file
@interface Lei : NSObject-(void)doPlay;@end
Implement in m file
@implementation Lei-(void)doPlay{ NSLog(@"doPlay");}@end
When it comes to methods, simply put '-' to indicate the instance method, and '+' to indicate the class method.
The so-called instance method can be accessed in any class instance; the class method is directly accessed through the class just like the static method in java.
Let's look at the example below.
. H
@interface Lei : NSObject-(void)doPlay;+(Lei*)shareInstance;@end
. M
@implementation Lei-(void)doPlay{ NSLog(@"doPlay");}+(Lei*)shareInstance{ return [Lei new];}@end
Others, for attribute settings, the oc will automatically encapsulate getter and setter, such as the above num
// Set num [self setNum: 8]; // obtain num [self num]; or self. num;
Self refers to the pointer of the current class.
2. Inheritance
Integration is simple. java usesextendsKeyword. The oc uses the colon to represent the inheritance relationship.
I don't need to talk about the benefits of inheritance.
3. Interfaces
Description Interface, java useinterfaceInterface. However, through the above class learning, we know that @ interface has been used to declare classes. Is there any interface in oc? The answer is no, but it is changed to a delegate ).
We all know that interfaces are often used as Callback mechanisms. The concept of delegation is also used in many programming languages and also used in oc. Speaking of delegate, We Have To Say protocol; these two often appear together. Let's take a look at how the delegation works.
Defined in. h
/// Lei. h // Test // Created by zhe on 15/7/19. // Copyright (c) 2015 zhe. all rights reserved. // # import <Foundation/Foundation. h> @ protocol LeiDelete <NSObject>-(void) setName :( NSString *) name; @ end @ interface Lei: NSObject @ property int num; @ end
In the above code snippet, @ protocal defines a delegate.
How to use it?
In. m
/// Lei. m // Test // Created by zhe on 15/7/19. // Copyright (c) 2015 zhe. all rights reserved. // # import "Lei. h "@ interface Lei () <LeiDelete> @ end @ implementation Lei-(void) test {}- (void) setName :( NSString *) name {}@ end
It is completely different from java in use. It is included at the end of @ interface, and then implemented in @ implementation.
Ii. Comparison of common classes
| Language and type |
Java |
Oc |
| String |
String |
NSString |
| Integer |
Int |
Int or NSInteger |
| Integer |
Long |
Long or NSInteger |
| Floating Point Type |
Float |
Float |
| Dual-precision floating point |
Double |
Double |
| Bool |
Boolean |
BOOL |
| Set |
List |
NSArray or NSMutableArray |
| Dictionary |
Map |
NSDictionary or NSMutableDictionary |
These classes are common and there are many differences in their usage. Because oc is based on the C language extension, there are many rules for memory control in class usage at first, and now the new ios system is very good for automatic memory recovery, later, you will learn more slowly. Students who have learned java have always been concerned with memory collection and have not made much effort.
Iii. Conclusion
There are many things that need to be studied in depth for iOS development, such as the common usage and precautions mentioned above. When you use it, you will accidentally fall into the trap. However, as an android player with development experience, switching to iOS will be a very interesting process. As long as you learn the basics, it is not difficult to quickly enter iOS development. Please cheer for yourself!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.