From Android to iOS--(1), objective-c vs. Java syntax

Source: Internet
Author: User

Development from Android to iOS-(1), objective-c and Java syntax

Contrast

Since June, with the iOS project to do, started the iOS development journey, as of today, has done 2 projects, gave me the feeling that iOS development overall is more simple than Android, but the development of the details of the two but the same annoyance. So to develop iOS, then choose objective-c or swift , my advice is the former, the latter can be swift2.0 and xcode7 after everyone to learn will be better. Well, no nonsense, let's take a look at objective-c and Java syntax comparisons, hoping to let the Java students quickly grasp the objective-c grammar, can quickly open iOS development journey.

I. Object-oriented (OOP) 1, Class-class definition

First, look back at the Java class

publicclass Lei{}

Below, take a look at the class of OC (objective-c abbreviation). The OC language is extended with the C language, preserving the. h header file, which invented. m as an. h implementation. You can assume that each class requires an interface file. h, and an implementation file. M. Like what:

Lei.h文件

////  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文件

////  Lei.m//  Test////  Created by zhe on 15/7/19.//  Copyright (c) 2015年 zhe. All rights reserved.//#import "Lei.h"@implementation Lei@end

You might think Java is so easy, but if you want to do iOS, it's a must. Furthermore, if there is a C-language basis, it would be better. Language is a tool, we just need to know how to use the tool.
From the code snippet above you can see:

@interface用来定义oc的头文件@implementation用来实现头文件定义的类
Properties and Methods

The definition of a property can be in two places
1, in. h files

@interface Lei : NSObject@propertyint num;@end

2, in. m files

@interface Lei()@propertyint num;@end@implementation Lei@end

In the first way, the property is public, the second is private, and only in the Lei. Access in M. These two kinds are often used in Viewcontroller.

The definition of zodiac needs to be used to @property declare.

Method definition
defined in the header file

@interface Lei : NSObject-(void)doPlay;@end

Implemented in M files

@implementation Lei-(void)doPlay{    NSLog(@"doPlay");}@end

Speaking of methods, simply mention '-' means the instance method, ' + ' means the class method.
The so-called instance method is accessible in any instance of the class, and the class method is directly accessible through the class, just like a static method in Java.
Take another look at the following example
. h

@interface Lei : NSObject-(void)doPlay;+(Lei*)shareInstance;@end

. m

@implementation Lei-(void)doPlay{    NSLog(@"doPlay");}+(Lei*)shareInstance{    return[Lei new];}@end

Other, for property settings, getter and setter are automatically encapsulated in OC, such as Num above

//设置num[self setNum:8];//获取num[self num];或者self.num;

Self refers to the current class's own pointer.

2. Inheritance

Integration is simple, and Java uses extends keywords to define it. OC, however, uses a colon to represent an inheritance relationship.
I don't have to say much about the benefits of inheritance.

3. Interface

Speaking of interfaces, Java uses interface to define interfaces. But by learning from the above classes, you know that @interface has been used to declare classes. So is there any interface in OC for this thing? The answer is no, but a change is called a delegate (delegate).
We all know that interfaces are often used as callback mechanisms. The concept of delegation is also used in many programming languages and is used in OC. Speaking of delegate, you have to say protocol; The two often come together. Let's take a look at how the Commission is done.
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 intNum@end

The @protocal in the code snippet above defines a delegate.
How to use it?

. m medium

//  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 also completely different from Java, using the last <> in @interface and then implementing the method in @implementation.

Ii. Comparison of common classes
Language and type Java OC
String String NSString
Integral type Int int or Nsinteger
Integral type Long Long or Nsinteger
Floating point Type Float Float
Double-precision floating-point type Double Double
bool Boolean BOOL
Collection List Nsarray or Nsmutablearray
Dictionary Map Nsdictionary or Nsmutabledictionary

These classes are common, and there are many different ways to use them. Because OC is based on the C language extension, initially for memory control in the use of the class there are many rules, now the new version of the iOS system for the memory of the automatic recovery process is very good, late learning slowly. The students who have studied Java have been recovering from memory without making much effort.

Third, written in the last

There are a lot of things you need to delve into for iOS development, such as the usual usage and attention issues mentioned above. In the process of using a little careless will fall into the pit. But as a developer of Android, moving to iOS will be a fun process. As long as learn the basics, fast access to iOS development is not difficult, for their own refueling it!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

From Android to iOS--(1), objective-c vs. Java syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.