IOS Knowledge points

Source: Internet
Author: User

1. What is ARC?

ARC (Automatic Reference counting) automatic application counters, code automatically added Release/retain, originally needed to manually add to handle the memory management of the application count of the code can be automatically completed by the compiler. Arc was created to address memory management.

2. What is the difference between assign and Weak,_block and _weak?

Strong and weak are keywords that were introduced after arc, strong similar to retain, referencing the calculation +1,weak instead of changing the reference count. Weak and assign are reference calculations, and the difference between the two is that weak is used for object type, which is the pointer type, and assign is used for simple data types.

Block cannot modify local variables, if necessary, add block.
Block will strongly reference the object, causing retain-cycle, and need to use
weak

__weak __typeof(&*self)weakSelf =self;

This strong reference can be avoided by using weakself in block types.
(two pointers, pointing to the same piece of address (self));

3.__block meaning in Arc and non-arc

It is generally necessary to modify the variables in the block to be modified in advance by adding blocks .
In non-arc,
the reference calculation of a block-modified variable is constant.
In Arc, it is quoted and calculated as +1;
Can be used under non-arc (arc directly using __weak)

4. Is it always thread-safe to use atomic?

This question is seldom met, but the answer is of course not.
Atomic added a lock in the set method, preventing multi-threading from writing to the property, causing difficult-to-predict values. But it's also just a read-write lock. and thread safety is actually worse. Look underneath.

1 @interfaceMonperson:nsobject2@property (copy) NSString *FirstName;3@property (copy) NSString *LastName;4-(NSString *) FullName; 5 @end6 7 Thread A:8P.firstname =@"Rob";9 Thread B:TenP.firstname =@"Robert"; One Thread A: ALabel.string= P.firstname;//<< uh, oh-- would be Robert
View Code

But if a C is also written, D reads, D reads some random values (ABC modified values), which is not thread-safe. The best way is to use lock.

1 Thread A:2[PLock];//<< wait for it ......3 //Thread B now cannot access4Pp.firstname =@"Rob";5NSString FullName =P.fullname;6 [P unlock];7 //Thread B can now access plabel.string = FullName;8 9 Thread B:Ten[PLock];//<< wait for it ...... One //Thread A Now cannot access p ... A[P unlock];
View Code
Atomic a big problem is that it's slow, 20 times times slower than nonatomic.
Of course, it is suggested that this numerical value change can be done by the server.

5. An example of a retain cycle that has been encountered

Retain cycle can cause memory overflow, and serious situations can cause crashes. General note points will not occur, but in the network connection more places will be accidentally, VC asynchronous network request, after the successful block call VC, if at this time, the user has not used this VC, VC or not released.
Had nearly written a drop-down refresh, the view and VC strong references to each other, resulting in the release. View back to call the VC ScrollView Contentoffset.

6.+ (void) load; + (void) initialize; what's the use?

Two methods can perform initialization of some classes. Some of them are small differences.
+ (void) The Load method is compiled with the addition of the project, and the. m implements this method, which is called once, and it is worth noting that subclasses that are not implemented are not invoked, even if the parent class implements it. Categories, this method is implemented, and only one is called, with uncertainty.
+ (void) Initialize is called when the first message is sent to the class, unlike the Load method, which is later, lazy loading, and the parent class. M implements the method, and the subclass does not implement or invoke the parent class, as is the case with normal methods.

7. What is method swizzling?

In fact, it is very similar to the literal meaning. The harmonization of methods. You can modify the invocation of two methods in OC.

This picture looks a little bit more vivid.


is to swap two implementations

Specific practice,
First, use Categroy to build your own method.
The code to implement the method Exchange in the +load method (load can be guaranteed to be called, other methods are not reliable)

        Method ori_Method =  class_getInstanceMethod([MYclass class], @selector(lastObject));          Method my_Method = class_getInstanceMethod([MYclass class], @selector(myLastObject)); method_exchangeImplementations(ori_Method, my_Method);

Be sure to remember that your method implementation has changed.

What's the relationship between 8.UIView and Calayer?

View is a layer of a package, you have a lot of view of the operation is actually the layer, the reason why these two things is because

View supports a lot of gesture interactions, various clicks of the iphone you manipulate, drag and more.

Layer comes as far as Mac OS, is something that can cross the platform. Here is a very learned thing, the variable part of the system and the immutable part, the more variable part, the more unstable the system, but the function is more rich. Layer is as an immutable thing exists, view as a mutable thing exists, all of us in the iOS update time to get a lot of new view features, but the entire view system underlying a lot of things can still be used, various animations and so on. This is the credit for separating the view and layer. Word
Simply Speaking,uiview inherit from Nsresponder,handle events from the users, contains Calayer,which inherit from Nsobject,mai nly focus on rendering,animation etc.

9. How to add a fillet to the Uiimageview with high performance

In fact you only need the fillet picture .....

1 uigraphicsbeginimagecontextwithoptions (image.size, NO, Image.scale); 2 [[Uibezierpath bezierpathwithroundedrect:rect     Cornerradius:radius] addclip]; 3 [Image Drawinrect:rect]; 4  uiimage* imagenew =     uigraphicsgetimagefromcurrentimagecontext (); 5  Uigraphicsendimagecontext ();
View Code

10. What are the effects of using DrawRect?

Used for drawing, this method will be called at intiwithrect time.
The effect of this method is that when a touch event is available, it will be redrawn, and many of these buttons will have a more efficient effect. The following will be called
1. If the rect size is not set at UIView initialization, the drawrect will not be automatically called. The DrawRect is dropped after the Controller->loadview, Controller->viewdidload two method. So don't worry. In the controller, The drawrect of the view began to draw. This allows you to set some values in the controller to the view (if some variable values are needed for these view draw).
2, this method is called after calling SizeToFit, so you can call SizeToFit to calculate the size first. The system then automatically calls the DrawRect: method.
3, by setting the Contentmode property value to Uiviewcontentmoderedraw. The drawrect will be called automatically each time the frame is set or changed:.
4. Call Setneedsdisplay directly, or setneedsdisplayinrect: Trigger DrawRect:, but there is a precondition that rect cannot be 0.

11.SDWebImage What is the logic of loading images to Uiimageview?

Very very much like sdwebimage, even if later AF also did a picture cache, I also have been used. Picture caching is really a headache, but sdwebimage encapsulates everything, all you need is a simple set of URLs;
1, set up a booth map (optional);
2, according to the URL to find in memory, find the return picture
3, memory can not find, hard disk find, find back pictures, memory to do backup
4, hard drive can not find, go to download, return pictures, and storage (hard disk, memory).

There are a few technical details that are more important,
1, the picture in memory is the Key-value way.
2, in the hard disk is the way of data, Imagewithdata method obtained.
3, Key-value mode is the URL of the MD5.
4, register memory and hard disk notification, if very tight, delete part.
Many technical details, to see the source code.

What does 12.loadView do?

The Viewcontroller method will be called before Viewdidload.
Many people will be puzzled self.view, this view is where the truth comes, is here. Generally do not need to operate this. But if there is a special requirement, this method can be used when the Self.view is our own custom view.

  MyView *myview = [[MyView alloc]init];  self.view = myview;

Rewrite it just like the above.
Remember: Don't call-loadview-viewdidload yourself

  13.viewWillLayoutSubViewWhen Viewcontroller's bounds is changed again, call this method to implement the Subview position. This method can be overridden to implement a change in the parent view Subview. What kinds of queue are there in 14.GCD? Have you built the serial queue yourself? What is the threading model behind it? 1. Home row dispatch_main_queue (); Serial, update UI
2. Global queue dispatch_global_queue (); Parallel, four priority: Background,low,default,high
3. Custom queues dispatch_queue_t queue; You can customize is parallel:

IOS Knowledge points

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.