Questions tagged «swift»

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

13
如何在不使用情节提要的情况下创建新的Swift项目?
在XCode 6中创建一个新项目不允许禁用Storyboard。您只能选择Swift或Objective-C,而不能使用Core Data。 我尝试删除情节提要,并从项目中删除主情节提要,然后从didFinishLaunching手动设置窗口 在AppDelegate中,我有这个: class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow var testNavigationController: UINavigationController func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { testNavigationController = UINavigationController() var testViewController: UIViewController = UIViewController() self.testNavigationController.pushViewController(testViewController, animated: false) self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window.rootViewController = testNavigationController self.window.backgroundColor = UIColor.whiteColor() self.window.makeKeyAndVisible() return true } …
107 ios  swift  xcode6 

9
如何将度数转换为弧度?
我正在尝试将此Obj-C代码转换为Swift代码,但我不知道该代码应该是什么? #define DEGREES_TO_RADIANS(degrees)((M_PI * degrees)/180) 我用Google搜索并找到了这个 但是我不知道如何在Swift中转换它?
107 ios  objective-c  macos  swift  macros 

6
主线程检查器:在后台线程上调用的UI API:-[UIApplication applicationState]
我正在Xcode 9 beta,iOS 11中使用Google地图。 我收到如下错误输出到日志: 主线程检查器:在后台线程上调用的UI API:-[UIApplication applicationState] PID:4442,TID:837820,线程名称:com.google.Maps.LabelingBehavior,队列名称:com.apple.root.default-qos.overcommit ,QoS:21 为什么会发生这种情况,因为几乎可以肯定我没有在代码中更改主线程中的任何接口元素。 override func viewDidLoad() { let locationManager = CLLocationManager() locationManager.requestAlwaysAuthorization() locationManager.requestWhenInUseAuthorization() if CLLocationManager.locationServicesEnabled() { locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters locationManager.startUpdatingLocation() } viewMap.delegate = self let camera = GMSCameraPosition.camera(withLatitude: 53.7931183329367, longitude: -1.53649874031544, zoom: 17.0) viewMap.animate(to: camera) } func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) …

8
如何以编程方式创建UIImage视图-Swift
我正在尝试以编程方式创建UIImage视图,我有一个新视图,并且尝试这样做 let imageName = "yourImage.png" yourview.backgroundColor = UIColor.colorWithPatternImage(UIImage(named:imageName)) 这行不通,因为我不知道第二行中您的看法。 问题: 如何通过编码而不是在情节提要中使UIImageView出现在屏幕上

9
Swift函数中的默认可选参数
当我设置firstThing为默认nil值时,它将起作用,没有默认值nilI时会出现错误,即调用该函数时缺少参数。 通过输入,Int?我认为它使其具有默认值的可选,nil对吗?如果是这样,为什么没有,它为什么不起作用= nil? func test(firstThing: Int? = nil) { if firstThing != nil { print(firstThing!) } print("done") } test()
107 swift  function  optional 

14
如何使用SwiftUI调整图片大小?
我在Assets.xcassets中拥有很大的形象。如何使用SwiftUI调整图片大小以缩小图片? 我试图设置框架,但不起作用: Image(room.thumbnailImage) .frame(width: 32.0, height: 32.0)
107 ios  swift  swiftui 

10
Swift Alamofire:如何获取HTTP响应状态代码
我想检索HTTP响应状态代码(例如400、401、403、503等),以获取请求失败(也可以成功获取)。在此代码中,我正在使用HTTP Basic执行用户身份验证,并且希望能够向用户发送消息,告知用户用户输入错误密码时身份验证失败。 Alamofire.request(.GET, "https://host.com/a/path").authenticate(user: "user", password: "typo") .responseString { (req, res, data, error) in if error != nil { println("STRING Error:: error:\(error)") println(" req:\(req)") println(" res:\(res)") println(" data:\(data)") return } println("SUCCESS for String") } .responseJSON { (req, res, data, error) in if error != nil { println("JSON Error:: error:\(error)") println(" …
106 swift  alamofire 

1
带有“?”的快速可变装饰 (问号)和“!” (感叹号)
我了解在Swift中所有变量都必须设置一个值,并且通过使用可选参数,我们可以将变量设置为nil初始值。 我不明白的是,用a设置变量的作用!是什么,因为我的印象是这会“取消包装”可选值。我认为通过这样做,您可以保证该变量中有一个要解包的值,这就是为什么在IBActions上使用它的原因。 简而言之,执行以下操作时初始化的变量是什么: var aShape : CAShapeLayer! 以及为什么/何时会这样做?
106 swift  optional 

3
从UIWebView迁移到WKWebView
在我的应用程序中,我从UIWebView迁移到WKWebView,如何为WKWebView重写这些功能? func webViewDidStartLoad(webView: UIWebView){} func webViewDidFinishLoad(webView: UIWebView){} 和 func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { print("webview asking for permission to start loading") if navigationType == .LinkActivated && !(request.URL?.absoluteString.hasPrefix("http://www.myWebSite.com/exemlpe"))!{ UIApplication.sharedApplication().openURL(request.URL!) print(request.URL?.absoluteString) return false } print(request.URL?.absoluteString) lastUrl = (request.URL?.absoluteString)! return true } func webView(webView: UIWebView, didFailLoadWithError error: NSError?) …

5
Xcode 11-在iOS 13上退出UISceneDelegate / SwiftUI
我当前正在使用Xcode 11 Beta5。在我的应用程序中,它可以在iOS 12及更低版本上正常运行。但是,在iOS 13上,默认情况下看起来像在使用UIScene。这导致我的应用无法执行任何操作。 当应用程序以全新安装方式启动时,用户必须接受条款和条件。同意后,他们进入加载屏幕,然后将其定向到主视图。在我发布的屏幕截图中,当前当前前景背后的视图是启动加载屏幕。 我们将很快考虑在整个应用程序中添加多视图场景支持,但是现在我们需要处理更高优先级的内容。 https://imgur.com/i0qLhAL 该应用程序目标的“常规”设置中的“支持多个窗口”已被禁用。另外,我在info.plist文件中将“启用多个Windows”设置为“否”。 到目前为止,什么都没有真正起作用。基本上我想选择退出/禁用多个窗口和UIScene / SwiftUI以恢复iOS 10-12中的原始行为。这可能在iOS 13中还是我们必须对其进行更新? 更新: 这是视图调试层次结构的屏幕截图。左侧是iOS 12,右侧是iOS13。没有在Info.plist或场景代理类或方法中添加任何内容,为什么会有所不同?几乎只是在Xcode 11上的现有生产就绪代码中运行了它。 https://imgur.com/C3aLsDo
106 swift  xcode  swiftui  ios13 

7
Swift中的反向范围
有没有办法在Swift中使用反向范围? 例如: for i in 5...1 { // do something } 是一个无限循环。 在较新版本的Swift中,该代码可以编译,但在运行时会出现错误: 致命错误:无法使用upperBound <lowerBound形成Range 我知道我可以1..5代替使用计算j = 6 - i和j用作索引。我只是想知道是否还有更清晰的地方?
105 swift 


7
如何快速在类中定义静态常量
我在我的功能中有这些定义 class MyClass { func myFunc() { let testStr = "test" let testStrLen = countElements(testStr) } } 但是,如果我将“ testStr”和“ testStrLen”移到类级别,它将无法编译。它说'MyClass.Type没有名为'testStr'的成员。 class MyClass { let testStr = "test" let testStrLen = countElements(testStr) func myFunc() { } } 我怎样才能解决这个问题?我不想每次都为一个恒定的“测试”计数len付出代价。 基于对以下评论的理解,我需要这样做: class MyClass { let testStr = "test" let testStrLen = countElements("test") func …
105 swift 


4
“ #selector”是指未公开给Objective-C的方法
新的Xcode 7.3通过addTarget传递参数通常对我有用,但是在这种情况下,它会在标题中引发错误。有任何想法吗?当我尝试将其更改为@objc时,它将引发另一个 谢谢! cell.commentButton.addTarget(self, action: #selector(FeedViewController.didTapCommentButton(_:)), forControlEvents: UIControlEvents.TouchUpInside) 它正在调用的选择器 func didTapCommentButton(post: Post) { }
105 xcode  swift 

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.