IOS learning-sharing basic learning materials for beginners

Source: Internet
Author: User

IOS learning-sharing basic learning materials for beginners

/*
1. OC introduction:
OC is a superset of C language. It adds the smallest object-oriented syntax based on C language.
2. Compare C to learn OC
Data Types, keywords, process control, and functions
3. Object-oriented Thinking
1. It is a programming idea closer to real life.
2. It thinks about who focuses on the problem
3. to complete a task, first find the task object to complete. If no task object is created, the system directs the object to perform the task.
4. Objects
In object orientation, everything is an object.
From another perspective, any specific thing in real life can be considered as an object.
5. Class
From programming design: In reality, a class of things are abstracted. abstraction is to extract the attributes and behaviors shared by these things.
From the programming language: to create an object, a class is required, and the object is an instance of the class;
6. Relationship between classes and objects
Relationship between vehicle drawings and Automobiles
7. Design
1. Design a class through text description
Term Extraction Method: You can extract a class whenever you encounter related terms.
2. Image extraction class
Some common things on the graph can be converted into a class.
A class contains three parts:
1. Class Name: The name of this type of thing
2. attributes: certain things have some state features.
3. Behavior: Some things have some functional features.

8. Create a class
0. Design
1. Declaration: a summary of the statement. It is for the person who uses the statement.
1. Determine the class name
2. NSObject must be inherited to enable the class to have the ability to create objects.
3. The declaration must start with @ interface and end with @ end
4. Declare attributes in Braces Between @ interface and @ end.
Attributes cannot be initialized when they are declared.
5. The declaration method must be declared between {} and @ end. method implementation is not allowed.
Format: method type identifier (Return Value Type) method name: (parameter type) parameter name
1. All data types should be expanded with parentheses
2. A parameter corresponds to a colon
3. colon is part of the method name

2. implementation: all methods to be declared must be written in @ implementation and @ end. Note: 1. Only classes that are not declared are implemented, when linking, an error is reported. 2. Only classes without declarations can be used (OC weak syntax), but cannot be implemented during programming. 3. Declarations and implementations cannot be nested, other classes cannot be declared inside a class. 4. Less @ end 5. Declared attributes are not written in braces. 6. Declared methods are written in braces. 7. called object methods of the class. you can only use this type of object, it cannot be called directly like a function. 8. A function cannot directly access member variable 9 of an object. The function is parallel to method 1 and the function is parallel, no one belongs to anyone. 2. variables defined in functions are local variables, the function can directly operate on the member variables of A Class. 3. The function call is an object method called directly by the function name. 1. It belongs to the object of the class, only objects of this class can be called. 2. member variables can be directly accessed in object methods, because they all belong to the class Object 10, Object creation Object * obj = [class name new]; 1. Create a bucket for the class object in the heap. 2. Initialize all member variables to 0. initialize them to nil if they are of the object type. 3. Return a pointer to the object.

*/

// Create an NSString object

// 1. Create by literal
NSString * str = @ "The weather is cold. Pay attention to your health !";
NSLog (@ "% @", str );
// 2. Object Method
// NSString * str1 = [NSString new];
NSString * str1 = [NSString alloc];
// Str1 = [str1 init];
Str1 = [str1 initwithuf8string: "Everyone has to study hard."];
NSLog (@ "% @", str1 );

// 3. Create an object using the Class Method
// Class method it belongs to the class and is called by the class name
// The format string here is the same as the format string in NSLog
NSString * str2 = [NSString stringWithFormat: @ "the stock has fallen by % d today !", 10];
NSLog (@ "% @", str2 );

// 4. Specify the length function in the NSString object
// Command + click with the left mouse button
// Length indicates the number of characters in the string.
Long len = [str length];
NSLog (@ "% ld", len );

Char * strc = "The weather is cold. Pay attention to your health! ";

/*
A Boolean type provided by bool oc is used to indicate logical truth and false.

BOOL has two values in OC: YES and NO.
YES actually corresponds to number 1
NO false corresponds to number 0

BOOL can improve code readability and reduce code error rates.

Variables can be defined for all data types
All data types can be used as form parameters or real parameters.
All data types can be returned.

*/
03-objects and functions

// Passing an object as a function parameter is Address Transfer
// The member variables in a function can be accessed through objects
// You can also call its object method (member Method
/Because the object is being created in the heap, it can be used as the return value of the function,
// The local variable cannot be returned as a function. It should be in the stack and will be consumed if it leaves the function.

04-objects and methods (master)
/*
1. The method parameter passing of objects is address passing.
2. objects can be continuously transmitted in methods.
3. The object can be used as the return value of the method.
*/

*
Object method:
1. When declaring and implementing methods, start-
2. Object methods are subordinate to objects and can only be called through objects
3. You can directly access member variables in object methods, because both object methods and member variables belong to
4. Other Object methods can be called in object methods.
5. Class methods can also be called in object Methods
Class method:
1. Declarations and implementation identifiers are both +
2. The class method belongs to the class itself.
3. Class methods are called directly by class names and cannot be called through objects.
4. The class method cannot access the member variable because the member variable is of an object rather than a class.
5. The object method cannot be called directly in the class method, that is, the object method cannot be called through self.
If you want to call the object method in the class Method
1. Create an object in the class Method
2. Input an object as a form parameter

Class Method advantages:
1. No need to create objects when calling class methods, saving memory
2. To call an object method through an object, first find the isA pointer through the object, and then find the corresponding method in the class.
3. You can directly find the class and call the corresponding method through the class call method. The efficiency of all class methods is relatively high.
4. Use the class method when the member variables are not required in the method.
/*
Object method:
1. When declaring and implementing methods, start-
2. Object methods are subordinate to objects and can only be called through objects
3. You can directly access member variables in object methods, because both object methods and member variables belong to
4. Other Object methods can be called in object methods.
5. Class methods can also be called in object Methods
Class method:
1. Declarations and implementation identifiers are both +
2. The class method belongs to the class itself.

*/
@ Class CZSoldier;
// Tell the compiler that this is a class, but what is in the class? @ class cannot tell the Compiler

/*
In iOS development, a class declaration is a header file (. h), and a class implementation is an implementation file (. m)
*/
5. Multi-file Development
1. To facilitate program management, we divide a class into two files.
. H file: used to store the class declaration. It is for the person who uses the class, and the declaration is equivalent to the dial.
. M file: used to implement the class method, internal implementation of the table, such as gear
2. When we need to use a class, we need to import the header file of this class and cannot import the implementation file.
3. When two classes directly contain loops, @ class must be used at one end to declare the class.
4. @ class only tells the compiler that this is a class and other information about the class itself is unknown. This @ class is generally used only in header files.

/*
Encapsulation:
1. Class is the encapsulation of data and functions. Data is a member variable, and functions are class methods or object methods.
2. Data encapsulation
If we set the attribute to @ public, this attribute can be arbitrarily modified outside, and we lose the right to manage this attribute.
1. When you expose an attribute to the outside, you lose management of it. Once this attribute is used by many people, if you manage this attribute, the maintainability of the class becomes very poor.

2. How to encapsulate data 1. Provide getter and setter methods for each attribute 2. setter method: Also known as the write method, assign a value to an object. 1. It must be an object method. 2. It does not return a value. 3. Remove the underline from the name set + attribute name. The first letter is uppercase. 4. A parameter must be provided, the parameter type must be the same as the type of the member variable of the object. 3. The getter method is also called the read method. The value of the value assigned to the read attribute must be 1. The object method 2 must be returned, and the return value type must be the same as the member variable type. 3. The method name must be a member variable and remove the underline. 4. It must be a null parameter.

*/
@ Interface CZCar: NSObject
{
Int _ wheels; // Number of wheels'
Double _ speed; // speed
}

// _ Wheels setter
-(Void) setWheels :( int) wheels;

(Int) wheels;

(Void) setSpeed :( double) speed;

(Double) speed;

@ End

// However, external users still need to access this attribute, but we do not allow direct methods. Instead, we provide a read method, getter method, and setter method.
// When we provide the setter method, the program will be more flexible and can quickly respond to changes in external needs
// Age write method (setter)
/*
1. It must be an object method.
2. It does not return values.
3. The name set + attribute name removes the underline and the first letter is capitalized.
4. A parameter must be provided. The parameter type must be consistent with the type of the member variable of the object.
*/
-(Void) setAge :( int) age;

// Age read (getter method)
/*
1. It must be an object method.
2. The returned value must be of the same type as the member variable.
3. The method name must be a member variable. Remove the underline.
4. It must be a null parameter.
*/

(Int) age;

// Setter Method

// When accessing member variables in object methods, a pointer is hidden.
/*
If the method does not have the same variable name as the member variable name, the compiler automatically adds a self pointer to it during compilation.
If there is a local variable with the same name as the member variable, the name is a local variable, and the compiler will not add the self pointer to it.
*/

/**
Anonymous object: No Name object
1. Anonymous objects are used only once.
Use Cases:
1. When we call a method of an object once, we can use an anonymous object.
2. The anonymous object can be used as the actual parameter of the function.
*/
*
Dependency: Object A is A local variable of object B or A method parameter. object B depends on Object A and object A. At this time, we call A dependency between object B.

________________________See the following
In object-oriented design:
Coupling Degree: the degree of impact on another object when an object is modified
Low coupling: when an object is modified, the impact on other objects is relatively small.
High Cohesion: an object only performs its own tasks.
Object-Oriented Design Principle: single Responsibility Principle

Import

Import

Related Article

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.