Tag: 迅速

如何在iOS图表的X轴上添加string?

随着新版本的发布,我有一些麻烦来创build一些图表,以前的代码是: func setChart(dataPoints: [String], values: [Double]) { var dataEntries: [BarChartDataEntry] = [] for i in 0..<dataPoints.count { let dataEntry = BarChartDataEntry(value: values[i], xIndex: i) dataEntries.append(dataEntry) } let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold") let chartData = BarChartData(xVals: months, dataSet: chartDataSet) barChartView.data = chartData } 您可以使用以下行将值传递给例如几个月的数组: let chartData = BarChartData(xVals: months, dataSet: chartDataSet) 新版本之后,实现相同graphics的代码是: func […]

Swift中的不可变/可变集合

我指的是Apple Swift编程指南,用于理解Swift语言中可变/不可变对象(数组,字典,集合,数据)的创build。 但是我不明白如何在Swift中创build一个不可变集合。 我想在Objective-C中的Swift中看到下面的等价物 不可变arrays NSArray *imArray = [[NSArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; 可变数组 NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; [mArray addObject:@"Fourth"]; 不可变的词典 NSDictionary *imDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Value1", @"Key1", @"Value2", @"Key2", nil]; 可变的字典 NSMutableDictionary *mDictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Value1", @"Key1", @"Value2", @"Key2", nil]; [mDictionary setObject:@"Value3" forKey:@"Key3"];

在xcode中制作RGB颜色

我正在使用从照片店的颜色的RGB值,并在xcode中使用相同的值。颜色R-160,G-97,B-5 …在Photoshop中的颜色显示为淡黄色,但在Xcode时,当我使用 myLabel.textColor= [UIColor colorWithRed:160 green:97 blue:5 alpha:1] ; 颜色显得发白。 为什么这种差异正在发生?

迅速的NSTimer在背景中

我遇到了很多关于如何在后台处理NSTimer的问题。 我已经尝试了所有的选项之一,实际上是有道理的..当应用程序去背景停止计时器 NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil) 和 NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidBecomeActive", name: UIApplicationWillEnterForegroundNotification, object: nil) 起初我以为我的问题解决了,我只是节省了时间,当应用程序进入背景,并计算出应用程序进入前台时的差异。但后来我注意到,时间实际上是推迟了3,4,5秒。 。它实际上是不一样的。我已经比较了另一台设备上的秒表。 有没有真正的任何SOLID解决scheme在后台运行NSTimer?

Swift UIColor初始值设定项 – 仅在定位iPhone5时才编译错误

情况: 从一个新项目工作: iOS>应用程序>游戏> SpriteKit,Swift,iPhone 在这里我已经写了一个函数在我的GameScene.swift接受一个hex颜色值和alpha的双。 我正在通过Swift文档,并参考我在网上看到的一个优雅的解决scheme。 我注意到这个确切的代码编译,运行并显示模拟器中iPhone5构build目标的正确颜色。 但… 当我的构build目标设置为iPhone5时,完全相同的项目甚至不会编译。 无法find接受提供的参数的“init”的重载。 function: // colorize function takes HEX and Alpha converts then returns aUIColor object func colorize (hex: Int, alpha: Double = 1.0) -> UIColor { let red = Double((hex & 0xFF0000) >> 16) / 255.0 let green = Double((hex & 0xFF00) >> 8) / 255.0 let […]

在Swift中传递WebService中的参数

我正在学习Swift,我不知道如何使用Swift将参数发送到服务器。 在Objective-C中,我们可以使用"%@"作为占位符。 但是,Swift的情况下应该怎么做,假设我有一个需要电子邮件和密码的loginWeb服务。 现在我想知道的是,我将如何发送logintextfield和passwordtextfield文本到服务器,如, var bodyData = "email=logintextfield.text&password=passwordtextfield.text"

应用程序终止时执行后台任务

我正在为我的学校创build一个应用程序,如果网站上有新的标记,应该每n分钟检查一次。 要在用户首次login时执行此操作,实际标记的编号将保存在“UserDefaults”中。 当应用程序被终止时,在n分钟之后,标记的数量被重新logging并与前一个比较,并且在数字被改变的情况下发送通知。 我想知道是否有办法执行这个任务。 我已经尝试在-applicationWillTerminate中创build一个计时器,但它只被触发一次。 这是我试过的: func applicationWillTerminate(_ application: UIApplication) { DispatchQueue.main.async { self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(AppDelegate.findMark), userInfo: nil, repeats: true) self.timer.fire() } } select器findMark是任务。 提前致谢!

绘制类绘制直线而不是曲线

我有下面的代码,使用UIBezierPath绘制线条。 代码使用addCurveToPoint ,它应该使用三次bezierpath绘制曲线,但代码的最终结果是绘制连接的直线,但addLineToPoint没有被使用。 可能会发生什么,为什么不是绘制曲线的代码? import UIKit class DrawingView: UIView, UITextFieldDelegate { // Modifiable values within the code let lineWidth : CGFloat = 2.0 let lineColor = UIColor.redColor() let lineColorAlpha : CGFloat = 0.4 let shouldAllowUserChangeLineWidth = true let maximumUndoRedoChances = 10 var path = UIBezierPath() var previousImages : [UIImage] = [UIImage]() // Represents current image […]

Swift希望将#selector的参数暴露给Objective-C

我有一个Swift项目,我想将一个方法附加到UIButton的轻敲事件。 我有以下代码: class MyClass { let myButton = UIButton(frame: CGRectMake(50, 50, 100, 50)) init() { myButton.addTarget(self, #selector(self.didTap(_:)), forControlEvents: .TouchUpInside) } func didTap(sender: UIButton) { print("Tapped") } } XCode突出显示我的addTarget行,并说: Argument of '#selector' refers to a method that is not exposed to Objective-C 如果我添加@objc前缀到我的func didTap喜欢它build议然后一切工作正常。 我是否在我的构build设置中启用了导致这种奇怪行为的东西? PS。 我在7.3.1中得到这个行为。 但是,如果我在7.2.1中尝试这样做,它不接受#selector(method(_:))语法,而Selector("method:")正常工作。

将图像转换为CVPixelBuffer进行机器学习Swift

我正在尝试在2017年的WWDC上演示的苹果示例Core ML型号正常工作。 我正在使用GoogLeNet尝试对图像进行分类(请参阅Apple机器学习页面 )。 该模型将CVPixelBuffer作为input。 我有一个名为imageSample.jpg的图片,我正在使用这个演示。 我的代码如下: var sample = UIImage(named: "imageSample")?.cgImage let bufferThree = getCVPixelBuffer(sample!) let model = GoogLeNetPlaces() guard let output = try? model.prediction(input: GoogLeNetPlacesInput.init(sceneImage: bufferThree!)) else { fatalError("Unexpected runtime error.") } print(output.sceneLabel) 我总是得到输出中意外的运行时错误,而不是图像分类。 我的代码转换图像如下: func getCVPixelBuffer(_ image: CGImage) -> CVPixelBuffer? { let imageWidth = Int(image.width) let imageHeight = Int(image.height) let attributes : […]