swift中指針的使用javascript
func incrementor(ptr: UnsafeMutablePointer<Int>) { ptr.memory += 1 } var a = 10 incrementor(&a)
[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();"];
@IBDesignable class CustomView : UIView { @IBInspectable var borderColor: UIColor = UIColor.clearColor() @IBInspectable var borderWidth: CGFloat = 0 @IBInspectable var cornerRadius: CGFloat = 0 }
1 extension UIView { 2 3 4 func findController() -> UIViewController! { 5 return self.findControllerWithClass(UIViewController.self) 6 } 7 8 func findNavigator() -> UINavigationController! { 9 return self.findControllerWithClass(UINavigationController.self) 10 } 11 12 func findControllerWithClass<T>(clzz: AnyClass) -> T? { 13 var responder = self.nextResponder() 14 while(responder != nil) { 15 if (responder!.isKindOfClass(clzz)) { 16 return responder as? T 17 } 18 responder = responder?.nextResponder() 19 } 20 21 return nil 22 } 23 24 } 25 26 27 if UI_USER_INTERFACE_IDIOM() == .Pad { 28 // 設備是 iPad 29 } 30 31 if UIInterfaceOrientationIsPortrait(orientation) { 32 // 屏幕是豎屏 33 }
Xcode默認使用https的解決方案java
<key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbitraryLoads</key> <true/></dict>
IBInspectable在IB的Attribute Inspector(屬性檢查器)中查看類的屬性,而IBDesignable能實時更新視圖
1 // 1.獲取images文件夾中全部的文件 2 NSString *sourcePath = @"/Users/apple/Desktop/abc"; 3 NSString *dest = @"/Users/apple/Desktop/lnj"; 4 5 // 2.獲取images文件夾中全部的文件 6 NSFileManager *mgr = [NSFileManager defaultManager]; 7 NSArray *subPaths = [mgr subpathsAtPath:sourcePath]; 8 // NSLog(@"%@", subPaths); 9 // 3.剪切文件到lnj文件夾中 10 11 for (int i = 0; i < subPaths.count; i++) { 12 // 3.1獲取當前遍歷到得文件的名稱 13 NSString *fileNmae = subPaths[i]; 14 // 3.2根據當前文件的名稱, 拼接全路徑 15 NSString *fromPath = [sourcePath stringByAppendingPathComponent:fileNmae]; 16 NSString *toPath = [dest stringByAppendingPathComponent:fileNmae]; 17 NSLog(@"fromPath = %@", fromPath); 18 NSLog(@"toPath = %@", toPath); 19 20 [mgr moveItemAtPath:fromPath toPath:toPath error:nil]; 21 } 22 23 dispatch_apply(subPaths.count, dispatch_get_global_queue(0, 0), ^(size_t index) { 24 // 3.1獲取當前遍歷到得文件的名稱 25 NSString *fileNmae = subPaths[index]; 26 // 3.2根據當前文件的名稱, 拼接全路徑 27 NSString *fromPath = [sourcePath stringByAppendingPathComponent:fileNmae]; 28 NSString *toPath = [dest stringByAppendingPathComponent:fileNmae]; 29 NSLog(@"fromPath = %@", fromPath); 30 NSLog(@"toPath = %@", toPath); 31 32 [mgr moveItemAtPath:fromPath toPath:toPath error:nil]; 33 34 }); 35
CLLocationManager *manager = [CLLocationManager new]; if ([manager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) { // 在iOS 8中不可用 manager.allowsBackgroundLocationUpdates = YES; }
if (UIFontTextStyleCallout) { textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCallout]; }
不幸的是結果並不是如此。原來這個標誌在iOS 8中是存在的,只是沒有宣佈公有。使用一個私有的方法或值有可能出現難以預料的結果,何況這也和咱們的想法不一樣。
if #available(iOS 9.0, *) { let store = CNContactStore() } else { // 舊版本的狀況 }
let manager = CLLocationManager() if #available(iOS 9.0, *) { manager.allowsBackgroundLocationUpdates = true }
可用性檢查的使用情形web
if #available(iOS 9, OSX 10.10, *) { // 將在iOS 9或OS X 10.10上執行的代碼 }
private func somethingNew() { guard #available(iOS 9, *) else { return } // 在iOS 9中執行的代碼 let store = CNContactStore() let predicate = CNContact.predicateForContactsMatchingName("Zakroff") let keys = [CNContactGivenNameKey, CNContactFamilyNameKey] ... }
@available(iOS 9.0, *) private func checkContact() { let store = CNContactStore() // ... }
編譯時的安全性檢查swift
if #available(iOS 9.0, *) { label.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCallout) } else { label.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody) }