iOS development Getting Started Tutorial-summary-write to avid programming enthusiasts

Source: Internet
Author: User
Tags install mac os x

The program is frivolous, the code is dim, Apple developed Android crazy! --to avid programming enthusiasts

Write it in front.

Learning iOS app development has been a while, recently a little idle, just want to record the previous stage of the entire learning process. Simply start with the basics, step-by-step through the entire process of creating and running a simplest iOS app, with a lot of knowledge or problems. It's not really a tutorial, but hopefully it will help more development enthusiasts get started faster and better.

iOS Development Environment Brief

The most friendly, realistic, and convenient development environment is the installation of Xcode in a Mac system for development. The reason: First, the development tools (Xcode) limit. Apple officially only provides xcode for Mac, and Xcode's built-in Xcode IDE, Profiling Tools, IOS Sdk,ios Emulator and the latest OS X make it easy to quickly code-edit and apply debugging, making it the perfect choice for Mac and IOS development tools. Second, code hints and auto-completion. The name of the class or method used in the development of iOS, in order to be more semantic, so that developers can be at a glance, usually longer, which often makes the developers just in touch with iOS development to remember these methods to break through the brain. and Xcode has OC code hints and auto-completion features that are better than other editors, which will greatly reduce the entry threshold for development, which determines that there is no reason for you to develop an iOS app without using Xcode. As a result, developers using non-Apple computers are best equipped to install MAC OS x virtual machines for development learning, otherwise you will not only spend one or more more time to build the development environment than others, and code writing will be more energy than those who use Xcode development.

The first step is to create the project 1.1 new iOS Project

Open Xcode, and if you're opening Xcode for the first time, you'll see a welcome screen:

You can either click on the Create a new Xcode project in the diagram at 1, and click the button labeled 2 on the graph to open the existing Xcode project in your computer. You can also modify the check box state of the 3 mark to set the Welcome screen to be displayed the next time you start Xcode.

This article does not demonstrate creating a project here by clicking on the 1 location, first click on the red button in the upper left corner of the interface to exit the Welcome screen, and then tap File--NEW project in the upper left corner of the screen. As follows:

Next you will see the following interface:

First, the system filters, the iOS app selects the iOS list counterpart, and the OS X app selects the OS X list counterpart. This example is an iOS app, so select Application in iOS (shown in the upper left corner of the image), then select Apply template selection, list the most common application template types on the right side of the interface, and the developers can choose their own application according to their own needs, this example select single View Application (shown in the red box in the upper right corner of the figure). After completing the above selection, click the Next button in the bottom right corner of the window to enter the next Setup window:

Here the content will be based on individual circumstances and different, so do not repeat, follow the window prompts, fill in the relevant information can be. Note the three checkboxes below the window, with the functions of using storyboards, using automatic reference counters, including unit tests, and checking/canceling according to individual circumstances. When you are finished, click the Next button in the lower right corner to enter the next window:

As shown in the window, select the Project storage location and click the Create button in the bottom right corner of the window to complete the app creation process.

1.2 Application directory Structure description

After the project is created, Xcode enters the project development interface, which is broadly divided into three parts, the left-hand project and directory structure list, the code editing area in the middle, the right-hand property settings, and the control list. The following is the directory structure:

The project initially contains three main catalogs, which developers can add to their own content in a subsequent development. which

    • Firstiosapp directory with the name of the project, mainly for the application of relevant source code and configuration files;
    • The framework that the framework uses to store the project, by default adds a diagram of three necessary frameworks;
    • Products are used to store generated application files, which developers generally don't care about.
1.3 Overview of application Initial structure

The portal file for the entire application defaults to APPDELEGATE.M and should be modified by the configuration file (not attempted). Its internal structure is as follows:

This file contains the method interfaces that can be called at various stages in the application life cycle so that the application can perform the appropriate operations at different state stages. These methods are not explained, the developer can almost look at the name of the idea.

1.3.1 The life cycle of iOS apps

For the above diagram method if there is no confusion about where, you can refer to (from the network):

Getting started with the second step of code (data type)

It's not enough to do iOS, just understand the directory structure, just understand the life cycle, and these are just the basics. Certain programming capabilities are also essential. The following is a brief introduction to the iOS Development programming language--objective-c, followed by the abbreviation OC.

Learn a language, often need to start from the most basic data type (remember when the teacher is doing this when the university study), as for the programming logic, artifice, etc. is in the development process slowly accumulated. Good to have.

2.1 Basic data types

OC is a superclass of C, so OC has almost all the features of C or C + +, and of course it has its own unique place. So OC also supports C-language data types, such as Int,float, which can also be used in OC. The underlying data type is not the focus of this article, and there is not much to talk about here.

2.2 OC Data type

OC is not just using the C + + data type, it also has its own data type, although it looks a bit weird with the various types of data commonly known to people, but the fact that OC's data type is a fun thing to understand is not difficult. Several common OC data types are listed below.

2.1.1 NSNumber class and Nsinterger

NSNumber is to wrap the underlying data type in the form of an object, which provides a way to:

+ (NSNumber *) Numberwithchar: (char) value;+ (NSNumber *) Numberwithint: (int) value;+ (NSNumber *) Numberwithfloat: (Flo at) value;+ (NSNumber *) Numberwithbool: (BOOL) value;

For example, to initialize a nsnumber from an int data can be written like this:

NSNumber *number = [NSNumber numberwithint:100];

To get Nsinteger data from an NSNumber instance, you can do the following:

Nsinteger integer = [number Intvalue];
2.1.2 NSString Type

NSString is a string class of OC. Unlike ordinary strings, the value of NSString requires an "@" symbol in front of the normal string. For example, to initialize a "I ' m a string." The OC string, we need to do this:

NSString *str = [NSString stringwithstring: @ "I ' m a string."];

Looks very troublesome, very complex look, don't worry, OC should also take this into account, so it also offers shorthand form:

NSString *str = @ "I ' m a string.";

NSString class also provides a lot of other forms of string creation methods (such as: stringWithFormat, etc.), here is not listed, and later in the development of slowly experience it.

2.1.3 Nsarray Type

Nsarray is an array class of OC. An array of OC is powerful, allowing different types of data to be allowed in an array, just like a powerful javascrit. Initializing an array can do this:

Nsarray *arr = [Nsarray arraywithobjects:@ "a", @ "B", @1, nil];

Simplified forms are supported, of course:

Nsarray *arr = @[@ "A", @ "B", @1];

It is important to note that when you create an array using the Arraywithobjects method, you end up with nil, and you do not need to use a simplified method.

The following two forms can be used in development to obtain the value of the array's corresponding index position (index):

NSString *a = Arr[index];
NSString *b = [arr objectatindex:index];
2.1.4 Nsdictionary Type

Nsdictionary is the dictionary class of OC. The dictionary type exists in the form of a Key-value key-value pair in use. also supports a variety of initialization methods, the following list of two kinds:

Nsdictionary *dic = [Nsdictionary dictionarywithobjects:@[@ "a", @ "B", @ "C"] forkeys:@[@ "First" @ "second", @ "third"];
Nsdictionary *dic = @{@ "First": @ "a", @ "second": @ "B", @ "third": @ "C"};

The value method is similar to Nsarray:

NSString *a = [dic objectforkey: @ "First"];
NSString *b = dic[@ "Second"];
2.1.5 NSDate Type

NSDate is the date class for OC. Examples of common usage:

NSDate *date = [NSDate Date]; Return Current time
NSDate *date = [[NSDate alloc] init]; Initialized to the current time, similar to date
The third step is the realization of OC class (member variables and methods)

iOS development is often accompanied by views, and views are often inseparable from view controllers, and each view controller in iOS development is an OC class, so learning iOS development must understand the OC class.

The OC class uses @interface classname:superclass <protocol> ... @end structure to define, NSObject is the parent class of all OC classes. Compared to Java @interface keyword is equivalent to the Java class keyword, OC protocol is the Java interface, which is very confusing, we need to pay special attention when learning.

Declaration of Class 3.1

As an example of declaring a person's class (view Controller) and implementing it, with name, age two member variables, you naturally need to define getName and SetName, Getage, and Setage, let's look at this process.

First, the structure of the class is as follows:

@interface person:nsobject {    nsstring *name;    int age;} @end

We then declare its setter and getter method, which is used to set and read the values of its member variables. In the traditional way it should be defined as follows:

@interface person:nsobject {    nsstring *name;    int age;} -(void) SetName: (NSString *) newname;-(NSString *) getname;-(void) Setage: (int) newage;-(int) getage; @end

See, isn't it simple? However, since the above mentioned is the traditional way, then certainly OC also provides a more modern way:

@interface person:nsobject {    nsstring *name;    int age;} @property (nonatomic, strong) NSString *name; @property (nonatomic, assign) int age; @end

Yes, you're right, it's that simple, it's the same as the code above. To add, the @property itself can accept parameters to specify the getter and setter of the variable, with the parameters available values and specific functions as follows:

Nonatomic//Declare variable only works in single thread atomic//default, there may be multiple threads that use this variable copy//Allocate a new space, copy the contents of the original address assign//simple pass pointer retain// Pass pointer after reference counter (Retaincount) will add 1strong//strong reference weak//Weak reference

The declaration is done, and below we look at its implementation, the implementation of the class in OC needs to use another keyword @implementation, the format is as follows:

@implementation Person@end

Let's take a look at the traditional way of declaring getter and setter methods:

@implementation person-(void) SetName: (NSString *) newName {    self.name = newName;} -(NSString *) getName {    return self.name;} -(void) Setage: (int) NewAge {    self.age = newage;} -(int) getage {    return self.age;} @end

After reading the traditional way of implementation, do you want to see the style of modern style? We continue to look down:

@implementation person@synthesize name; @synthesize age;//or directly below the sentence//@synthesize name,age; @end

Isn't it amazing? In the new version of Xcode, @synthesize can be omitted, but the suggestions are all written.

3.2 "+" and "-"

Careful readers should have found that the traditional way above contains a magical symbol "-". What does this mean? First we need to explain that a class can have member variables and methods, and methods can contain class methods and instance methods. A class method is a method that can be called directly using the class name, whereas an instance method is a method that requires an instance of the class to be called. The use of "+" in OC means that the method is a class method, while "-" is simply a natural representation of an instance method.

As for the Declaration and implementation of the method, which is roughly the same as the traditional way of declaring getter and setter, this part is no longer duplicated.

Fourth step operation and commissioning

Each application development process is long, and in this process we inevitably need to run the program, to see the current effect, check the code for any exceptions or errors.

4.1 iOS simulator 4.1.1 Select device

The power of Xcode is not blown out, with the iOS simulator built into Xcode that simulates almost any iOS real-time device, so developers can test applications under development with the iOS simulator, improve development efficiency, reduce errors, and save development times. It is necessary to select the analog device corresponding to the application before running, otherwise it may cause unexpected problems in running the result.

Below the menu bar of Xcode, there is a place to select the simulator:

Select the simulator and click the Run button on the left to launch the app in the specified simulator. The first time you start the simulator is usually slower, and the back is good. You can also stop the current app in the emulator by clicking the Stop button next to run. There may be times when you want to run the iOS emulator independently without starting the current app, which is also possible, see:

Perhaps the same device, but multiple dimensions, multiple screen resolutions, such as: iphone 4, 4S, 5, and so on. So how do you choose? Don't be careful, there are simulators:

Really iOS simulator in hand, development and debugging do not worry Ah!

4.1.2 Simulator Custom settings

After reading the above introduction, is it felt that there is an impulse to start immediately? Don't worry, there are more advanced! As you know, many popular iOS applications now have location capabilities, so how do you test the positioning in the simulator? Look at you and you'll understand:

4.1.3 Storage screen shot

Development applications are often not one step, and may produce a variety of outlandish phenomena, perhaps a display that is not normal, perhaps an unexpected effect, or you want to share your current display with other members of the development team, as a developer, you may want to record the occurrence of these phenomena, such as:, recording screen and so on. These are good methods, but the iOS simulator has a much more convenient way to:

4.2 Program debugging

Every developer can not guarantee that his program is perfect, so usually when we run the program there will be a lot of strange warnings or errors, some can be at a glance, solve, some are hidden deep, hard to find. At this point debugging the breakpoint is intuitive.

In the line number area on the left side of the Code Editor, click in this area to add/close (not delete) breakpoints on the corresponding line. Start a breakpoint, and when you run the program again, the program executes to the current line and pauses on the current line, and the developer can see the information logged at the breakpoint at the console. Roughly

, at the breakpoint we can see information such as the current value of the variable date and the memory address at the console so that it can be used to determine whether the program is running to that point, or to view some additional information.

4.3 NSLog

There are times when we may encounter problems that cannot be solved by breakpoints, and we can also debug with NSLog. The NSLog is also used to output information in the console.

Well, it's time for a brief, hoping to help you.


Reprinted from http://www.seejs.com/?p=102

iOS development Getting Started Tutorial-summary-write to avid programming enthusiasts

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.