Questions tagged «swift»

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

12
导航栏rightbaritem图像按钮错误iOS 11
此代码在ios10中可以正常工作。我得到我的标签和一个图像按钮,该按钮是用户照片资料,圆形。.好的。但是当运行xcode 9 ios11模拟器时,我将其拉长了。按钮框架必须是32x32,当在sim上检查并获取视图并告诉xcode描述视图时,我将输出为170x32或类似的东西。 这是我的代码。 let labelbutton = UIButton( type: .system) labelbutton.addTarget(self, action:#selector(self.toLogin(_:)), for: .touchUpInside) labelbutton.setTitleColor(UIColor.white, for: .normal) labelbutton.contentHorizontalAlignment = .right labelbutton.titleLabel?.font = UIFont.systemFont(ofSize: 18.00) let button = UIButton(type: .custom) button.addTarget(self, action:#selector(self.toLogin(_:)), for: .touchUpInside) button.frame = CGRect(x: 0, y: 0, width: 32, height: 32) button.setTitleColor(UIColor.white, for: .normal) button.setTitleColor(UIColor.white, for: .highlighted) var …
101 ios  swift  ios11  xcode9-beta  swift4 

11
在Swift中将参数附加到button.addTarget动作
我试图将一个额外的参数传递给buttonClicked动作,但是无法计算出Swift中的语法。 button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside) 任何我的buttonClicked方法: func buttonClicked(sender:UIButton) { println("hello") } 有任何想法吗? 谢谢你的帮助。
101 swift  button  uibutton 

19
在Swift中将String转换为Float
我正在尝试转换从UITextField中获取的数字(我认为这些数字实际上是字符串),并将其转换为Float,以便可以对其进行乘法运算。 我有两个UITextfield声明如下: @IBOutlet var wage: UITextField @IBOutlet var hour: UITextField 当用户按下UIButton时,我想计算用户赚取的工资,但是我不能,因为我需要先将其转换为浮动工资,然后才能使用它们。 我知道如何通过执行以下操作将它们转换为整数: var wageConversion:Int = 0 wageConversion = wage.text.toInt()! 但是,我不知道如何将它们转换为浮点数。


5
在Swift中什么是“获取”和“设置”?
我正在学习Swift并且正在阅读 The Swift Programming Language从Apple,我没有Objective C背景(只有PHP,JS和其他语言,但是没有Obj C) 在24-25页上,我看到以下代码: //...Class definition stuff... var perimeter: Double { get { return 3.0 * sideLength } set { sideLength = newValue / 3.0 } } //...Class continues... 这部分没有在书中指定,我无法理解它们的用途。 谁能解释我得到和设定的是什么?
101 swift  accessor 

8
如何创建固定大小的对象数组
在Swift中,我试图创建一个由64个SKSpriteNode组成的数组。我想先将其初始化为空,然后将Sprites放在前16个单元格中,然后将最后16个单元格中(模拟象棋游戏)。 根据我在文档中了解的内容,我期望会出现以下情况: var sprites = SKSpriteNode()[64]; 要么 var sprites4 : SKSpriteNode[64]; 但这是行不通的。在第二种情况下,我收到一条错误消息:“尚不支持定长数组”。那可以是真的吗?对我来说,这听起来像一个基本功能。我需要直接通过其索引访问元素。
101 swift  ios8  xcode6 

20
如何在Swift中为UIImage着色?
我有一张名为的图片arrowWhite。我想将此图像着色为黑色。 func attachDropDownArrow() -> NSMutableAttributedString { let image:UIImage = UIImage(named: "arrowWhite.png")! let attachment = NSTextAttachment() attachment.image = image attachment.bounds = CGRectMake(2.25, 2, attachment.image!.size.width - 2.25, attachment.image!.size.height - 2.25) let attachmentString = NSAttributedString(attachment: attachment) let myString = NSMutableAttributedString(string: NSString(format: "%@", self.privacyOptions[selectedPickerRow]) as String) myString.appendAttributedString(attachmentString) return myString } 我想得到这张图片blackColour。 tintColor不管用...
101 ios  image  swift  colors  uiimage 

5
协议只能用作一般约束,因为它具有Self或relatedType要求
我有一个协议RequestType,它具有如下的relatedType模型。 public protocol RequestType: class { associatedtype Model var path: String { get set } } public extension RequestType { public func executeRequest(completionHandler: Result<Model, NSError> -> Void) { request.response(rootKeyPath: rootKeyPath) { [weak self] (response: Response<Model, NSError>) -> Void in completionHandler(response.result) guard let weakSelf = self else { return } if …

9
在Swift中将对象数组映射到Dictionary
我有个Person的对象数组: class Person { let name:String let position:Int } 数组是: let myArray = [p1,p1,p3] 我想映射myArray成为字典[position:name]的经典解决方案是: var myDictionary = [Int:String]() for person in myArray { myDictionary[person.position] = person.name } 有雨燕任何优雅的方式做到这一点与功能的办法map,flatMap......或其他现代风格的雨燕
101 ios  arrays  swift  dictionary 

8
在Swift中实现失败的初始化程序的最佳实践
通过以下代码,我尝试定义一个简单的模型类,它是可失败的初始化器,该初始化器将(json-)字典作为参数。nil如果用户名未在原始json中定义,则初始化程序应返回。 1.代码为什么不编译?错误消息显示: 从初始化器返回nil之前,必须初始化类实例的所有存储属性。 那没有道理。为什么计划返回时初始化那些属性nil? 2.我的方法正确吗?还是有其他想法或通用模式可以实现我的目标? class User: NSObject { let userName: String let isSuperUser: Bool = false let someDetails: [String]? init?(dictionary: NSDictionary) { if let value: String = dictionary["user_name"] as? String { userName = value } else { return nil } if let value: Bool = dictionary["super_user"] as? Bool { …

9
如何调试“发送到实例的无法识别的选择器”错误
我正在为表格视图创建自定义表格单元格视图。将自定义单元格(在情节提要中)的图像视图快速连接到代码后,出现以下错误。 [UITableViewCellContentView image]: unrecognized selector sent to instance 0x7fb4fad7fd20' *** First throw call stack: ( 0 CoreFoundation 0x000000010ccbb3f5 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010e7e9bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010ccc250d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010cc1a7fc ___forwarding___ + 988 4 CoreFoundation 0x000000010cc1a398 _CF_forwarding_prep_0 + 120 5 UIKit 0x000000010d7d8881 -[UITableViewCell …
100 ios  swift  uitableview 



16
故事板中带有自定义字体的属性字符串无法正确加载
我们在项目中使用自定义字体。它在Xcode 5中运行良好。在Xcode 6中,它在纯文本(代码中的属性字符串)中运行。但是,在演示板或设备上运行时,在情节提要中设置的那些归因字符串都将还原为Helvetica,尽管它们在情节提要中看起来还不错。 我不确定这是Xcode 6还是iOS 8 SDK的错误,还是Xcode 6 / iOS 8中更改了自定义字体的使用方式?


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.