// curring 另类调用方式 let funcWithA = addCur(1) print("funcWithA type : \(funcWithA.dynamicType)") let funcWithAB = funcWithA(b: 2) print("funcWithAB type : \(funcWithAB.dynamicType)") let resultCurring = funcWithAB(c: 3) print("resultCurring type : \(resultCurring.dynamicType)") ```
此时你还是会觉得这只是把一个函数拆开来调用了 那么我们打印一下 每个调用步骤中变量的类型
let funcWithA = addCur(1) print(“funcWithA type : (funcWithA.dynamicType)”) // let funcWithAB = funcWithA(b: 2) print(“funcWithAB type : (funcWithAB.dynamicType)”) let resultCurring = funcWithAB(c: 3) print(“resultCurring type : (resultCurring.dynamicType)”)
1 2
打印结果为
1 + 2 + 3 = 6 funcWithA type : Int -> Int -> Int funcWithAB type : Int -> Int 1 + 2 + 3 = 6 resultCurring type : Int
1 2 3 4 5 6 7 8 9 10 11 12 13
可以看到 变量funcWithA 为 Int -> Int -> Int 类型 funcWithAB 为Int -> Int
// 拼接字符 func concat(chars: Array) { var resultString = “” for char in chars { resultString += char } print(resultString) }
concat([“1”,”2”,”3”]) print(“**“)
// 每个字符代表的数字 + 1 后再拼接 func concatAdd(chars: Array, addNum: Int) { var resultString = “” for char in chars { var num = Int(char)! num = num + addNum let newChar = String(num) resultString += newChar } print(resultString) }
print(“**“) // 每个字符代表的数字 10 后再拼接 func concatMultiply(chars: Array, multiplyNum: Int) { var resultString = “” for char in chars { var num = Int(char)! num = num multiplyNum let newChar = String(num) resultString += newChar } print(resultString) }
print(“**“) / 让我们使用柯里化吧 */ func add(a: Int)(b: Int) -> Int{ return a + b }
func multiply(a: Int)(b: Int) -> Int{ return a * b }
func concatByCurring(chars: Array)(caculateFunc: (Int)->Int) { var resultString = “” for char in chars { var num = Int(char)! var newNum = caculateFunc(num) let newChar = String(newNum) resultString += newChar } print(resultString) }
People().printSpeak() // 打印结果为 People func speak : People -> () -> () // 可见实例的方法就是一个Curring结构
1 2
下面是[Instance Methods are Curried Functions in Swift](http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/?utm_campaign=iOS_Dev_Weekly_Issue_157&utm_medium=email&utm_source=iOS%2BDev%2BWeekly)中的例子 很开脑洞
Markdown is a plain text format for writing structured documents, based on conventions used for indicating formatting in email and usenet posts. It was developed in 2004 by John Gruber, who wrote the first markdown-to-html converter in Perl, and it soon became widely used in websites. By 2014 there were dozens of implementations in many languages.
// 键盘响应布局 @objc func keyboardFrameChanged(notification: NSNotification) { let dict = NSDictionary(dictionary: notification.userInfo!) let keyboardValue = dict.objectForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue let bottomDistance = mainScreenSize().height - keyboardValue.CGRectValue().origin.y let duration = Double(dict.objectForKey(UIKeyboardAnimationDurationUserInfoKey) as! NSNumber) UIView.animateWithDuration(duration, animations: { self.inputViewConstraint!.constant = -bottomDistance self.view.layoutIfNeeded() }, completion: { (value: Bool) in self.chatTableView.scrollToBottom(animation: true) }) }
ScrollView下拉动态修改keyboard(InputView)的frame
常见隐藏keyboard的一些方式
TouchBeigin
DidDrag
EndDrag
Interactive
iOS7 开始,ScrollView提供
1 2 3 4 5 6
@available(iOS 7.0, *) publicenum UIScrollViewKeyboardDismissMode : Int { case None case OnDrag // dismisses the keyboard when a drag begins case Interactive // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss }
//组合动画方式1 AnimatorSet set = new AnimatorSet(); ((set.play(animator).with(animator1).before(animator2)).before(animator3)).after(animator4); set.setDuration(5000); set.start();
publicintgetRealSize(int measureSpec){ int result = 1; int mode = MeasureSpec.getMode(measureSpec); int size = MeasureSpec.getSize(measureSpec);
if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.UNSPECIFIED) { //自己计算 result = (int) (mRadius * 2 + mStrokeWidth); } else { result = size; }
return result; }
privatevoidinitRect(){ if (mRect == null) { mRect = new RectF(); int viewSize = (int) (mRadius * 2); int left = (mWidth - viewSize) / 2; int top = (mHeight - viewSize) / 2; int right = left + viewSize; int bottom = top + viewSize; mRect.set(left, top, right, bottom); } }
// in objective-c, but in swift, #define can't be used any more // use let keyword to define a macro, look up original document: /* Simple Macros Where you typically used the #define directive to define a primitive constant in C and Objective-C, in Swift you use a global constant instead. For example, the constant definition #define FADE_ANIMATION_DURATION 0.35 can be better expressed in Swift withlet FADE_ANIMATION_DURATION = 0.35. Because simple constant-like macros map directly to Swift global variables, the compiler automatically imports simple macros defined in C and Objective-C source files. */ // in objective-c // #define kCommonAPI @"http://xxxxxxx" // but in swift, no #define, just use let to define let kCommonAPI = "http://xxxxxxx"
let dog = "a gog"// 可以不添加分号 let cat = "a cat"; print(cat)// 除了最后一条语句可以不添加分号外,其它都需要添加分号来隔开 let catTwo = "two cats"; let name = "Jobs"; print("\(name) has \(catTwos)")
类型转换
Swift不会像C、OC那样自动隐式转换类型,所以我们需要手动进行类型转换
1 2 3 4 5 6 7 8
let twoThousand: UInt16 = 2000 // one是UInt8类型 let one: UInt8 = 1 // twoThousand是UInt16类型,one是UInt8类型,如果要执行相加,那么就需要进行类型转换 // 否则会报错的。 let twoThousandAndOne = twoThousand + UInt16(one)
#coding=utf-8 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage from com.android.monkeyrunner import MonkeyRunner as mr from com.android.monkeyrunner import MonkeyDevice as md from com.android.monkeyrunner import MonkeyImage as mi device=mr.waitForConnection() for i in range(1000): MonkeyRunner.sleep(1) print i device.touch(500,950,'DOWN_AND_UP') print"end"
// Here, this Activity is the current activity if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block // this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } }
@Override publicvoidonRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){ switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the // contacts-related task you need to do.
} else {
// permission denied, boo! Disable the // functionality that depends on this permission. } return; }
// other 'case' lines to check for other // permissions this app might request } }