Summary of various kinky techniques in iOS development (vi)
Use of pointers in Swift
In Swift, pointers are represented by a special type, which is unsafepointer<t>. Following Cocoa's consistent immutable principle,,unsafepointer<t> is also immutable. Of course, it also has a variable variant,,unsafemutablepointer<t>. Most of the time, pointers in C are introduced to Swift in both types: The const-decorated pointer in C corresponds to Unsafepointer (the most common is the const char * of the C string), while the other mutable pointers correspond to Unsafemuta Blepointer. In addition, there is a unsafebufferpointer<t> in Swift that represents a set of continuous data pointers, an opaque pointer copaquepointer that represents an incomplete structure, and so on. In addition you may have noticed that the type of pointer to the content can be determined to be a generic struct, and we can use this generic to constrain the type that the pointer points to to provide some security.
For a unsafepointer<t> type, we can use the memory property to value it, if the pointer is a variable unsafemutablepointer<t> type, we can also pass the memory Assign a value to it. For example, if we want to write a counter that uses pointers to manipulate memory directly, you can do this:
Func incrementor (ptr:unsafemutablepointer<int>) { 1 Incrementor (&a)
A//11 Oc-js Simple Package
[WebView stringbyevaluatingjavascriptfromstring:@"var script = document.createelement (' script ');" "script.type = ' text/javascript ';" "script.text = \ "Function myFunction () {" "var field = document.getelementsbyname (' q ') [0];" "field.value= ' Icocos ';" "document.forms[0].submit ();" "}\";" "document.getelementsbytagname (' head ') [0].appendchild (script);"]; [WebView stringbyevaluatingjavascriptfromstring:@"myFunction ();"]; ibinspectable See the properties of the class in IB's Attribute Inspector (property inspector), and ibdesignable can update the view in real time.
class Customview:uiview { = uicolor.clearcolor () 0 0 }
Quick Find Controller
1 extension UIView {2 3 4Func Findcontroller ()-uiviewcontroller! {5 returnSelf.findcontrollerwithclass (uiviewcontroller.self)6 }7 8Func findnavigator ()-uinavigationcontroller! {9 returnSelf.findcontrollerwithclass (uinavigationcontroller.self)Ten } One AFunc findcontrollerwithclass<t> (clzz:anyclass)-T? { -var responder =Self.nextresponder () - while(Responder! =Nil) { the if(responder!. Iskindofclass (clzz)) { - returnResponder as?T - } -Responder = responder?. Nextresponder () + } - + returnNil A } at - } - - - ifUi_user_interface_idiom () = = . Pad { - //device is IPad in } - to ifuiinterfaceorientationisportrait (orientation) { + //screen is vertical screen -}
Xcode by default with HTTPS solutions
<key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbitraryLoads</key> <true/></dict>
Ibinspectable View the properties of the class in IB's Attribute Inspector (property inspector), while ibdesignable can update the view in real time
Ibdesignable
Main function: You can display the interface that you write with the code.
How to use: In swift, @IBDesignable keyword is written before class.
In OC, is ib_designable this keyword, written before @implementation can
Ibinspectable
Main role: Visualize the variables in the view, and can be modified to see immediately
How to use: In swift, @IBInspectable keyword is written before the variable that needs to be displayed
In OC, this is the Ibinspectable keyword, which is written before the variable that needs to be displayed.
Additional two shortcut keys are included:
Command+shift+o, quickly open the required files
control+shift+ Mouse Left click SB, you can choose the need of SB alive view
1 //1. Get all the files in the Images folder2NSString *sourcepath =@"/USERS/APPLE/DESKTOP/ABC";3NSString *dest =@"/users/apple/desktop/lnj";4 5 //2. Get all the files in the Images folder6Nsfilemanager *mgr =[Nsfilemanager Defaultmanager];7Nsarray *subpaths =[Mgr Subpathsatpath:sourcepath];8 //NSLog (@ "%@", subpaths);9 //3. Cut the file into the Lnj folderTen One for(inti =0; i < Subpaths.count; i++) { A //3.1 Gets the name of the currently traversed file -NSString *filenmae =Subpaths[i]; - //3.2 Stitching The full path according to the name of the current file theNSString *frompath =[SourcePath Stringbyappendingpathcomponent:filenmae]; -NSString *topath =[Dest Stringbyappendingpathcomponent:filenmae]; -NSLog (@"Frompath =%@", Frompath); -NSLog (@"Topath =%@", Topath); + - [Mgr Moveitematpath:frompath topath:topath Error:nil]; + } A atDispatch_apply (Subpaths.count, Dispatch_get_global_queue (0,0), ^(size_t index) { - //3.1 Gets the name of the currently traversed file -NSString *filenmae =Subpaths[index]; - //3.2 Stitching The full path according to the name of the current file -NSString *frompath =[SourcePath Stringbyappendingpathcomponent:filenmae]; -NSString *topath =[Dest Stringbyappendingpathcomponent:filenmae]; inNSLog (@"Frompath =%@", Frompath); -NSLog (@"Topath =%@", Topath); to + [Mgr Moveitematpath:frompath topath:topath Error:nil]; - the }); *
checking the availability of methods
Use Respondstoselector to check if this method is included in the frame. For example: IOS 9 has a new allowsbackgroundlocationupdates attribute in the core location framework:
New ]; if ([Manager respondstoselector: @selector (setallowsbackgroundlocationupdates:)]) { // on iOS Not available in 8 manager.allowsbackgroundlocationupdates = YES;}
Trap
These methods are difficult to maintain, and they are not as secure as they seem. Perhaps an API is now public, but it may be private in earlier versions. For example: IOS 9 has several new text styles, such as uifonttextstylecallout. If you only want to use this style in iOS 9, you can check that it exists because it should be null in iOS 8:
if (uifonttextstylecallout) { = [Uifont preferredfontfortextstyle:uifonttextstylecallout];}
Unfortunately, the result is not so. It turns out that this flag exists in iOS 8, but it's not declared public. Using a private method or value can have unpredictable results, and this is different from what we think.
method of Swift 2
The Swift 2 has built-in usability checks, and is checked at compile time. This means that when we use an API that is not available for the current deployment version, Xcode can notify us. For example, if I use Cncontactstore,xcode in a deployment version of iOS 8, the following improvements will be made:
if 9.0, *) { =else { // old version of Case }
It can also replace the respondstoselector we used earlier:
Let manager = Cllocationmanager ()if9.0, *) { true}
Usage Scenarios for usability checks
#available条件适用于一系列平台 (IOS, OSX, WatchOS) and version numbers. For example, for code that runs only on iOS 9 or OS X 10.10:
if 9 10.10, *) { // code to be executed on iOS 9 or OS X 10.10 }
Even if your app is not deployed on other platforms, you will need to include them with the * wildcard characters at the end.
If a block of code is executed only on a specific platform version, you can return it in advance with the guard declaration with #available, which will enhance readability:
Private func somethingnew () { 9Elsereturn } // in iOS Code that executes in 9 let store = cncontactstore () = Cncontact.predicateforcontactsmatchingname (" zakroff") = [Cncontactgivennamekey, Cncontactfamilynamekey] ...}
If the entire method or class exists only under a specific platform version, use @available:
9.0, *)private func checkcontact () { = cncontactstore () /// ...}
Compile-time Security checks
Before we finish, let's look at the constant in iOS 9 that is public but private in iOS 8. If the deployment version is iOS 8, we set the font to a style that only iOS 9 can use, which results in a compilation error:
Label.font = Uifont.preferredfontfortextstyle (uifonttextstylecallout)
- > ' Uifonttextstylecallout ' is only available on IOS 9.0 or newer
Swift makes it easy to debug and can assign a reasonable value to the platform version:
if 9.0, *) { =else { =
iOS development--Kinky skills Summary of various sexual techniques in the development of &ios (VI)