Li Hongqiang iOS Classic face question 30-a large-size face question

Source: Internet
Author: User
Tags gcd list of attributes

Li Hongqiang iOS Classic face question 30-a large-size face question

To examine the basis of an interviewer, basically asking a @property is enough:

What modifiers can be followed @property?
    • Thread-Safe:
      • Atomic,nonatomic
    • Access Permissions for
      • Readonly,readwrite
    • Memory Management (ARC)
      • Assign,strong,weak,copy
    • Memory Management (MRC)
      • Assign,retain,copy
    • Specify Method Name
      • Setter=
      • Getter=
What is the difference between using the weak keyword and comparing assign? Like what:
    • In arc, there must be one end to use weak when there is a circular reference, such as: Custom View proxy Properties
    • It has already been strongly applied to it, it is not necessary to use a strong reference once, the weak is also used, the child control properties of custom view are generally used weak, but B is also available strong
    • Weak when the object is destroyed, the pointer is automatically set to nil, and assign does not * Assigin can use non-OC objects, and weak must be used for OC objects
How do I use the Copy keyword?
    • For properties of strings and blocks, you typically use the copy
    • The string uses copy to change the contents of the string externally, affecting the property
    • Block using copy is a legacy of the MRC, in the MRC, the block inside the method is in the stack area, and copy can be used to place it in the heap area. Use copy or strong effect for block in ACR
What's wrong with this notation: @property (copy) Nsmutablearray *array;
    • When you add, delete, or modify elements in an array, the program crashes because the method is not found. Because copy is copying an immutable Nsarray object
How do I make my class use the copy modifier?
    • Are you saying that my class also supports copy functionality?
    • If the interviewer says yes:
      • Compliance with Nscopying Protocol
      • Implementation-(ID) Copywithzone: (Nszone *) zone; Method
    • If the interviewer says no, how to use copy in attributes
      • Copy is generally used when using strings and blocks
How do I rewrite the setter with the Copy keyword?
    • When overriding the copy setter method, be sure to call the copy method of the incoming object and then the member variable that corresponds to the method assigned to the setter
This set of problems is relatively large, if the above questions can be answered correctly, can be extended to ask a deeper point: what is the nature of the @property? How Ivar, Getter, setter are generated and added to this class
    • In the ordinary OC object, @property is to compile it automatically to help us generate a private member variable and setter and getter Method declaration and implementation
    • In order to understand how the property is implemented, has been anti-compilation of related code, he roughly generated five of things
    • objc_ivar_$ class Name $ Property name offset of this property
    • implementation function corresponding to setter and getter method
    • Ivar_list is the list of member variables
    • Method_list Method List
    • Prop_list Property List
      In other words, each time we add a property, the system adds a description of the member variable in the Ivar_list, adds a description of the setter and getter methods in the Method_list, and adds a description of the property in the attribute list, It then calculates the offset of the property in the object, then extends the implementation of the setter to the Getter method, assigns a value from the position of the offset in the Setter method method, starts from the offset in the getter method, and, in order to be able to read the correct number of bytes, The pointer type of the system object offset is strongly type-shifted.
How to use @property in @protocol and category
    1. Using property in protocol only generates setter and getter method declarations, and we use properties to implement this property for objects that want to obey my protocol.
    2. Category use @property is also the only way to generate a setter and getter method declaration, if we really need to add property to the category implementation, need to rely on the run-time two functions
      1. Objc_setassociatedobject
      2. Objc_getassociatedobject
How the runtime implements the weak property

Runtime on the registered class, will be laid out, for the weak object will be placed in a hash table. With the object address pointed to by weak as key, when the reference count of this object is 0, it will be dealloc, and in this weak table find all the weak objects with the object address as key, set to nil

Everyone is good at the field is not the same, we will generally find themselves from the resume to write good technical chat, if they are not very familiar, it is best not to write or pull out, in case the interviewer just very proficient here is the story.

Checklist

Summed up some interview questions, did not persist, and later put these as checklist, interview when really did not talk about when to do a reminder, language, framework, the nature of the operating mechanism:

[※] What are the property keywords in the @property?
同上
[Does the ※]weak attribute need to be nil in dealloc?
不需要,在ARC环境无论是强指针还是弱指针都无需在deallco设置为nil,ARC会自动帮我们处理
[※※] What is the role of @synthesize and @dynamic respectively?
    1. @property have two corresponding words, one is @synthesize and the other is @dynamic. If @synthesize and @dynamic are not written, then the default is @syntheszie var = _var;
    2. The semantics of @synthesize is that if you do not implement setter methods and Getter methods manually, then the compiler will automatically add these two methods to you.
    3. @dynamic tells the compiler that the setter and getter methods of the property are implemented by the user themselves and are not generated automatically. (Of course, for ReadOnly properties, only getter is required). If a property is declared as @dynamic var, then you do not provide the @setter method and the @getter method, the compile time is not a problem, but when the program runs to Instance.var =somevar, due to the lack of setter method will cause the program to crash , or when running to Somevar = Var, the lack of getter method can also cause a crash. Compile-time is no problem, the runtime executes the corresponding method, which is called dynamic binding.
[※※※] Arc, what are the default keywords when you do not display any of the property keywords specified?
    1. The default keyword for the corresponding base data type is
      Atomic,readwrite,assign
    2. For a normal OC object
      Atomic,readwrite,strong
[※※※] 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?
    1. Because the parent pointer can point to a subclass object, the purpose of using copy is to make the properties of this object unaffected by the outside world, and by using copy, whether I pass in a mutable object or a non-object, I am holding an immutable copy.
    2. If we use a strong, then this property may point to a mutable object, which is affected if the Mutable object is modified externally.
[※※※] 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?

If you do not specify a member variable name with a member variable that automatically generates a property with the same name, if the specified member variable is named, a member variable of the specified name is generated and is no longer generated if the member already exists.
If it is @synthesize foo; A member variable called Foo is also generated
If it is @synthesize foo = _foo; The member variable is not generated.

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

@synthesize is mainly used to generate the implementation of the Setter,getter method, after @property is enhanced, in fact, has rarely used @synthesize, you know @synthesize other use scenes? Can you introduce me to you?
If you understand, feel that the interviewer is very reasonable, can say a little praise.

[What happens when a message is sent to a nil object in ※※]OBJC?]
    1. Sending a message to nil in objective-c is completely valid--it just doesn't work at all:
      • If a method return value is an object, the message sent to nil returns 0 (nil). For example: Person * motherinlaw = [Aperson spouse] mother]; If the spouse object is nil, the message sent to nil mother will also return nil.
      • If the method return value is a pointer type, its pointer size is less than or equal to sizeof (void*), Float,double,long double, or a long long integer scalar, the message sent to nil returns 0.
      • If the method return value is a struct, the message sent to nil returns 0. Each field in the struct will have a value of 0.
      • If the return value of the method is not the case mentioned above, the return value of the message sent to nil will be undefined.
[What is the relationship between sending a message to an object in ※※※]OBJC [obj foo] and the Objc_msgsend () function?

This method compiles after the Objc_msgsend () function call. If I'm not mistaken, it's probably the case.
(Void () (ID, SEL)) (void ) objc_msgsend) ((ID) obj, sel_registername ("foo"));

When will [※※※] report unrecognized selector's abnormality?
    • When a method on the object is not implemented on the object [※※※※] How does a OBJC object make a memory layout? (Consider the case of a parent class)
    • All of the parent class's member variables and their own member variables are stored in the corresponding storage space for that object.
    • Inside each object is an Isa pointer to his class object, which holds the list of object methods and member variables for this object, a list of attributes, an internal ISA pointer to the meta-object (meta Class), and a list of class methods inside the meta-object. There is also a superclass pointer inside the class object that points to his parent class object
    • The root object is nsobject.
    • :

[※※※※] What does the pointer to ISA for a OBJC object point to? What's the effect?
    • Point to his class object so that the method on the object can be found

[※※※※] 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             

The results of the output are: Son,
Cause: Super and self are all pointing to this instance object,
The difference is that super calls a method that skips this class of methods, calling the parent class
The class method of the parent class method is originally implemented in the base class, so both the self and the super call are the same.
Specific analysis reference inquisitive objective-c Runtime (1)-Self & Super

[※※※※]runtime how to find the corresponding IMP address via selector? (Consider class methods and instance methods, respectively)
    • Each class object is a list of methods, the method list records the name of the method, the method implementation, and the parameter type, in fact, selector is the method name, the method name can be found in the method list corresponding method implementation.
[※※※※] objects associated with the runtime associate method need to be released when the main object Dealloc?
    • Not required under ARC
    • In MRC, the need for using retain or copy policies

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

    • Class method
      • A class method is a class object that belongs to the
      • Class methods can only be called through class objects
      • Self in a class method is a class object
      • Class methods can call other class methods
      • A member variable cannot be accessed in a class method
      • An indefinite call to an object method in a class method
    • Instance method
      • An instance method is an instance of an object that belongs to
      • Instance methods can only be called through an instance object
      • Self in an instance method is an instance object
      • Member variables can be accessed in instance methods
      • Invoking instance methods directly in an instance method
      • Class methods can also be called in Instance methods (by class name)
[What does the ※※※※※]_objc_msgforward function do, and what does it happen to call it directly?]
    1. I have not studied, from the name is used to forward the message, can you tell me? Thank you!
[How does ※※※※※]runtime implement the weak variable's auto-nil?
1. 没有研究过,你有研究过吗,可以给我讲讲吗?2. 我猜系统会维护一个弱指针列表,当某个对象销毁时候,它会把所有指向该对象的弱指针设置为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?
    因为编译后的类已经注册在 runtime 中,类结构体中的 objc_ivar_list 实例变量的链表 和 instance_size 实例变量的内存大小已经确定,同时runtime 会调用 class_setIvarLayout 或 class_setWeakIvarLayout 来处理 strong weak 引用。所以不能向存在的类中添加实例变量,运行时创建的类是可以添加实例变量,调用 class_addIvar 函数。但是得在调用 objc_allocateClassPair 之后,objc_registerClassPair 之前,原因同上。
[What is the relationship between ※※※]runloop and threads?]
1. 每一个线程中都一个runloop,只有主线的的runloop默认是开启的,其他线程的runloop是默认没有开启的 2. 可以通过CFRunLoopRun() 函数来开启一个事件循环 3. 看SDWebImage源码的时候见到有这么用过.
[What is the mode role of ※※※]runloop?]

The model is primarily used to specify the priority of the time in the running loop
There are two publicly available Mode in Apple:
Kcfrunloopdefaultmode
Kcfrunloopcommonmodes

If we add a Nstimer object with Kcfrunloopdefaultmode to the main run loop, Nstimer will no longer be dispatched when there is user event handling
If we add a Nstimer object with Kcfrunloopcommonmodes to the main running loop, when there is a user event processing, Nstimer can also be dispatched normally without affecting each other.

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

Ditto

[※※※※※] Guess how the Runloop interior is implemented?
  1. 他是一个死循环  2.如果事件队列中存放在事件,那就取出事件,执行相关代码  3.如果没有事件,就挂起,等有事件了,立即唤醒事件循环,开始执行. 简单来说。。。function loop() { initialize(); do { var message = get_next_message(); process_message(message); } while (message != quit);}
[What mechanism does ※]OBJC use to manage object memory?]
* MRC 手动引用计数* ARC 自动引用计数,现在通常使用自动引用计数
[※※※※] 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)
在每次事件循环开始创建自动释放池,在每次事件结束销毁自动释放池以viewDidLoad方法为例,可以理解为在viewDidLoad方法开始执行之前创建自动释放池,在viewDidLoad方法执行之后销毁自动释放吃

[※※※※] Under what circumstances does the bad_access appear?
1.  死循环了2.  访问一个僵尸对象
[※※※※※] How did Apple achieve autoreleasepool?
1. 我猜想autoreleasepool 本质就是一个队列(数组),2. 当调用autorelease的时候会把该对象添加到autoreleasepool中,并且把引用计数+13. 当autoreleasepool即将销毁的时候,把其中的所有对象进行一次release操作
[※※] What happens when a reference loop is used when using block, and how is it resolved?
 只要是一个对象对该block进行了强引用,在block内部有直接使用到该对象,
[※※] How do i modify the block external variables within a block?
    1. External variables modified by __bock can be modified inside the block
    2. If you want to pack B, you can say something about what __bock did inside.
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?
一般不用考虑,因为官方文档中没有告诉我们要注意发生强引用,所以推测系统控件一般没有对这些block进行强引用,所以我们可以不用考虑循环强引用的问题
[※※] 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)
  总体上说:  dispatch group,然后 wait forever 等待完成, 或者采取 group notify 来通知回调。  细节:  1. 创建异步队列  2. 创建dispatch_group dispatch_group_t = dispatch_group_create() 3. 通过组来执行异步下载任务 dispatch_group_async(queueGroup, aQueue, ^{ NSLog(@"下载图片."); }); 4.等到所有任务完成 dispatch_group_wait(queueGroup, DISPATCH_TIME_FOREVER); 5.合成图片
[What is the role of ※※※※]dispatch_barrier_async?]
barrier:是障碍物的意思,在多个并行任务中间,他就像是一个隔离带,把前后的并行任务分开.dispatch_barrier_async 作用是在并行队列中,等待前面操作并行任务完成再执行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");}
    1. can only output 1, and then thread the main path deadlock
[※※]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?
   // 添加键值观察    /**     1. 调用对象:要监听的对象     2. 参数     1> 观察者,负责处理监听事件的对象     2> 观察的属性     3> 观察的选项     4> 上下文     */    [self.person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"Person Name"];
 //NSObject classification method means that all NSObject can implement this method! //with the method of the protocol is very similar, the classification method can also be called "Implicit proxy"! Do not advocate, but know the concept! //all KVO monitoring events, this method will be called /** 1. Observed properties 2. Objects observed 3. Change Attribute Change dictionary (new/old) 4. Context, and the consistency that is passed when listening can be used to distinguish different listeners from the context! */-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (id) object change: (nsdictionary *) Change context :(void *) Context {nslog (@ "Sleep%@" , [nsthread CurrentThread]); [nsthread sleepfortimeinterval:1.0]; nslog (@ "%@%@%@%@", KeyPath, object, change, context);}   
[※※※] How to manually trigger a value of KVO
1.通过setValue:forKey: 给属性赋值2.通过setValue:forKeyPath: 给属性赋值3.直接调用setter方法方法给属性赋值4.直接通过指针给属性赋值5.给这个value设置一个值,就可以触发了
[※※※] 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?
1. 必须用在集合对象上或普通对象的集合属性上2. 简单集合运算符有@avg, @count , @max , @min ,@sum,3. 格式 @"@sum.age"或 @"集合属性.@max.age"
[※※※※] KVC and Kvo's keypath must be a property?
1.一个可以是成员变量
[※※※※※] How do I turn off the default implementation of the default KVO and go into a custom KVO implementation?

How to implement KVO on your own

[※※※※※]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?
    User Defined Runtime Attributes 是一个不被看重但功能非常强大的的特性,    它能够通过KVC的方式配置一些你在interface builder 中不能配置的属性。当你希望在IB中作尽可能多得事情,    这个特性能够帮助你编写更加轻量级的viewcontroller
[※※※] How to debug bad_access errors
1.设置全局断点快速定位问题代码所在行
[※※※]lldb (GDB) Common debug commands?
po 对象

Li Hongqiang iOS Classic face question 30-a large-size face question

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.