Tag: 迅速

如何在Swift中列出符合协议的所有类?

如何列出在Swift中实现给定协议的所有类? 假设我们有一个例子: protocol Animal { func speak() } class Cat:Animal { func speak() { print("meow") } } class Dog: Animal { func speak() { print("Av Av!") } } class Horse: Animal { func speak() { print("Hurrrr") } } 这是我目前的(不可编译的)方法: func getClassesImplementingProtocol(p: Protocol) -> [AnyClass] { let classes = objc_getClassList() var ret = [AnyClass]() for cls […]

使用新的Swift 3和AlamofireparsingJSON

我使用Alamofire作为HTTP库,因为Swift 3的更新,如何根据下面的例子来parsingJSON? Alamofire.request("https://httpbin.org/get").responseJSON { response in debugPrint(response) if let json = response.result.value { print("JSON: \(json)") } } respone.result.value是任何对象,是非常新的和混乱的。

在Swift中简单的UITableView – 意外地发现了零

很简单的代码: func numberOfSectionsInTableView(tableView: UITableView?) -> Int { return 1 } func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int { return 5 } func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! { let cell: BookTableViewCell = BookTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "BookCell") println("ip: \(indexPath.row)") cell.bookLabel.text = "test" return cell } 在cell.bookLabel.text行我得到这个: fatal error: unexpectedly found nil while unwrapping an Optional value BookTableViewCell是这样定义的: […]

无法使用types为“(ChecklistItem)”的参数列表调用“indexOf”

当我正在写代码从数组中find一个项目使用indexOf时,它显示了上述错误。 这是我的代码: func addItemViewController(controller: AddItemViewController, didFinishEditingItem item: ChecklistItem) { if let index = items.indexOf(item) { let indexPath = NSIndexPath(forRow: index, inSection: 0) if let cell = tableView.cellForRowAtIndexPath(indexPath) { configureTextForCell(cell, withChecklistItem: item) } }

Swift UITableView reloadData在闭包中

我相信我有一个问题,我的closures发生在后台线程和我的UITableView没有足够快的更新。 我打电话给一个REST服务,并在我的closures,我有一个tableView.reloadData()调用,但这需要几秒钟发生。 如何使数据重新加载更快(也许在主线程上)? REST查询函数 – 使用SwiftyJSON库进行解码 func asyncFlightsQuery() { var url : String = "http://127.0.0.1:5000/flights" var request : NSMutableURLRequest = NSMutableURLRequest() request.URL = NSURL(string: url) request.HTTPMethod = "GET" NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, networkData: NSData!, error: NSError!) -> Void in var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil // Parse with SwiftyJSON let json = JSON(data: networkData) // […]

types'String.Index'不符合协议'IntegerLiteralConvertible'

随着Beta 3一切正常,现在我得到一个奇怪的错误,我不知道如何解决它。 尝试了所有类似的问题的解决scheme。 这是我的代码: if !name.isEmpty { var splitted: [String] = name.componentsSeparatedByString(" ") for curPart in splitted { if !curPart.isEmpty { acronym += curPart.substringToIndex(1) //Error } } if (acronym as NSString).length > 2 { acronym = acronym.substringToIndex(2) //Error } } 两个标记都给了我同样的错误: types'String.Index'不符合协议'IntegerLiteralConvertible' 有人能帮我吗? 还是Beta 4被窃听? 谢谢!

与自定义相机Swift 3拍照

在Swift 2.3中,我使用这个代码在自定义相机中拍照: func didPressTakePhoto(){ if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) { stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in if sampleBuffer != nil { let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) let dataProvider = CGDataProviderCreateWithCFData(imageData) let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault) let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right) self.captureImageView.image = image } }) } } […]

Swift:沿着贝塞尔path渐变(使用CALayers)

我有一个相对直观的实现与CALayer对象设置的进度视图。 进度视图本身是UIView的子视图。 以下是设置进度环的代码: self.progressRingLayer = CAShapeLayer() let innerRect = CGRectInset(bounds, CGFloat(self.lineWidth) / 2, CGFloat(self.lineWidth) / 2) let innerPath = UIBezierPath(ovalInRect: innerRect) self.progressRingLayer.path = innerPath.CGPath self.progressRingLayer.fillColor = UIColor.clearColor().CGColor self.progressRingLayer.strokeColor = kProgressColor.CGColor self.progressRingLayer.anchorPoint = CGPointMake(0.5, 0.5) self.progressRingLayer.transform = CATransform3DRotate(self.progressRingLayer.transform, (CGFloat(M_PI))*1, 0, 0, 1) self.progressRingLayer.lineCap = kCALineCapRound self.progressRingLayer.lineWidth = CGFloat(self.lineWidth) self.layer.addSublayer(self.progressRingLayer) 我现在要做的就是向progressRingLayer添加一个渐变(跟随(或弯曲))path。 我已经成功地为填充添加线性渐变,但不是仅添加到path。 这里是我想要什么效果的一个例子: 到目前为止,我发现的所有东西都需要一些CoreGraphics和CGContext的附加步骤,这些步骤不太适合我的实现。 任何帮助将是伟大的,谢谢!

让AVAudioPlayer一次播放多个声音

我试图获得多个声音文件在AVAudioPlayer实例上播放,但是当一个声音播放时,另一个停止。 我一次只能听到一个以上的声音。 这是我的代码: import AVFoundation class GSAudio{ static var instance: GSAudio! var soundFileNameURL: NSURL = NSURL() var soundFileName = "" var soundPlay = AVAudioPlayer() func playSound (soundFile: String){ GSAudio.instance = self soundFileName = soundFile soundFileNameURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundFileName, ofType: "aif", inDirectory:"Sounds")!) do{ try soundPlay = AVAudioPlayer(contentsOfURL: soundFileNameURL) } catch { print("Could not play sound file!") […]

如何在一个方法中实现viewDidLoad,而不是再次调用如;

我想在一个方法中重新加载ViewdidLoad但是现在想再次调用像[self viewDidLoad]; 这可能吗?