iOS Development Knowledge Point Supplement

Source: Internet
Author: User

One: The difference between self class,self superclass super class Super Superclass

New Subperson inherits person, which is printed in Subperson as follows:

NSLog (@ "%@%@%@%@", [Self-class], [self-superclass], [Super class], [Super superclass]), printing results:

Subperson person Subperson Person

Self-Subperson,

Class: Gets the classes of the current method caller, [self class] refers to the class of the current method call

Superclass: Gets the parent class of the current method caller

Super: Just a compilation indicator, which is for the compiler to see, not a pointer

Essence: As soon as the compiler sees the super flag, it will let the current object call the parent class method, essentially the current object in the call

Print in parent class Person:

NSLog (@ "%@%@%@%@", [Self-class], [self superclass], [Super class], [Super superclass]);

The result is:

Subperson person Subperson Person

Second: Bugs encountered in the project

1: Duplicate import file will error:

Duplicate represents a repeating definition, _objc_class, followed by a repeating defined class, workaround: Delete the duplicate defined class to

2: Sometimes the following error will occur if the target is not checked

If there is undefined, it means undefined, that is, the person does not participate in the compilation: solution:

Click on the number to add the. m files that are not involved in the compilation to the target.

Three: Const,static,extern Introduction

# # # One, the difference between the const and the macro (interview questions):

* ' Introduction to const ': previously commonly used string constants, usually pumping macro, but Apple does not recommend us to smoke macro, we recommend the use of const constants.

* ' Compile time ': Macros are pre-compiled (processed before compilation), const is the compile phase.

* ' Compile check ': Macros do not check, do not report compilation errors, just replace, const will compile the check, will report compilation errors.

* ' Macro benefits ': Macros can define a number of functions, methods. Const cannot.

* ' Macro disadvantage ': using a large number of macros, it is easy to compile for a long time, need to replace each time.

Note: Many blogs say that using macros, will consume a lot of memory, I this validation does not generate a lot of memory, macros are defined as constants, constants are placed in the constant area, will only generate a copy of memory.

```

Common constants: a macro

#define XMGACCOUNT @ "Account"

#define Xmguserdefault [Nsuserdefaults Standarduserdefaults]

String constants

Static NSString * Const ACCOUNT = @ ' account ';

-(void) Viewdidload {

[Super Viewdidload];

Preferences Storage

Using macros

[Xmguserdefault setvalue:@ "123" forkey:xmgaccount];

Using const constants

[[Nsuserdefaults Standarduserdefaults] setvalue:@ "123" forkey:account];

}

```

# # # Two, Const action: restriction type

* 1.const is only used to modify the right variable (basic data variable p, pointer variable *p)

* 2. Const-Modified variables are read-only.

* ' Const basic use '

```

-(void) Viewdidload {

[Super Viewdidload];

Defining variables

int a = 1;

Allow modification of values

A = 20;

Two usages of const

Const: Modifier basic Variable P

The two formulations are the same, and the const only modifies the basic variable B on the right.

const int b = 20; B: Read-only variables

int const B = 20; B: Read-only variables

Modifying values is not allowed

b = 1;

Const: modifier pointer variable *p, variable with *, is pointer variable.

Defines a pointer variable that points to the int type, pointing to the address of a

int *p = &a;

int c = 10;

p = &c;

Allows you to modify the address that P points to,

Allows modifying the value of p to access memory space

*p = 20;

The const modifier pointer variable accesses the memory space, decorated with the right *p1,

Both ways

const int *P1; *P1: constant p1: Variable

int const *P1; *P1: constant p1: Variable

Const modifier pointer variable p1

int * const P1; *P1: variable P1: constant

The first const modifier *P1 the second const modifier P1

Both ways

const int * const P1; *P1: constant P1: constant

int const * Const P1; *P1: constant P1: constant

}

```

# # # Three, the use of const development scenarios:

* 1. When a method parameter is read-only

* 2. Define read-only global variables

```

@implementation Viewcontroller

To define a read-only global constant

NSString * Const STR = @ "123";

When a method is a parameter, read-only.

-(void) Test: (NSString * const) name

{

}

Pointer is read-only and cannot be modified by pointer

-(void) Test1: (int const *) a{

*a = 10;

}

Basic data type read-only

-(void) TEST2: (int const) a{

}

@end

```

# # # Four, static and extern simple to use (to use a thing, first understand its role)

* ' static action ':

* Modify Local variables:

1. Extend the life cycle of a local variable, and the program ends before it is destroyed.

2. Local variables generate only one copy of memory and are initialized only once.

* Modify Global variables

1. Can only be accessed in this file, modify the scope of global variables, the life cycle will not change

* ' extern effect ':

* just used to get the values of global variables (including global static variables) and cannot be used to define variables

* ' extern working principle ':

* First in the current file to find there are no global variables, not found, will go to other files to find.

```

Global variables: Only one copy of memory, all file shares, used in conjunction with extern.

int a = 20;

static modifier global variable

static int age = 20;

-(void) test

{

static modifier local variable

static int age = 0;

age++;

NSLog (@ "%d", age);

}

-(void) Viewdidload {

[Super Viewdidload];

Additional setup after loading the view, typically from a nib.

[Self test];

[Self test];

extern int age;

NSLog (@ "%d", age);

}

I

```

# # # Five, static and const combined use

* static and CONST functions: Declaring a read-only static variable

* Development Usage Scenarios: string constants that are frequently used in ' a file ' can be combined with static and const

```

Static modified global variables used in development, only changing scope

Why change the scope of global variables to prevent duplicate declarations of global variables.

The global variables declared in development, some do not want outside changes, only allow reading.

Like a basic data type that you don't want others to change.

Declares a static global read-only constant

static const int a = 20;

The role of Staic and const unions: Declaring a static global read-only constant

Staic and const common usage scenarios in iOS are used instead of macros to define a frequently used string constant as a static global read-only variable.

The key modification value is often obtained in development, so the Const modifier key means that key is read-only and no modification is allowed.

Static NSString * Const KEY = @ "name";

If const modifies *key1, which means *key1 is read-only, Key1 can still be changed.

static NSString Const *KEY1 = @ "name";

```

# # # VI, extern and const combined use

* use scenarios in development: the same string constants used frequently in ' Multiple files ' can be combined with extern and Const.

* Reason:

* Static and const combinations: you need to define a static global variable in each file.

* extern and const combination: only need to define a copy of global variables, multiple file shares.

* Global constant formal notation: in the development of easy to manage all the global variables, usually engage in a globeconst file, which specifically defines global variables, unified management, or project files more difficult to find.

* GlobeConst.h

```

/******************************* Home ****************************/

extern NSString * Const NAMEKEY = @ "name";

/******************************* Home ****************************/

```

* GLOBECONST.M

```

#import <Foundation/Foundation.h>

/******************************* Home ****************************/

NSString * Const NAMEKEY = @ "name";

/******************************* Home ****************************/

```

iOS Development Knowledge Point Supplement

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.