Three. Creation and use of OC Foundation--1.nsstring, 2-File development, 3-class method, 4 package

Source: Internet
Author: User

Three: Oc--1. The creation and use of NSString,

1, create a constant string, and note the "@" symbol.
NSString *astring = @ "This is a string!"; The following is a constant, preceded by a pointer variable

2,nsstring *s1 = [NSString new];//no meaning
S1 [email protected] "jian le ma";

3, the third method of creating a string
Format creates a string (creates a string in the specified format)
NSString *imgname = [NSString stringwithformat:@ "xxxxxx%02d.jpg", I];

4. Create a new string with a string that already exists
NSString *s2 = [[NSString alloc] initwithstring:s1];


5.NSString length calculation method.
NSString *str = @ "Ban Long";
[STR length]; 4

More than 2 file development,

1. Why use multiple files?
The functionality implemented in development is generally more complex, so the classes are divided into declarations (. h files) and implementation (. m files) sections,
Write in the. h file: Declaration of member variables and methods, write in. m files: Implementation of member variables and methods.
This writing greatly improves the development efficiency and improves the maintainability of the code.

Class 3 method,

A. Features:
1. Start with a plus sign (+),
2. Calling format: [class name Method name]

Two. Precautions:
1. Class methods can have the same name as instance methods and do not affect each other

2. Class methods can inherit from the parent class, subclasses can override class methods//, such as the Description class method

3. Class methods and instance methods are declared in interface and implemented in implementation.

4. The class method is called by the class name, and the instance method is called by the object.

5. Self is used in the class method, and self refers to the caller class

6. You cannot use member variables of a class in a class method

7. The class method and the object method cannot be called with self to each other

Summary self: Who calls the current method, self represents who


Three. Comparison of class methods and Object methods
1. Object methods start with a minus sign, you can use member variables
2. Class methods start with a plus sign and cannot use member variables

Four. Class method Usage Scenarios
If we call a method that does not need to use a member variable, then we can declare the method as a class method


Five. Anonymous objects
Usage Scenarios for anonymous objects: When the method of an object is called only once, you can use an anonymous object to complete it.

The advantages and disadvantages of anonymous objects:
Advantages: Can simplify the code, convenient method call; Save memory space
Disadvantage: After the anonymous object is instantiated, only the member variable can be used correctly once;

4 Package

I. What is encapsulation:
1. Encapsulation refers to the properties and implementation details of hidden objects, providing public access only to external

Two. Benefits of encapsulation:
1. Isolate the changes. (no matter how it changes, the user does not need to know)
2. Easy to use. (Users only need to follow a certain method to use it)
3. Improve reusability. (Anyone can operate on a packaged feature)
4. Improve security. (because exposing to the user is just some external operation, not the essence of the inside, so it improves the maintenance of him, that is, security)

Three. Principles of encapsulation:
1. Hide content that does not need to be provided externally.
2. Hide the properties and provide public access to them.


Four. Implementation of Setter method
Writing specification for Set methods:
1) The Set method must be an object method
2) The Set method must not have a return value
3) The Set method must start with set
4) set followed by the instance variable minus the underscore, and the first letter capitalized
5) The Set method must have parameters, and the type of the parameter is consistent with the type of the instance variable
6) The formal parameter name of the set method is usually to remove the instance variable name of the next loop
7) Parameter name cannot have the same name as instance variable name
8) in the implementation of the Set method, be sure to assign values to the instance variables with the formal parameters

Member variable: int _age; The Setter method
-(void) Setage: (int) age{

_age = age;
}

Get method Writing Specification:
1) The Get method must be an object method
2) The Get method must have a return value, the type of the return value and the type of the instance variable to be consistent
3) The method name of the Get method is to remove the instance variable name from the next loop
4) Get method must have no parameters
5) in the implementation of the Get method, the value of the instance variable must be returned

Member variable: int _age; The Setter method
-(int) age{
return _age;
}

Three. Creation and use of OC Foundation--1.nsstring, 2-File development, 3-class method, 4 package

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.