iOS written question four

Source: Internet
Author: User


What's the difference between #import and # include, @class? What's the difference between #import<> and #import?

#import和 # include can contain the contents of a file completely, #import能防止同一个文件被包含多次

@class simply declares a class name and does not contain a complete declaration of the class; @class can also solve the problem of loop containment

#import <> used to include the file that comes with the system, #import "" to include the custom file

Attribute readwrite,readonly,assign,retain,copy,nonatomic What is the role of each, in that case use?

ReadWrite: Simultaneous generation of both the Get method and the set method declaration and implementation

ReadOnly: Generate only declarations and implementations of Get methods

The implementation of the Assign:set method is a direct assignment for the basic data type

The implementation of the Retain:set method is the release old value, retain new value, for OC object type

The implementation of the Copy:set method is the release old value, copy new value, for NSString, block and other types

Nonatomic: Non-atomic, the implementation of the set method is not locked (higher than atomic performance)

Write a setter method for completing the @property (nonatomic,retain) NSString *name, and write a setter method for completing @property (nonatomic,copy) nsstring *name.

@property (nonatomic, retain) NSString *name;

-(void) SetName: (NSString *) name

{

if (_name! = name) {

[_name release];

_name = [name retain];

}

}

@property (nonatomic, copy) NSString *name;

-(void) SetName: (NSString *) name

{

if (_name! = name) {

[_name release];

_name = [name copy];

}

}

For statement nsstring*obj = [[NSData alloc] init]; , what is the type of compile-time and run-time obj respectively?

Compile-time is nsstring type

Run-time is a nsdata type

What are the common object-c data types, and what is the difference between the basic data types of C?

Commonly used OC types: NSString, Nsarray, Nsdictionary, NSData, NSNumber, etc.

OC objects need to manage memory manually, C's basic data type does not need to manage memory

What are the characteristics of the variables declared by ID?

The variable that the ID declares can point to any OC object

Objective-c How to manage the memory, say your opinion and solve the method?

Each object has a reference counter, and the counter for each new object is 1, and when the object's counter is reduced to 0 o'clock, it is destroyed

By retain can let the object counter + 1, release can let the object counter-1

You can also manage memory through the Autorelease pool

If arc is used, the compiler automatically generates code to manage memory

What are the several principles of memory management? Follow the default rules. Which methods generate objects that need to be released manually? How to effectively avoid memory leaks when combined with property?

Whenever a alloc, copy, new method is called to produce a new object, it must be called at the last release or Autorelease

Whenever retain is called, either release or autorelease must be called at the end.

@property If you use Copy or Retian, you need to do a release operation on the attributes that you no longer use.

If arc is used, another discussion

Look at the following program, three times NSLog will output what? Why?

nsmutablearray* ary = [[Nsmutablearray array] retain];

NSString *str = [nsstring stringwithformat:@ "test"]; 1

[Str retain]; 2

[Ary Addobject:str]; 3

NSLog (@ "%d", [str retaincount]);

[Str retain]; 4

[STR release]; 3

[STR release]; 2

NSLog (@ "%d", [str retaincount]);

[Ary Removeallobjects]; 1

NSLog (@ "%d", [str retaincount]);

Results: 3, 2, 1

What is the method for creating threads in OC? If you specify code execution in the main thread? How to delay code execution?

Methods for creating threads

Nsthread

Nsoperationqueue and Nsoperation

GCD

Executing code in the main thread

[Self performSelectorOnMainThread:withObject:waitUntilDone:];

[Self performselector:onthread:[nsthread mainthread] Withobject:waituntildone:];

Dispatch_async (Dispatch_get_main_queue (), ^{

});

Delay execution

Double delayinseconds = 2.0;

dispatch_time_t poptime = Dispatch_time (Dispatch_time_now,

(int64_t) (Delayinseconds * nsec_per_sec));

Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {

});

[Self performSelector:withObject:afterDelay:];

[Nstimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:];

iOS written question four

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.