Questions tagged «swift»

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

3
如何在Objective-C项目中导入和使用Swift Pod框架
我一直在尝试检出CocoaPods新框架设置以使某些Pod运行起来,而在我的Objective-C项目中使用Swift时遇到了麻烦。 首先,这是CocoaPods预发行版0.35,您可以在此处阅读有关如何使用和安装它的信息。 这是我当前的Podfile: source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' pod 'MBProgressHUD' pod 'SLPagingViewSwift' MBProgressHUD是常见的旋转指示器,SLPagingViewSwift是一个随机项目,我通过在cocoapods搜索中键入Swift来找到。这是ViewController.m在我的项目中: #import "ViewController.h" @import SLPagingViewSwift; @import MBProgressHUD; @interface ViewController () @end @implementation ViewController - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Works just fine MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:hud]; [hud show:YES]; // Causes Error -- Won't build …

4
如何在Swift中将新单元格插入UITableView
我正在一个有两个UITableViews和两个UITextFields的项目中工作,当用户按下按钮时,第一个中的数据textField应进入tableView,第二个中的数据应进入第二个tableView。我的问题是,我不知道tableView每次用户按下按钮时都如何放入数据,我知道如何插入数据,tableView:cellForRowAtIndexPath:但是据我所知,这种方法只能工作一次。那么,tableView每当用户按下按钮时,我可以使用什么方法来更新?

5
如何使用“完成”按钮制作UIPickerView?
我很难使用户点击UITextField时出现带有完成按钮的UIPickerView。到目前为止,这是我的代码。一切正常,但是当我点击文本字段时,出现键盘,而不是选择器。 class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { @IBOutlet var textField1: UITextField! let pickerData = ["11", "12", "13"] @IBAction func textButton(sender: AnyObject) { let picker: UIPickerView picker = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300)) picker.backgroundColor = .whiteColor() picker.showsSelectionIndicator = true picker.delegate = self picker.dataSource = self let toolBar = UIToolbar() toolBar.barStyle = …

7
在带有继承的Swift 4中使用Decodable
使用类继承是否会破坏类的可解码性。例如下面的代码 class Server : Codable { var id : Int? } class Development : Server { var name : String? var userId : Int? } var json = "{\"id\" : 1,\"name\" : \"Large Building Development\"}" let jsonDecoder = JSONDecoder() let item = try jsonDecoder.decode(Development.self, from:json.data(using: .utf8)!) as Development print(item.id ?? …
72 swift  swift4  codable 

14
如何在iOS 13上更改状态栏的背景颜色和文本颜色?
随着iOS 13的到来,statusBar的视图不再能通过低谷访问: value(forKey: "statusBar") as? UIView 由于: 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“在UIApplication上名为-statusBar或-statusBarWindow的应用程序:此代码必须更改,因为不再有状态栏或状态栏窗口。而是在窗口场景上使用statusBarManager对象。” 但是尚不清楚应如何使用它来更改颜色,因为keyWindow?.windowScene?.statusBarManager它似乎不包含与之相关的任何内容。 我正在编译具有(iOS 10,*)兼容性的代码,因此我打算继续使用UIKit。 关于这个主题有什么想法吗?
72 ios  swift  statusbar  ios13 

14
Swift:如何在设备旋转后刷新UICollectionView布局
我使用UICollectionView(flowlayout)构建简单的布局。每个单元格的宽度设置为使用self.view.frame.width 但是当我旋转设备时,单元格不会更新。 我找到了一个函数,该函数在方向改变时被调用: override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { //code } 但我找不到更新UICollectionView布局的方法 主要代码在这里: class ViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{ @IBOutlet weak var myCollection: UICollectionView! var numOfItemsInSecOne: Int! override func viewDidLoad() { super.viewDidLoad() numOfItemsInSecOne = 8 // Do any additional setup after loading the view, typically from …

22
如何在Swift中敬酒消息?
有什么办法可以迅速烤面包? 我已经在目标c中进行了尝试,但没有迅速找到解决方案。 [self.view makeToast:@"Account created Successfully" duration:0.5 position:@"bottom"];
72 ios  swift 

2
如何在Swift 3中记录函数的闭包参数的参数?
在Xcode 8 beta和Swift 3中,当您有一个将闭包作为参数的方法时,例如: func foo(bar: (String) -> Void) { bar("Hello, world") } 您如何记录闭包需要的参数?例如,如果我这样写: /// Calls bar with "Hello, world" /// - parameter bar: A closure to call func foo(bar: (String) -> Void) { bar("Hello, world") } 然后,快速帮助如下所示: 我想知道什么语法可以使我写一些文字来代替“无描述”。非常感谢!
72 swift  xcode  markup 

5
如何使用Swift中的Objective-C #define
我正在迁移UIViewController课程,以便对Swift进行一些培训。我通过桥接头成功地使用了Objective-C代码,但是我需要导入一个包含#define指令的常量文件。 我在结合Swift和Cocoa和Objective-C(简单宏)中看到以下内容: 简单宏 通常#define在C和Objective-C中使用指令定义原始常量的地方,在Swift中使用全局常量代替。例如,常量定义#define FADE_ANIMATION_DURATION 0.35可以在Swift中使用更好地表达let FADE_ANIMATION_DURATION = 0.35。因为简单的类似于常量的宏直接映射到Swift全局变量,所以编译器会自动导入C和Objective-C源文件中定义的简单宏。 因此,似乎有可能。我已经将包含常量的文件导入到桥接头文件中,但是我的.swift文件没有可见性,无法解决。 我应该怎么做才能使我的常量对Swift可见? 更新: 似乎可以使用NSString常量,但不能使用布尔值: #define kSTRING_CONSTANT @"a_string_constant" // resolved from swift #define kBOOL_CONSTANT YES // unresolved from swift

1
Swift 3不正确的字符串插值,带有隐式展开的Optionals
为什么隐式解开了可选在Swift 3中使用字符串插值时,没有解开变量? 示例:在操场上运行以下代码 var str: String! str = "Hello" print("The following should not be printed as an optional: \(str)") 产生以下输出: The following should not be printed as an optional: Optional("Hello") 当然,我可以将字符串与 +运算符将字符串但是我在应用程序中的几乎所有地方都使用了字符串插值,由于这个原因,现在不再起作用了(错误?)。 这甚至是一个错误,还是他们有意使用Swift 3更改了此行为?

7
在Swift中使用where子句扩展数组类型
我想使用Accelerate框架来扩展[Float]和[Double],但是每一个都需要不同的实现。 我尝试了明显的方法: extension Array<Float> { } 并得到这个错误: “必须在具有'where'子句指定约束的非专用泛型'Array'上声明受约束的扩展” 这样在Swift 2中扩展泛型类型是否可行? 我的代码现在按预期工作了。这是显示使用Accelerate框架求和的示例。 extension _ArrayType where Generator.Element == Float { func quickSum() -> Float { var result: Float = 0 if var x = self as? [Float] { vDSP_sve(&x, 1, &result, vDSP_Length(x.count)) } return result } } extension _ArrayType where Generator.Element == …

5
如何关闭UISearchController?(iOS 8及更高版本)
这一定是微不足道的,但是我找不到您应该如何以编程方式关闭UISearchController? 请注意,它是新的UISearchController(于2014年在iOS 8中引入),而不是UISearchDisplayController。 到目前为止,这就是我所拥有的 // Dismiss the search tableview searchController.dismissViewControllerAnimated() // Clear the Search bar text searchController.active = false 但我仍然有“取消”按钮,无法摆脱它。

3
迅速。UILabel文本对齐
我迅速创建了UILabel: let label = UILabel(frame: CGRect( x: 50, y: 50, width: 100, height: 50)) 设置属性似乎很简单: label.textColor = UIColor.redColor() 如何实现像textAlignment这样的枚举类型?在目标C中 label.textAlignment = NSTextAlignmentCenter; 但很快似乎没有用。
71 enums  uilabel  swift 

6
null / nil快速语言
我们如何用快速编程语言定义以下内容: 空值 零 零 [NSNull null] 换句话说,这些目标c项中的每一个的等效项将是什么。此外,还想知道是否存在针对非目标c类型(如结构和枚举)的任何特定用例。提前致谢。
71 ios  swift 

5
我可以通过Swift中的NSURLSession以某种方式执行同步HTTP请求吗
我可以NSURLSession在Swift中通过某种方式执行同步HTTP请求吗? 我可以通过以下代码进行异步请求: if let url = NSURL(string: "https://2ch.hk/b/threads.json") { let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in var jsonError: NSError? let jsonDict = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &jsonError) as [String: AnyObject] if jsonError != nil { return } // ... } task.resume() } 但是同步请求呢?

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.