IPhone interview questions

Source: Internet
Author: User

1. Write a delegate interface.

@ Protocol mydelegate

@ Class myclass: nsobject {

ID <mydelegate> delegate _;

}

@ End

@ Protocol mydelegate <nsobjec>

-(Void) myfunction :( nsinteger) ARGs;

@ End

2. Write an nsstring class, + (ID) initwithcstring :( const char *) nullterminatedcstring encoding :( nsstringencoding) encoding.

+ (ID) stringwithcstring :( const char *) nullterminatedcstring encoding :( nsstringencoding) encoding {

Nsstring * result;

Result = [self allocwithzone: nsdefamalmalloczone];

Result = [result initwithcstring: nullterminatedcstring encoding: encoding];

Return autorelease (result );

}

3. Does obj-C have multiple inheritance types? If no, what should I replace it?

Most of the classes in cocoa are nsobejct subclasses, which do not have multiple inheritance. Multi-inheritance is implemented by using a protocol delegate proxy.

4. Is there a private Method for obj-C? What about private variables?

There are only two methods in the obj-C Class: static method and instance method. According to the OO principle, only useful things are exposed for an object. If there is no private method, it is not easy to reuse some small-scale code. Declare a private method in the class:

@ Interface DEMON: nsobject {

Nsstring * smoething;

}

+ (Void) thisisastaticmethod;

-(Void) thisaninstancemethod;

@ End

 

@ Interface demon (private)

-(Void) thisisprivatemethod;

@ End

@ Private can be used to modify private variables. In obj-C, all instance variables are private by default, and all instance methods are shared by default.

5. # What is the difference between import and # include? What about @ class?

@ Class is generally used when the header file needs to declare an instance variable of this class. In the M file, you still need to use # Import

Compared with # include, # import does not cause cross-compilation.

6. Functions of readwrite, readonly, assign, retain, copy, and nonatomic attributes

@ Property is an attribute access statement. The extension number supports the following attributes:

(1) getter = gettername, setter = settername, set the method names of setter and getter

(2) readwrite, readonly, set the available access level

(3) assign and setter methods directly assign values without retain operations. To solve the problem of original type and cycle reference

(4) The retain and setter Methods perform the release old value for the parameter and retain the new value. All implementations are in this Order (relevant information is available on CC)

(5) the copy and setter Methods perform the copy operation. Like the retain process, the old value is release first, and then the new object is copied. The retaincount is 1. This is a mechanism introduced to reduce context dependencies.

(6) nonatomic, non-atomic access, without synchronization, multi-thread concurrent access will improve performance. Note: If this attribute is not added, both access methods are atomic transaction access by default. The lock is added to the object instance level.

7. Advantages and Disadvantages of obj-C

Advantages of objc:
1) cateogies
2) posing
3) Dynamic Identification
4) indicator calculation
5) elastic message transmission
6) Not an overly complex C-derivative Language
7) Objective-C and C ++ Mixed Programming

Disadvantages:
1)Name Space is not supported
2)Operator Overloading is not supported.

3) Multi-inheritance is not supported.

4) use the dynamic runtime type. All methods are called by functions, so many compilation optimization methods are not used. (Such as inline functions), poor performance8. What is the automatic release pool and how it works?

When you send an autorelease message to an object, cocoa puts a reference of this object into the latest Auto Release pool. It is still a legitimate object, so other objects in the scope defined by the automatic release pool can send messages to it. When the program is executed at the end of the scope, the automatically released pool will be released, and all objects in the pool will be released.

1.Ojc-C manages the memory by using a "referring counting" (reference count) method. When an object starts to allocate memory (alloc), the reference count is one, in the future, when there is a copy or retain, the reference count will be added. When the release and autorelease parameters are met, the reference count will be reduced by one. If the Count of this object is changed to 0, will be destroyed by the system.

2. The NSAID utoreleasepool is used to manage the reference count. You don't need to worry about this.

3. There is no difference between autorelease and release, but the timing for reducing the reference count by one is different. autorelease will reduce the reference count by one when the object is actually used.9. How to mix C and obj-C

1) when the obj-C compiler processes files with a suffix of m, it can recognize the code of obj-C and C. When processing mm files, it can recognize obj-C, C, c ++ code, but the CPP file must only use C/C ++ code, and the obj-C code cannot appear in the header file of the CPP File Include, because CPP is only CPP

2) Mix CPP in the MM file and use it directly. Therefore, obj-C Mixed CPP is not a problem.

3) Mixing obj-C in CPP is actually what we want to write modules using obj-C.

If the module is implemented as a class, write the class definition according to the CPP class standard. The header file cannot contain obj-C, including # import cocoa. In the implementation file, that is, the implementation code of the class can use obj-C and can be imported, but the suffix is mm.

If the module is implemented by functions, the header file should declare the function in the C format. In the implementation file, the C ++ function can use obj-C internally, but the suffix is still mm or M.


Summary: as long as the CPP file and the CPP include file do not contain obj-C, you can use it. The key to mixing CPP with obj-C is to use interfaces, instead, you cannot directly use the implementation code. In fact, CPP is a mix of the O files compiled by obj-C. This is actually the same, so it can be used. The obj-C compiler supports CPP.

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.