Questions tagged «swift»

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

6
与逻辑AND运算符&&一起使用Swift
我们知道我们可以使用一条if let语句作为简写形式来检查可选的nil,然后将其解包。 但是,我想使用逻辑AND运算符将其与另一个表达式组合&&。 因此,例如,在这里,我进行可选的链接来解包,也可以选择将我的rootViewController下放到tabBarController。但是,与其嵌套if语句,不如将它们组合在一起。 if let tabBarController = window!.rootViewController as? UITabBarController { if tabBarController.viewControllers.count > 0 { println("do stuff") } } 联合捐赠: if let tabBarController = window!.rootViewController as? UITabBarController && tabBarController.viewControllers.count > 0 { println("do stuff") } } 上面给出了编译错误使用未解析的标识符'tabBarController' 简化: if let tabBarController = window!.rootViewController as? UITabBarController && true { …
93 swift  expression 

15
如何使枚举符合Swift中的协议?
Swift文档说,类,结构和枚举都可以符合协议,我可以说它们都符合。但是我无法让枚举的行为完全类似于类和结构示例: protocol ExampleProtocol { var simpleDescription: String { get set } mutating func adjust() } class SimpleClass: ExampleProtocol { var simpleDescription: String = "A very simple class." var anotherProperty: Int = 69105 func adjust() { simpleDescription += " Now 100% adjusted." } } var a = SimpleClass() a.adjust() let aDescription …

4
如何快速进行可选的关闭?
我试图在Swift中声明一个需要可选闭包的参数。我声明的函数如下所示: class Promise { func then(onFulfilled: ()->(), onReject: ()->()?){ if let callableRjector = onReject { // do stuff! } } } 但是Swift抱怨说,“ if let”声明时,“条件中的绑定值必须是Optional类型”。

12
尚不支持类变量
我将拆分视图控制器作为初始视图控制器开始我的项目,然后从情节提要自动启动它。 通常,具有此UI的应用程序具有一个且只有一个拆分视图控制器作为根,因此我在子类中创建一个静态变量,并在完成初始化时对其进行设置。 所以我想尽快尝试这种行为。 我在iBook上阅读了有关Type属性(带有static和class关键字)的Swift编程语言指南,并尝试了一段代码来完成这项工作: import UIKit class SplitViewController: UISplitViewController { class func sharedInstance() -> SplitViewController { return SplitViewController.instance } class let instance: SplitViewController = nil init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) self.initialization() } init(coder aDecoder: NSCoder!) { super.init(coder: aDecoder); self.initialization() } func initialization() { SplitViewController.instance …
93 xcode  swift 


9
我可以更改UIActivityIndi​​cator的大小吗?
无论我在分配时给它多大的尺寸,它仅显示固定的尺寸。有可能增加吗? 码: activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(142.00, 212.00, 80.0, 80.0)]; [[self view] addSubview:activityIndicator]; [activityIndicator sizeToFit]; activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin); activityIndicator.hidesWhenStopped = YES; activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;

10
Swift-具有两行文本的UIButton
我想知道是否可以用两行文本创建一个UIButton。我需要每行具有不同的字体大小。第一行将是17点,第二行将是11点。我试过将两个标签放在UIButton内,但我无法使它们停留在按钮的范围内。 我正在尝试在ui生成器中而不是通过编程来完成所有这些操作。 谢谢
93 ios  swift  uibutton 

20
字符0周围的Alamofire无效值
Alamofire.request(.GET, "url").authenticate(user: "", password: "").responseJSON() { (request, response, json, error) in println(error) println(json) } 这是我对Alamofire的请求,对于某些请求有时可以工作,但是有时我得到: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x78e74b80 {NSDebugDescription=Invalid value around character 0.}) 我已经读到这可能是由于无效的JSON引起的,但是响应是我在JSON验证程序中验证为有效的静态JSON字符串。它确实包含åö字符和一些HTML。 为什么有时会出现此错误?
93 ios  swift  alamofire 

21
如何将UIView转换为图像
我想将UIView转换为图像并将其保存在我的应用程序中。有人可以告诉我如何拍摄视图的屏幕截图或将其转换为图像,以及将其保存在应用程序(而不是相机胶卷)中的最佳方法是什么?这是该视图的代码: var overView = UIView(frame: CGRectMake(0, 0, self.view.frame.width/1.3, self.view.frame.height/1.3)) overView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)-self.view.frame.height/16); overView.backgroundColor = UIColor.whiteColor() self.view.addSubview(overView) self.view.bringSubviewToFront(overView)
93 ios  swift  uiview  screenshot 


11
更改按钮中png的颜色-iOS
我有一组创建的图标,它们是透明的白色PNG: 而我想做的就是能够将它们着色为其他颜色。例如蓝色,灰色等 我注意到,“单击/点击”它们会自动变为灰色。因此,我想我可以通过点击或将其正常状态更改为我喜欢的灰色: 实现这一目标的最佳方法是什么?
93 ios  swift 

11
从Swift函数中的异步调用返回数据
我已经在我的Swift项目中创建了一个实用程序类,用于处理所有REST请求和响应。我建立了一个简单的REST API,因此可以测试我的代码。我创建了一个需要返回NSArray的类方法,但是由于API调用是异步的,因此我需要从异步调用内的方法中返回。问题是异步返回void。如果我在Node上执行此操作,则将使用JS promises,但我找不到能在Swift中使用的解决方案。 import Foundation class Bookshop { class func getGenres() -> NSArray { println("Hello inside getGenres") let urlPath = "http://creative.coventry.ac.uk/~bookshop/v1.1/index.php/genre/list" println(urlPath) let url: NSURL = NSURL(string: urlPath) let session = NSURLSession.sharedSession() var resultsArray:NSArray! let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in println("Task completed") if(error) { println(error.localizedDescription) …
93 ios  rest  asynchronous  swift 

15
Swift中的“使用未解决的标识符”
因此,我一直在开发一个应用程序,并且一切正常。但是今天我像往常一样创建了一个新类,由于某种原因,我不能从其他类中访问Public / Global变量。所有其他班级都可以,但是现在无论何时我尝试开设一个新班级我都做不到。如何解决? 我正在使用Swift和Xcode 6。 工人阶级: import UIKit import Foundation import Parse import CoreData var signedIn = true class ViewController: UIViewController { 新课程: import UIKit class NewClass: UIViewController { override func viewDidLoad() { super.viewDidLoad() signedIn = false } 但是在 signedIn = false 我得到错误: 使用未解决的标识符“ signedIn”
93 ios  xcode  swift 

13
Swift-如何检测方向变化
我想将两个图像添加到单个图像视图中(即用于横向显示一个图像,用于纵向显示另一个图像),但是我不知道如何使用快速语言检测方向变化。 我尝试了这个答案,但只拍了一张图片 override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { if UIDevice.currentDevice().orientation.isLandscape.boolValue { print("Landscape") } else { print("Portrait") } } 我是iOS开发的新手,任何建议将不胜感激!
93 ios  swift 

5
如何在Swift中为UIViewController子类创建自定义初始化程序?
不好意思,如果以前已经问过这个问题,我已经搜索了很多内容,并且很多答案都来自早期Swift版本,当时情况有所不同。我似乎找不到确切的答案。 我想创建一个子类,UIViewController并有一个自定义的初始化程序,使我可以轻松地在代码中进行设置。我在Swift中无法做到这一点。 我想要一个init()可以用来传递特定参数的函数,NSURL然后将其与视图控制器一起使用。在我看来,它看起来像init(withImageURL: NSURL)。如果我添加了该功能,则要求我添加该init(coder: NSCoder)功能。 我相信这是因为它在超类中用required关键字标记了吗?所以我必须在子类中这样做吗?我添加它: required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } 怎么办?我的特殊初始化程序被认为是convenience一个吗?指定一个?我叫超级初始化器吗?来自同一类的初始值设定项? 如何将特殊的初始化程序添加到UIViewController子类上?

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.