iOS interview (4)

Source: Internet
Author: User

1.id, what does nil represent?

Id

The ID and void * are not exactly the same. In the above code, the ID is a pointer to the struct objc_object, which basically means that the ID is a pointer to any object that inherits the class of object (or NSObject). It is important to note that the ID is a pointer, so you do not need to add an asterisk when using the ID. For example, ID foo=nil defines a nil pointer that points to an arbitrary subclass of NSObject. The ID *foo=nil defines a pointer to another pointer, pointing to a subclass of NSObject.

Nil

Nil is the same as NULL in the C language, as defined in objc/objc.h. Nil represents a Objctive-c object with a pointer to null (Nothing is empty).

2. Memory management autorelease, retain, copy, assign set method and meaning?

1, you Initialize (Alloc/init) The object, and you need to release it (release).

For example: Nsmutablearray aarray = [[Nsarray alloc] init], after which [Aarray release] is required;

2, you retain or copy the, you need to release it.

For example: [Aarray retain], need [Aarray release];

3, the object that is passed (assign), you need to consider the retain and release.

For example: Obj2 = [[Obj1 somemethod] autorelease];

Object 2 receives an automatically freed value of object 1, or passes a base data type (nsinteger,nsstring): You or you want to retain object 2 to prevent it from being automatically freed before it is used. But after retain, be sure to release it at the right time.

Questions about index count (Reference counting)

Retain value = Index count (Reference counting)

The Nsarray object retain the object in any array (retain value plus one). When Nsarray is unloaded (Dealloc), all objects in the array are freed once (the retain value is reduced by one). Not only Nsarray, but any collection class (Collection Classes) performs a similar operation. such as nsdictionary, or even uinavigationcontroller.

Alloc/init the object that is created, the index count is 1. No need to retain it again.

Methods such as [Nsarray array] and [NSDate Date] Establish an object with an index count of 1, but it is also an automatic release object. So it's a local temporary object, so it doesn't matter. If you are a variable (IVAR) that you intend to use in a full class, you must retain it.

The default class method return value is executed with the "auto-release" method. (* Nsarray in the above)

In the Unload method "Dealloc" in the class, release all the NS objects that are not balanced. (* All not autorelease, while retain value is 1)

4. What is the role of the category?

Sometimes we need to add some methods to a class that has already been defined, rather than rewriting the class. For example, when the project is already large, the amount of code is greater, or there are many methods already wrapped in the class, and other code has called the class to create the object and use the method of the class, you can use the class to extend the new method.

Note: Categories can only extend methods, not member variables.

Delegate (for example)

The principal agent (Degegate), as the name implies, entrusts something to other objects to do. Then the other object is the agent of this object, instead of it to take care of what to do. Reflected in the program, the first thing to know is the object of the client is which object, the delegate is what the content.
Entrust mechanism is a design pattern, used in many languages, this is only a general idea, there will be a lot of information on the Internet.
So in the Apple development process, with the implementation of the delegation of the idea of the program as follows, I mainly take how to transfer information between views to do an example.
For example: in two pages (Uiiview view object) Implementation of the value, with the delegate (delegate) can do well!
Method:
Class A
@interface A:uiview
ID transparendvaluedelegate;
@property (nomatic, retain) ID transparendvaluedelegate;
@end

@implemtion A
@synthesize transparendvaluedelegate
-(void) Function
{
nsstring* value = @ "Hello";
Let proxy object perform Transparendvalue action
[Transparendvaluedelegate Transparendvalue:value];
}
@end

Class B
@interface B:uiview
nsstring* value;
@end

@implemtion B
-(void) Transparendvalue: (nsstring*) Fromvalue
{
value = Fromvalue;
NSLog (@ "The value is%@", value);
}
@end

The following settings a proxy delegate object is b
At defining A and Class B objects:

* a = [[a alloc] init];
b* B = [[B alloc] init];
A. transparendvaluedelegate = b;//Set Object A proxy is object B

This allows the values to be passed between views A and B through a delegate!

The following example delegate has two categories:
1. The proxy object of a view class object is the parent view, and the child view uses the proxy implementation to have the parent view display a different child view;
2. One child view under the same parent view is a proxy object for another child view, and another child view changes its background color to the given color;
===============================================

iOS interview (4)

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.