Questions tagged «swift»

Swift是Apple Inc.针对其平台和Linux开发的一种安全,快速且具有表现力的通用编程语言。Swift是开源的。仅将标记用于有关语言功能或在Swift中需要代码的问题。将标签[ios],[ipados],[macos],[watch-os],[tvos],[cocoa-touch]和[cocoa]用于有关平台或框架的(语言不可知)问题。

3
快速平等协议
我正在为Swift编写本教程:https : //www.raywenderlich.com/125311/make-game-like-candy-crush-spritekit-swift-part-1并遇到以下代码: func == (lhs: Cookie, rhs: Cookie) -> Bool { return lhs.column == rhs.column && lhs.row == rhs.row } 我正是这样写的,但是Xcode给出了我这些错误: Consecutive declarations on a line must be separated by ';' Expected declaration operators are only allowed at global scope 我从苹果的文档中找到了以下代码:https : //developer.apple.com/documentation/swift/equatable 这与我写的非常相似。怎么了?对我来说,这似乎是个错误。我正在使用Xcode 6 Beta 2 编辑: 这是我的整个Cookie课: …
85 ios  swift  xcode6 

3
Swift中的切片是什么?
Swift中的切片是什么?它与数组有何不同? 从文档中,下标(Range)的类型签名为: subscript(Range<Int>) -> Slice<T> 为什么不返回另一个Array<T>而不是一个Slice<T>? 看起来我可以将一个切片与数组连接起来: var list = ["hello", "world"] var slice: Array<String> = [] + list[0..list.count] 但这会产生错误: 无法找到接受提供的参数的“下标”的重载 var list = ["hello", "world"] var slice: Array<String> = list[0..list.count] 什么是切片?
85 swift  slice 

2
Xcode 8自动生成的快速帮助文档
在Xcode 8中宣布的新功能中,我们可以看到 “自动生成快速帮助文档”: 就像我们可以从NSHipster上学习本文一样,手动代码文档到此结束了吗? 有人可以概述此新功能的优缺点吗?它会取代Jazzy之类的工具吗? 我找不到有关新功能的更多信息。
85 xcode  swift  xcode8 

5
如何在Swift中创建通用协议?
我想使用一种接受通用输入并返回通用值的方法来创建协议。 到目前为止,这是我尝试过的方法,但是会产生语法错误。 使用未声明的标识符T。 我究竟做错了什么? protocol ApiMapperProtocol { func MapFromSource(T) -> U } class UserMapper: NSObject, ApiMapperProtocol { func MapFromSource(data: NSDictionary) -> UserModel { var user = UserModel() as UserModel var accountsData:NSArray = data["Accounts"] as NSArray return user } }
85 swift 

9
使用Xcode 6.1 / Swift以编程方式将图像设置为UIImageView
我试图在Xcode 6.1中以编程方式设置UIImageView: @IBOutlet weak var bgImage: UIImageView! var image : UIImage = UIImage(named:"afternoon")! bgImage = UIImageView(image: image) bgImage.frame = CGRect(x: 0, y: 0, width: 100, height: 200) view.addSubview(bgImage) 但是Xcode表示bgImage = UIImageView(image: image) Image的“预期声明”"afternoon"是PNG,而我的理解是PNG不需要在XCode 6.1中进行扩展。 也尝试过bgImage.image = UIImage(named: "afternoon"),但仍然得到: 更新 好的,我已将要更新的代码UIImageView放入viewDidLoad函数中,但UIImageView仍未显示图像(该图像以下午.png的形式存在于基本目录中): @IBOutlet weak var bgImage: UIImageView! @IBOutlet weak var dateLabel: UILabel! …
85 ios  xcode  swift 

6
带有快速变量的NSLocalizedString
我正在尝试使用NSLocalizedString本地化我的应用程序。当我导入XLIFF文件时,大多数都像超级按钮一样工作,但是有些却没有,并且某些字符串未本地化。我注意到问题出在NSLocalizedString中,其中包含一些变量,例如: NSLocalizedString(" - \(count) Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare") 要么 NSLocalizedString("Notifica per \(medicina!) della prescrizione \(prescription!)\nMemo: \(memoTextView.text)", comment: "Messaggio della Local Notification") 也许这不是这类东西的正确语法。有人可以向我解释如何迅速做到这一点?非常感谢你。



11
将小数点后两位取整
如何currentRatio将小数点后两位取整? let currentRatio = Double (rxCurrentTextField.text!)! / Double (txCurrentTextField.text!)! railRatioLabelField.text! = "\(currentRatio)"
85 swift  double 

14
Swift-拐角半径和阴影问题
我正在尝试创建一个带有圆角和阴影的按钮。无论我如何切换,该按钮都将无法正确显示。我尝试了masksToBounds = false和masksToBounds = true,但是要么拐角半径有效,阴影不起作用,要么阴影起作用,并且拐角半径不限制按钮的拐角。 import UIKit import QuartzCore @IBDesignable class Button : UIButton { @IBInspectable var masksToBounds: Bool = false {didSet{updateLayerProperties()}} @IBInspectable var cornerRadius : CGFloat = 0 {didSet{updateLayerProperties()}} @IBInspectable var borderWidth : CGFloat = 0 {didSet{updateLayerProperties()}} @IBInspectable var borderColor : UIColor = UIColor.clearColor() {didSet{updateLayerProperties()}} @IBInspectable var shadowColor : …

20
在Swift中设置UITextField的最大字符长度
我知道还有其他主题,但似乎无法找到实现方法。 我试图将UITextField限制为仅5个字符 最好是字母数字和-和。和_ 我看过这段代码 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let maxLength = 4 let currentString: NSString = textField.text let newString: NSString = currentString.stringByReplacingCharactersInRange(range, withString: string) return newString.length <= maxLength } 和 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { …

13
如何使用Swift从一个View Controller导航到另一个
我想从一个视图控制器导航到另一个。如何将以下Objective-C代码转换为Swift? UIViewController *viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"Identifier"]; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewController]; [self.navigationController pushViewController:navi animated:YES];




By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.