Swift初见 — 张超耀 宏 1 2 3 4 5 6 7 8 9 10 /* 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 with let 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. */ let kCommonAPI = "http://xxxxxxx"
注释 & 分号
在Swift中,注释跟C/OC语言中的注释很像,但最大的不同点就是在Swift中多行注释可以嵌套
1 2 3 4 5 6 // 这是单行注释 /* 这也是注释,但是多行注释 /* 多行注释在swift中是可以嵌套的* / /* 原官方指导教程上说嵌套多行注释可以快速、简单地把大的代码块分成多块来注释 * / * /
与其它开发语言不同的时,swift是不要求写分号的,当然如果想写,也是可以的。当你想把多个语句写到同一行时,这种情况下就一定要使用分号来隔开不同的语句了
1 2 3 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 let one: UInt8 = 1 let twoThousandAndOne = twoThousand + UInt16(one) * * 浮点值转换成整型时,会截尾* *
类型别名(Typealias)
类型别名也就是给已经存在的类型起一个别名。定义类型别名是使用关键字typealias。 类型别名一般是为了让开发者更容易看出变量或者常量的类型或者是更好地归类某一个模块中需要使用到的类型,让开发者见名知意。
1 2 3 4 5 6 7 typealias mySample = UInt16 所以mySample.min = UInt16.min ,也就是0 var maxAmplitudeFound = mySample.min
“?” 和 “!”
Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始值, 也就是说变量不会有默认值,所以要求使用变量之前必须要对其初始化 。如果在使用变量之前不进行初始化就会报错.
1 2 3 4 5 6 7 8 9 10 11 12 13 var stringValue : String ?stringValue = nil print ("\(stringValue) " )let hashValue = stringValue?.hashValuevar strValue: String ?if (strValue != nil ) { }
下期预告 深度理解Swift的”?”和”!” Swift之断言 浅谈Swift的closure Dubbo 上手 — 陈奎 『图像和滤镜』 - 图像选择器 我们可以使用以下常规的图像获取方式
图库与相册
系统自带 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 override func touchesBegan (touches: Set<UITouch>, withEvent event: UIEvent?) { let sheet = UIAlertController (title: "图片选择" , message: "简单版的三种选择" , preferredStyle: .ActionSheet ) if (UIImagePickerController .isSourceTypeAvailable(.Camera )) { sheet.addAction(UIAlertAction .init (title: "Camera" , style: .Default , handler: { _ in self .showPhotoes(.Camera ) })) } sheet.addAction(UIAlertAction .init (title: "PhotoLibrary" , style: .Default , handler: { _ in self .showPhotoes(.PhotoLibrary ) })) sheet.addAction(UIAlertAction .init (title: "SavedPhotosAlbum" , style: .Default , handler: { _ in self .showPhotoes(.SavedPhotosAlbum ) })) sheet.addAction(UIAlertAction .init (title: "Cancel" , style: .Cancel , handler: nil )) presentViewController(sheet, animated: true , completion: nil ) }
1 2 3 4 5 6 7 8 func showPhotoes (source: UIImagePickerControllerSourceType) { let controller = UIImagePickerController () controller.delegate = self controller.sourceType = source controller.allowsEditing = source == .SavedPhotosAlbum ? true :false self .presentViewController(controller, animated: true , completion: nil ) }
UIImagePickerController的代理
1 2 3 4 5 6 7 8 func imagePickerControllerDidCancel (picker: UIImagePickerController) { dismissViewControllerAnimated(true , completion: nil ) } func imagePickerController (picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {}
1 2 3 4 5 6 7 8 9 10 UIImagePickerControllerMediaType : StringUIImagePickerControllerOriginalImage : UIImage UIImagePickerControllerEditedImage : UIImage UIImagePickerControllerCropRect : NSValue -> CGRect UIImagePickerControllerMediaURL : NSURL UIImagePickerControllerLivePhoto : StringUIImagePickerControllerMediaMetadata : NSDictionary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ▿ 5 elements ▿ [0] : 2 elements - .0 : "UIImagePickerControllerCropRect" ▿ [1] : 2 elements - .0 : "UIImagePickerControllerOriginalImage" ▿ [2] : 2 elements - .0 : "UIImagePickerControllerReferenceURL" - .1 : assets-library: //asset/asset .JPG ?id=99 D53A1F-FEEF -40 E1-8 BB3-7 DD55A43C8B7&ext=JPG ▿ [3] : 2 elements - .0 : "UIImagePickerControllerMediaType" - .1 : public.image ▿ [4] : 2 elements - .0 : "UIImagePickerControllerEditedImage"
自定义
遍历相册的所有图片1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 func loadLocalPhotoes () { var countOne = 0 assetsLibrary.enumerateGroupsWithTypes(ALAssetsGroupSavedPhotos , usingBlock: { (group: ALAssetsGroup !, stop) in print ("is goin" ) if group != nil { let assetBlock : ALAssetsGroupEnumerationResultsBlock = { (result: ALAsset !, index: Int , stop) in if result != nil { self .assets.append(result) countOne++ } } group.enumerateAssetsUsingBlock(assetBlock) print ("assets:\(countOne) " ) self .startChangeLocalImages(0 ) } }, failureBlock: { (fail) in print (fail) }) } func startChangeLocalImages (var index: Int) { if index==assets.count { index = 0 } let myAsset = assets[index] let image = UIImage (CGImage :myAsset.thumbnail().takeUnretainedValue()) self .backImageView.image = image let popTime = dispatch_time(DISPATCH_TIME_NOW , Int64 (1 * Double (NSEC_PER_SEC ))) dispatch_after(popTime, dispatch_get_main_queue()) { self .startChangeLocalImages(index+1 ) } }
iOS9 开始使用新库PHPhotoLibrary