Powerful iOS face questions

Source: Internet
Author: User

online wandering to see the strength of the question list, ask others ask themselves are good dry goods, saved. Here are the topics

[※] What are the property keywords in the @property?

Property-Related keywords, you can follow some features to classify thread-related

Atomic: The default is this property, the function is in multi-threaded case, the compiler automatically generate the corresponding thread security code, to achieve the purpose of mutual exclusion lock, to avoid the corresponding variable read and write out of sync problem.
Nonatomic: This keyword can be used to improve performance and efficiency when the variable does not need to consider multithreading. The

Atomic is primarily used for thread protection, which prevents data errors when one thread accesses a single address to prevent another thread from accessing the address at the same time. As we all know, the thread lock consumes system resources, so it is recommended to use nonatomic if there is no thread-related content in practice.
In most iOS apps today, Nonatomic is also widely used for performance-based considerations.

reads and writes about the

ReadWrite: Default type, which automatically generates the appropriate read and write methods.
ReadOnly: Only the corresponding Get method is generated, not the set method.

Primary Purpose: Controls access to this property by an external class.

Memory Reference related

There are two classes based on the system version, one for iOS 5.0 and one for iOS 5.0 after joining the arc mechanism.

Arc before

Assgin: The default type, which is directly assigned in the set method, directly points to an address without any changes to the reference count. In the
Retain:set method, the object before the (release) is first disposed, that is, the reference count for the old object is removed, and the address of the new object is pointed directly, and the reference count of the new object is added one. The
Copy:set method, like retain, first release the old object, and then copy the new object, essentially using the new memory space and address, the reference count is unique, and the contents of the target object are copied over. Can be considered a copy of the content.

Arc after

Weak: weak reference, similar to Assgin, except that when the object is released, the weak type is automatically empty, and the pointer is set to nil, avoiding the wild pointer.
Strong: Strong reference, default type, similar to retain. The

Memory count keyword is OBJC primary content.

[Does the ※]weak attribute need to be nil in dealloc?

No, you can refer to the answer above, the weak type property automatically sets the pointer to nil after the object is released, preventing the wild pointer.

[※※] What is the role of @synthesize and @dynamic respectively?

@synthesize

This keyword automatically generates the set and Get methods during compilation. When a set or Get method is customized, the compiler production method is automatically overwritten.

@dynamic

The keyword does not automatically generate the set and get methods, and it avoids compilation warnings, but we need to implement the set and get methods ourselves, typically using dynamic binding or some special definition of read and write methods.

[※※※] Arc, what are the default keywords when no property keywords are explicitly specified?

The default keywords under arc are: Atomic,weak,readwrite.
The default keywords under non-arc are: Atomic,retain,readwrite.

In fact, it is thread-related, memory-related, read-write related.

[※※※] The Copy keyword is frequently used by nsstring (or nsarray,nsdictionary) declared with @property, why? If you use the strong keyword, what might be causing the problem?

The main consideration here is the Copy keyword usage scenario, first of all to understand the nature of the Copy keyword, copy is the equivalent of a copy of the content, its pointer address and memory space are different from the copied object, but the content is the same. Then the usage scenario for the Copy keyword is:

Use the Copy keyword when you need to modify the contents of an assigned object, and you cannot modify the contents of the original object.

Back to the original question, NSString, Nsarray, and so on are all structure classes used to encapsulate data, and objects of these classes often need to be modified in content. Therefore, to avoid changes to the contents of the original assignment object, but only to modify the contents of the object in the current class, you need to use the Copy keyword. Of course, if you are sure that you do not need to modify the object content during use, it is possible to use retain or strong based on the ARC environment, and to a certain extent, reduce the use of memory.

[※※※] What are the rules @synthesize synthetic instance variables? If the property is named Foo and there is an instance variable named _foo, will the new variable be automatically synthesized?

The following content is based on personal understanding and experience.

The rules for @synthesize synthetic instance variables are:

The Get and set methods for the specified instance are automatically generated in the implementation file (. m file) of the code.

If Ivar declarations are not defined in the code's declaration file (. h file), in the current version of Xcode 6.0, as long as the property type is defined and no @synthesize keyword is required, the compiler dynamically creates the corresponding Ivar variable, such as: @synthesize foo = _foo; .
Prior to the Xcode version does not dynamically generate the corresponding composition code, need to hand-written, the complete wording is as follows:

. h file
#import <Foundation/Foundation.h>@interface TestClass : NSObject{    NSObject *_object;}@property (nonatomic, retain) NSObject *object;@end
. m file
#import "TestClass.h"@implementation TestClass@synthesize object = _object; //这里写成@synthesize object;也是可以的,但是相应的属性名称和ivar名称就是相同的了。在调用时,如果直接写object,就是ivar调用;如果是self.object则是调用get方法。@end
When there is an instance variable named _foo, will the new variable be automatically synthesized?

No.


Programmers are warning.

[※※※※※] What other usage scenarios do @synthesize have after the auto-compositing attribute instance variable?

Here's an answer that looks quite right.

http://stackoverflow.com/questions/19784454/when-should-i-use-synthesize-explicitly

Specifically, this is:

Do not automatically generate the place, and do not want to write the get and set method, just use this.

[What happens when a message is sent to a nil object in ※※]OBJC?]

Nothing will happen. The answer is very detailed:

http://blog.csdn.net/hanangellove/article/details/45013581

The specific reasons are as follows:

OBJC are dynamic languages, and each method is dynamically converted to a message at run time, i.e.: objc_msgSend(receiver, selector) .

So, to facilitate understanding of this content, or paste a OBJC source code:

Runtime.h (Definition of class in runtime)
  struct Objc_class {class ISA objc_isa_availability;//isa pointer to Meta class, because the OBJC class itself is also an object, in order to handle this relationship, Runtime created the Meta class, and when the message was sent to the class [NSObject alloc], the message was actually sent to class Object #if!__objc2__ class Super_class objc2_ unavailable; Parent Class const char *name objc2_unavailable; Class name long version objc2_unavailable; The version information for the class, which defaults to 0 long info objc2_unavailable; Class information for the run-time use of some bit ID long instance_size objc2_unavailable; The instance variable size of the class struct objc_ivar_list *ivars objc2_unavailable; The member variables of this class are linked list struct objc_method_list **methodlists objc2_unavailable; Method definition of the linked list struct objc_cache *cache objc2_unavailable;  Method cache, the object to a message will look for the message object according to the ISA pointer, which will be traversed in method lists, if the cache, the usual method calls can improve the efficiency of the call. struct Objc_protocol_list *protocols objc2_unavailable; Protocol linked list #endif} objc2_unavailable;  

OBJC when sending a message to an object, the runtime library finds the class that the object actually belongs to, based on the object's ISA pointer, then looks for the method in the list of methods in the class and its parent class method, and then when the message is sent, the Objc_msgsend method does not return a value. The so-called return content is executed at the time of the specific invocation.
So, back to the point, if you send a message to a nil object, the first time you look for the object's ISA pointer is the 0 address returned, so there is no error.

[What is the relationship between sending a message to an object in ※※※]OBJC [obj foo] and the Objc_msgsend () function?

The previous question: [obj foo]; when the OBJC is dynamically compiled, it will be converted to: objc_msgSend(obj, @selector(foo)); .

When will [※※※] report unrecognized selector's abnormality?

There is an article here that is very clear.

http://tech.glowing.com/cn/objective-c-runtime/

The simple process is as follows, as mentioned in the previous question:

OBJC are dynamic languages, and each method is dynamically converted to a message at run time, i.e.: objc_msgSend(receiver, selector) .

OBJC when sending a message to an object, the runtime library finds the class to which the object actually belongs based on the object's Isa pointer, and then looks for methods in the list of methods in that class and its parent class method list, if the corresponding method is still not found in the topmost parent class. The program will hang up at run time and throw an exception unrecognized selector sent to XXX . But before that, OBJC's runtime will give you the chance to crash the rescue program three times:

    1. Method resolution
      The OBJC runtime invokes +resolveInstanceMethod: or gives +resolveClassMethod: you the opportunity to provide a function implementation. If you add a function and return YES, the runtime restarts the process of sending the message, and if the Resolve method returns NO, the runtime moves to the next step, message Forwarding (msg Forwarding).
    2. Fast forwarding
      If the target object is implemented -forwardingTargetForSelector: , Runtime calls this method, giving you the chance to forward the message to other objects.
      As long as this method returns not nil and self, the entire message sending process is restarted and of course the sent object becomes the object you return. Otherwise, you will continue to Normal fowarding.
      This is called fast, just to differentiate the next forwarding mechanism. Because this step does not create any new objects, the next forwarding creates a Nsinvocation object, so it is relatively faster.
    3. Normal forwarding
      This step is the last time the runtime gives you a chance to save. First it sends -methodSignatureForSelector: a message to get the function's arguments and return value types. If -methodSignatureForSelector: you return nil,runtime, the message will be sent and the program will be dead -doesNotRecognizeSelector: . If a function signature is returned, runtime creates a Nsinvocation object and sends a -forwardInvocation: message to the target object.

[※※※※] How does a OBJC object perform memory layout? (Consider the case of a parent class)

[※※※※] What does the pointer to ISA for a OBJC object point to? What's the effect?

[※※※※] What does the following code output?

@implementation Son : Father- (id)init{    self = [super init];    if (self) {        NSLog(@"%@", NSStringFromClass([self class]));        NSLog(@"%@", NSStringFromClass([super class]));    }    return self;}@end

[※※※※]runtime how to find the corresponding IMP address via selector? (Consider class methods and instance methods, respectively)

[※※※※] objects associated with the runtime associate method need to be released when the main object Dealloc?

[What are the essential differences and linkages between class methods and instance methods in ※※※※※]OBJC?]

[What does the ※※※※※]_objc_msgforward function do, and what does it happen to call it directly?]

[How does ※※※※※]runtime implement the weak variable's auto-nil?

[※※※※※] Can I add an instance variable to a compiled class? Can I add an instance variable to a class that is created at run time? Why?

[What is the relationship between ※※※]runloop and threads?]

[What is the mode role of ※※※]runloop?]

[※※※※] to +scheduledtimerwithtimeinterval ... The way the timer triggers the list on the sliding page, the timer will tentative callback, why? How to solve?

[※※※※※] Guess how the Runloop interior is implemented?

[What mechanism does ※]OBJC use to manage object memory?]

[※※※※] How does arc help developers manage memory?

[※※※※] Without manually specifying the Autoreleasepool, what time is a Autorealese object released? (for example, created in a VC viewdidload)

[※※※※] Under what circumstances does the bad_access appear?

[※※※※※] How did Apple achieve autoreleasepool?

[※※] What happens when a reference loop is used when using block, and how is it resolved?

[※※] How do i modify the block external variables within a block?

If [※※※] uses some of the system's block APIs (such as UIView's block version to write animations), is the reference loop issue also considered?

[※※] What are the two types of GCD queues (dispatch_queue_t)?

[※※※※] How to synchronize several asynchronous calls with GCD? (such as loading multiple pictures asynchronously based on several URLs, and then compositing an entire image after the download is complete)

[What is the role of ※※※※]dispatch_barrier_async?]

[※※※※※] Why did Apple abandon dispatch_get_current_queue?

[※※※※※] What is the result of running the following code?

- (void)viewDidLoad{    [super viewDidLoad];    NSLog(@"1");    dispatch_sync(dispatch_get_main_queue(), ^{        NSLog(@"2");    });    NSLog(@"3");}

[※※]addobserver:forkeypath:options:context: What are the roles of each parameter, and which method do you need to implement in observer to get the KVO callback?

[※※※] How to manually trigger a value of KVO

[※※※] If a class has an instance variable NSString *_foo, call Setvalue:forkey: Can I use Foo or _foo as key?

[※※※※] How is the set operator in KVC KeyPath used?

[※※※※] KVC and Kvo's keypath must be a property?

[※※※※※] How do I turn off the default implementation of the default KVO and go into a custom KVO implementation?

[※※※※※]apple in what way to achieve the kvo of an object?

[※※] Why can iboutlet view properties be set to weak?

[※※※※※] How does the user Defined Runtime attributes in IB work?

[※※※] How to debug bad_access errors

[※※※]lldb (GDB) Common debug commands?

http://www.cnblogs.com/qq449832375/

Powerful iOS face questions

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.