Application scenarios for iOS learning--weak

Source: Internet
Author: User

OC uses the arc mechanism, so attributes such as strong also have application scenarios. When you use Alloc to allocate memory space to an object in the heap, and then let the stack use a strong pointer to the memory address, you can ensure that the data in the heap is not freed before the pointer is automatically released. Then if the heap memory address is executed with a weak pointer instead of a strong pointer, it will be freed automatically after allocating the memory space.

So, there is a different pointer property for this application scenario.

@property (nonatomic, Strong) Uiscrollview **Imgview; @end @implementation Viewcontroller
-(void   =<    Span style= "COLOR: #000000" > [[Uiscrollview alloc] initWithFrame:self.view.bounds];    Self.view  = Self.scrollview;  //  network data are both binary  nsdata *data = [NSData Datawithcontentsofurl:[nsurl urlwithstring:@ "  Http://mpic.tiankong.com/bf0/7f9/bf07f9842b76981cc2cae5d8850bbeaf/640.jpg   "     ];    UIImage  *image = [UIImage Imagewithdata:data];    Uiimageview  *imgview = [[Uiimageview alloc] initwithimage:image];    Self.imgview  = Imgview;    [Self.scrollview AddSubview:self.imgView]; [Self.imgview SizeToFit];}  

Here I do a few things, one is that when using Loadview to change Self's view to Scrollview,loadview call, the system will no longer read the Sb/xib file, so even if you do something on Xib, It can be completely covered with loadview. The second is to create a weak pointer _imgview. Here, if you change the ScrollView strong to weak, then you will execute [Self.view Addsubview:imgview]; When the code crashes, the cause of the crash is obvious, Self.view equals nil, because Self.scrollview is a weak pointer, [[Uiscrollview alloc] InitWithFrame:self.view.bounds] is released when it is created. However, Imgview is not a problem when it is declared as a weak pointer, because it does [Self.view Addsubview:imgview] before assigning Imgview to Self.imgview. Imgview added to self.view.subViews this array, look at the source code can be found, subviews is also a strong pointer, it points to the Imgview this memory, So when you use Self.imgview again, the weak pointer points to the memory and the memory is not freed. Pointing to Imgvie with a weak pointer is an advantage, and if you perform a delete operation, you generally use Removefromsuperview to remove the Imgview from Subviews. If Self.imgview is a strong pointer, though the interface is not visible, the memory allocated to the Imgview in the heap is still not released, and the weak pointer points to the heap memory that was cleared immediately after remove.
PS: Network data are binary--network transmission to the physical layer, and the cable using current transmission, the current high and low frequency is the binary.

iOS Learning--weak scenarios

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.