10 surprise raids developed by iOS

Source: Internet
Author: User
Tags variable scope
<span id="Label3"></p><p><p><strong>1. About keyword volatile</strong></p></p><p><p>A variable that is defined as volatile means that the variable may be unexpectedly changed so that the compiler does not assume the value of the Variable. precisely, the optimizer must carefully re-read the value of the variable each time it uses the variable, rather than using the backup stored in the Register. Volatile is a type modifier, just as you are more familiar with Const. It is designed to modify variables that are accessed and modified by different threads. If you do not add volatile, it will basically result in the inability to write multithreaded programs or the compiler losing a lot of optimization opportunities.</p></p><p><p>Volatile variables have synchronized visibility characteristics, but they do not have atomic properties. This means that threads can automatically discover the latest values of volatile variables. Volatile variables can be used to provide thread safety, but can only be applied to a very limited set of use Cases: there is no constraint between multiple variables or between the current value of a variable and the modified Value. therefore, using volatile alone is not sufficient to implement counters, mutexes, or any class that has invariant (invariants) associated with multiple variables (for example, "start <=end").</p></p><p><p>For simplicity or scalability, you might prefer to use volatile variables instead of locks. Some idioms are easier to encode and read when using volatile variables rather than locks. In addition, volatile variables do not cause threads to block like locks, and therefore are less likely to cause scalability Problems. In some cases, if the read operation is much larger than the write operation, the volatile variable can also provide a performance advantage over the LOCK.</p></p><p><p><strong>code example</strong></p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">volatile int I=10;int j = I;...int k = i;</pre></pre><p><p>Volatile tells the compiler that I can change at any time, and each time it is used it must be read from the address of i, so the compiler generated executable code will re-read the data from I's address in K. Compiler optimizations are compiled when a release executable code is generated, and variables related to the volatile keyword are not compiled for Optimization. The optimization approach is that since the compiler found that the code between the I-read data two times does not operate on i, it automatically places the Last-read data in K. Instead of re-reading from inside I. thus, If I is a register variable or represents a port data is prone to error, so volatile can guarantee a stable access to the special address, no Error.</p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">int square (volatile int *ptr) {return *ptr * *ptr;}</pre></pre><p><p>The purpose of this code is to return the pointer ptr to the square of the value, but since PTR points to a volatile parameter, the compiler will produce code similar to the Following:</p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">int square (volatile int *ptr) {int a a, a = *ptr; B = *ptr; return a * b;}</pre></pre><p><p>Because the values of the *ptr can be unexpectedly changed, A and B may be different. As a result, this code may return to the square value you expect! The correct code is as Follows:</p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">Long square (volatile int *ptr) {int a; a = *ptr; return a * a;}</pre></pre><p><p>Here are a few uses of volatile variables:</p></p> <ul class=" list-paddingleft-2"> <ul class=" list-paddingleft-2"> <li><p>Hardware registers for parallel devices (e.g., status Registers)</p></li> <li><p>A non-automatic variable that is accessed in an interrupt service subroutine (non-automatic Variables)</p></li> <li><p>Variables shared by several tasks in multi-threaded applications</p></li> </ul> </ul><p><p>So Here's the Question:</p></p> <ul class=" list-paddingleft-2"> <ul class=" list-paddingleft-2"> <li><p>Can a parameter be either const or volatile? The answer is Yes. An example is a read-only status register. It is volatile because it can be changed unexpectedly. It is const because the program should not attempt to modify it.</p></li> <li><p>Can a pointer be volatile? The answer is Yes. Although this is not very common. An example is when a service subroutine fixes a pointer that points to a buffer.</p></li> </ul> </ul><p><p>Key points for using volatile in writing multithreaded programs:</p></p><p><p>1) declare all shared objects as volatile;</p></p><p><p>2) do not apply volatile directly to the basic type;</p></p><p><p>3) when a shared class is defined, a volatile member function is used to ensure thread safety;</p></p><p><p>In multi-threading, we can use the mechanism of locking to protect the resource critical Area. The operation of shared variables outside the critical section requires volatile and is non-volatile in the critical Section.</p></p><p><p><strong>2. Location of the keyword const</strong></p></p><p><p>Const means "read-only" and analyzes the following meanings:</p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">const int A;int const A;//THE First two functions are the same, A is a constant integer number. const int *a;//the Third means that a is a pointer to a constant integer number (the integer number is not modifiable, but the pointer can be) int * Const a;//the Fourth meaning A is a constant pointer to an integer number (the integer number pointed to by the pointer is modifiable, but the pointer is not modifiable) int CONST * Const A;//A is a constant pointer to a constant number (the integer number pointed to by the pointer is not modifiable and the pointer is not modifiable). Indicates that a is a pointer constant that must be fixed to an int constant or int variable at initialization time, and then no longer point to another place, and it always treats the target it points to as an int constant. It can also be written as const int* Const a;</pre></pre><p><p>Proper use of the keyword const allows the compiler to naturally protect those parameters that you do not want to change, and prevent them from being unintentionally modified by the Code. This can reduce the occurrence of bugs. To prevent a variable from being changed, you can use the const KEYWORD.</p></p><p><p>When defining a const variable, It is often necessary to initialize it, since there is no chance to change it again; for pointers, you can specify that the pointer itself is a const, or that the pointer refers to a const or both, and that the const is specified in a function declaration. A formal parameter can be modified to indicate that it is an input parameter, that its value cannot be changed inside the function, that, for a member function of a class, it is a constant function and cannot modify the member variable of a class, and for a member function of a class, sometimes it must be specified that its return value is the const TYPE. So that its return value is not a "left value".</p></p><p><p><strong>3. Hide Navigation Bar when sliding</strong></p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">Navigationcontroller.hidesbarsonswipe = Yes;</pre></pre><p><p><strong>4. Remove the title of the navigation bar return key band</strong></p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">[[uibarbuttonitem appearance] Setbackbuttontitlepositionadjustment:uioffsetmake (0,-60) forbarmetrics:uibarmetricsdefault];</pre></pre><p><p><strong>5. Turn the navigationbar into transparent and not blurred</strong></p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">[self.navigationController.navigationBar setbackgroundimage:[uiimage new] Forbarmetrics:uibarmetri Csdefault];self.navigationcontroller.navigationbar. shadowImage = [UIImage new]; Self.navigationController.navigationBar. Translucent = YES;</pre></pre><p><p><strong>6. Static</strong></p></p><p><p>The function body static variable scope is the function body, differs from the auto variable, the memory of the variable is allocated only once, so its value will maintain the last value at the next call, and the static global variable inside the module can be accessed by the function used inside the module, but not by other functions outside the Module. The static function within the module can only be called by other functions within the module, and the Function's scope is limited to the module that declares it; the static member variable in the class is owned by the entire class and has only one copy of all objects of the class; the static member function in the class belongs to the entire class. , this function does not receive this pointer, so it can only access static member variables of the class.</p></p><p><p><strong>7. Replace all directions of uiswipegesture with a pan gesture</strong></p></p><pre class="brush:js;toolbar:false">-  (void) pan: (uipangesturerecognizer *) sender{typedef ns_enum (nsuinteger,  Uipangesturerecognizerdirection)  {uipangesturerecognizerdirectionundefined, uipangesturerecognizerdirectionup,uipangesturerecognizerdirectiondown,uipangesturerecognizerdirectionleft, uipangesturerecognizerdirectionright};static  uipangesturerecognizerdirection direction =  UIPanGestureRecognizerDirectionUndefined;switch  (sender.state)  {case   uigesturerecognizerstatebegan: {if   (direction ==  Uipangesturerecognizerdirectionundefined)  {cgpoint velocity = [sender velocityinview: recognizer.view]; Bool isverticalgesture = fabs (velocity.y)  > fabs (velocity.x);if  ( Isverticalgesture)  {    if  (velocity.y > 0)  {     direction = uipangesturerecognizerdirectiondown;    }  & NBsp; else  {    direction = uipangesturerecognizerdirectionup;     }}else{if  (velocity.x > 0)  {direction =  uipangesturerecognizerdirectionright;} else{direction = uipangesturerecognizerdirectionleft;}}} break ;} case uigesturerecognizerstatechanged: {switch  (direction)  {case  uipangesturerecognizerdirectionup: {[self handleupwardsgesture:sender];break ;} case uipangesturerecognizerdirectiondown: {[self handledownwardsgesture:sender];break;} case  uipangesturerecognizerdirectionleft: {[self handleleftgesture:sender];break;} case uipangesturerecognizerdirectionright: {[self handlerightgesture:sender];break ;} default : {break;}} break;} case  uigesturerecognizerstateended: {direction =  uipangesturerecognizerdirectionundefined;break;} default:break;}}</pre><p><p><strong>8. Stretch the picture without distortion</strong></p></p><p><p><strong></strong></p></p><p><p>Equivalent to:</p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">[[UIImage imagenamed:@ "] stretchableimagewithleftcapwidth:10 topcapheight:10]; [[UIImage imagenamed:@ "] resizableimagewithcapinsets:uiedgeinsetsmake (10, 10, 10, 10)];</pre></pre><p><p><strong>9. GIF Image Display optimization</strong></p></p><p><p><strong></strong></p></p><p><p>Flanimatedimage can help you complete the display of Gif. Resolves the situation in which the GIF shows Stuttering.</p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">Flanimatedimage *image = [flanimatedimage animatedimagewithgifdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:@ "https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif"]; Flanimatedimageview *imageview = [[flanimatedimageview alloc] init];imageview.animatedimage = Image;imageView.frame = CGRectMake (0.0, 0.0, 100.0, 100.0); [self.view addsubview:imageview];</pre></pre><p><p>It's so simple to Use.</p></p><p><p><strong>CollectionView implementation of TableView hover header</strong></p></p><p><p><strong></strong></p></p><p><p>Csstickyheaderflowlayout can solve your Question.</p></p><pre class="brush:js;toolbar:false">#import "CSStickyHeaderFlowLayout.h"-(void) viewdidload {[super viewdidload];//Locate your Layoutcsstickyheaderflowlayout *layout = (id) self.collectionviewlayout;if ([layout iskindofclass:[ Csstickyheaderflowlayout class]]) {layout.parallaxheaderreferencesize = Cgsizemake (320, 200);}} -(uicollectionreusableview *) collectionview: (uicollectionview *) CollectionView viewforsupplementaryelementofkind :(nsstring *) Kind atindexpath: (nsindexpath *) indexpath {//Check the kind if it ' s Csstickyheaderparallaxheaderif ([kind i Sequaltostring:csstickyheaderparallaxheader]) {uicollectionreusableview *cell = [collectionview Dequeuereusablesupplementaryviewofkind:kind withreuseidentifier:@ "header" forindexpath:indexpath]; Return cell;}}</pre><p><p>10 surprise raids developed by iOS</p></p></span>

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.