iOS 9 Swift Programming Cookbook

所需积分/C币:10 2015-10-27 09:16:09 7.66MB PDF

教你使用Swift语言开发iOS9应用,想要的快下
do, t Let s say Errortype enun Errors ErrorT case EmptylastName func fullNameFromFirstName (fName: String, lastName: String) throws - String f if fName, characters. count throw Errors. EmptyFirstName f lastName. characters. count ==0f throw Errors. EmptylastName return fName n+lastName func example1()I et fullName try fullNamefromFirstName ("Foo", lastName:Bar) print(fullName print( An error occurred" catch grained manncr. Lcts say that you want to trap crrors in fun print ( fullName tch⊥ete1 //handle this specific type of error here int(err) atch let ex as NSException I //handle exceptions here Bind variables to optionals and usc thosc variables in the rest of your mcthods body Let's have a look at a method that takes an optio NSData String func stringFromData(data: NSData?)-> String?[ guard let. data data data NSUTEStrir return nil eturn String(st stringFromData(nil) print (Got the string") j else nil to this method for now and trigger the failure block("No string came back?). What if we passed valid data? And to hav with this let,s create our nsdata quard quard func example2()i guard let data NSString(string dataUsingEncoding(NSUTF8StringEncoding) where data length >0 else t return if le- str= stringFromData(data)i print ("Got the string \(str)") print ("No string came back) func example3(firstNamefirstName: String?, lastName: Strin g:r age: UInt first characters. count >0 && last characters. count >0 else( print(first, " last Let's say that you want to define a method that takes in a string and renders it inside a new image context with a given size case Emptystring func imageFcrString (str: string, size: CGsize) throws - UIImage t eter UIGraphicsEndImageContext() UIGraphicsBeginImageContextWithOptions(size, true, 0) throw erro eturn UIGraphicsGetImageFromCurrentImageContext( I dont want to put print() or even better, grab the source code for this books example code from GitHub, where I have er let i try imageForString(Foo", size: CGSize(width: 100, height: 50)) rint (i) y catch let excep( toe #aVai⊥ab⊥e wc've all bccn waiting for this for a vcry long timc. The days of having to call the respondsToSelector: Let's say that we want to write a method that can read an array of bytes from an NSData NsData byTes enum Errors Errortype i case e func bytesFromData (data: NSData) throws - [UInt8]( f(aata,⊥ ength==0) throw Errors. Empt v Data var buffer [UInt8(count: data length, repeatedvalue: 0) f available(10s 8.1,*)I dat length: dat e1se「 data. getBytes(abuffer return buff quard let data ="Foo. dataUsingEncoding(nSUTE8StringEncoding) else return ⊥ et byte try bytesFromData(data) print ("Data =\(bytes)") print Failed to get bytes) You have many assets in your app for various circumstances, and want to save storage space and network usage on each user's d NSBundleResourceRequest be called "levell”, the second "levcl2”, and the third“ level nages in your first asset pack and open the attribute inspector. In the" On Demand Resource Tags "section of the inspector, enter"level1 and do level1-1 m□□ level1-2 evel1-3 a level2-1 a leve12-2 L level2-3 c level3-1 w level3-2 回回回回回回 |eve|3-3 Now,in your UI, placc three buttons and three image vicws, hook the buttons'actions to the codc, and hook the image vicw rc (dIBOutlet var imgI: UIImageview! @TROutlet var img2: UIImageview @IBOutlet var img 3: UIImageview return [ self. img l, self im2, self. img3 1 conditionallyBeginAccessingResourcesWithCompletionHandler function on your resource request. Dont blame me didnt name this function. This will return a boolean of yes no to tell you whether you have or dont have access to the resource. If you dont beginAccessingResourcesWithcomplecionHandler send for our asset pack so that the next time our buttons are tapped, we dont have to check their var currentResourcePack: NSBundleResourceRequest? NSOperationQueue, mainQueue(),addoperationwithBlocki for n in c.<self self imageviews [nl. image UIImage(named: tag " -\(n+1) func or lng 1I 1 img. Image=ni⊥ let ta if let reg currentResourcePacki reg. endAccessingResources( currentResourcePack NSBundleresourceRequest(tags: [tag]) guard let reg =currentResourcePack else return req. conditionallyBeginAccessingResourcesWithcommpletionHandlerfavailable in if ava1⊥ab⊥e{ self display ImagesForResourceTag(tag) t else g. beginAcce ResourcesWithCompleti i1 else dle the error her If display @IBAction func useLevel3(sender: Anyobject)I @IBAction func uselevel2(sender: AnyObject)I @IBAction func useLevell(sender: Anyobject)[ uselevel(l) On Demand Resources Tag Size Status Not downloaded 352KB Open Files Descriptor Type Size/Offset Path 50,331,650 268,435,460 /dev/ttys 004 CHR 268.435460 352 ev/ttys004 18470,527 /Users/vandadnpLibrary/Developer/Core Simulator. the first ass status will change to "In Use". Once you switch from that pack to another, the previously chosen pack will be set to"Downloaded"and ready to be You want to extract your app's binary for a spccific device architecturc to find out how big your Choose the" Save for Ad Hoc Deployment option in the new screen and press Next. new window, choose "Export for spccific device and then choose your device from the list With ioS, Apple introduced bitcodes. This is Apple's way of specifying how the bin

...展开详情

评论 下载该资源后可以进行评论 15

chgsh_2008 不错资源,感谢,好评!
2019-03-26
回复
roboter888 资源非常不错,很适合学习。推荐!
2018-02-26
回复
pandawpp 继续努力学习
2017-05-18
回复
fanyanqin56 非常好,赞一个
2016-10-04
回复
Jessciaphoenixw 好资源必须给好评
2016-09-21
回复
img

关注 私信 TA的资源

上传资源赚积分,得勋章
最新资源