鉴于:
typealias Action = () -> ()
var action: Action = { }
func doStuff(stuff: String, completion: @escaping Action) {
print(stuff)
action = completion
completion()
}
func doStuffAgain() {
print("again")
action()
}
doStuff(stuff: "do stuff") {
print("swift 3!")
}
doStuffAgain()
Run Code Online (Sandbox Code Playgroud)
有没有办法制作类型的completion参数(和action)Action?并保持@escaping?
更改类型会出现以下错误:
error: @escaping attribute only applies to function types
删除@escaping属性,代码编译并运行,但似乎不正确,因为completion闭包正在逃避函数的范围.
我在swift 3.0中写作
我有这个代码,它给我警告结果,呼叫未使用
public override init(){
super.init()
}
public init(annotations: [MKAnnotation]){
super.init()
addAnnotations(annotations: annotations)
}
public func setAnnotations(annotations:[MKAnnotation]){
tree = nil
addAnnotations(annotations: annotations)
}
public func addAnnotations(annotations:[MKAnnotation]){
if tree == nil {
tree = AKQuadTree()
}
lock.lock()
for annotation in annotations {
// The warning occurs at this line
tree!.insertAnnotation(annotation: annotation)
}
lock.unlock()
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试在另一个类中使用此方法,但它仍然给我错误,insertAnnotation的代码在上面
func insertAnnotation(annotation:MKAnnotation) -> Bool {
return insertAnnotation(annotation: annotation, toNode:rootNode!)
}
func insertAnnotation(annotation:MKAnnotation, toNode node:AKQuadTreeNode) -> Bool {
if !AKQuadTreeNode.AKBoundingBoxContainsCoordinate(box: node.boundingBox!, coordinate: annotation.coordinate) {
return false …Run Code Online (Sandbox Code Playgroud) openURL已在Swift3中弃用.任何人都可以提供一些示例,说明openURL:options:completionHandler:在尝试打开网址时替换是如何工作的吗?
我正在看Xcode 7.3笔记,我注意到了这个问题.
++和 - 运算符已被弃用
有人可以解释为什么它被弃用了吗?我是对的,在Xcode的新版本中,你现在要使用而不是++这个x += 1;
例:
for var index = 0; index < 3; index += 1 {
print("index is \(index)")
}
Run Code Online (Sandbox Code Playgroud)
该文章已在了解新的访问说明很有帮助Swift 3.它也给出了不同用途的一些例子fileprivate和private.
我的问题是 - 是不是fileprivate在一个仅在此文件中使用的函数上使用与使用相同private?
我正在尝试获取JSON响应并将结果存储在变量中.我已经在Swift的早期版本中使用了此代码的版本,直到GM版本的Xcode 8发布.我看了一下StackOverflow上的一些类似的帖子:Swift 2解析JSON - 无法下标Swift 3中的'AnyObject'和JSON解析类型的值.
但是,似乎那里传达的想法并不适用于这种情况.
如何在Swift 3中正确解析JSON响应?在Swift 3中读取JSON的方式有什么变化吗?
以下是有问题的代码(可以在游乐场中运行):
import Cocoa
let url = "https://api.forecast.io/forecast/apiKey/37.5673776,122.048951"
if let url = NSURL(string: url) {
if let data = try? Data(contentsOf: url as URL) {
do {
let parsedData = try JSONSerialization.jsonObject(with: data as Data, options: .allowFragments)
//Store response in NSDictionary for easy access
let dict = parsedData as? NSDictionary
let currentConditions = "\(dict!["currently"]!)"
//This produces an error, Type 'Any' has no subscript members …Run Code Online (Sandbox Code Playgroud) 好吧所以我只是将Xcode更新为7.3,现在我收到了这个警告:
'var'参数已弃用,将在Swift 3中删除
当我需要在此函数中使用var时,如何解决这个问题:
public func getQuestionList(var language: String) -> NSArray {
if self.data.count > 0 {
if (language.isEmpty) {
language = "NL"
}
return self.data.objectForKey("questionList" + language) as! NSArray
}
return NSArray()
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试让我的应用程序在Xcode 8.0中运行,并且遇到了错误.我知道这个代码在以前版本的swift中运行良好,但我假设在新版本中更改了代码.这是我正在尝试运行的代码:
let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.shared().registerForRemoteNotifications()
Run Code Online (Sandbox Code Playgroud)
我得到的错误是"参数标签"(forTypes:,categories :)'与任何可用的重载都不匹配"
是否有一个不同的命令,我可以尝试使其工作?
在Objective-C中,自定义通知只是一个简单的NSString,但在WWDC版本的Swift 3中它并不明显.
nsnotifications nsnotification nsnotificationcenter swift swift3
我有一个协议:
enum DataFetchResult {
case success(data: Data)
case failure
}
protocol DataServiceType {
func fetchData(location: String, completion: (DataFetchResult) -> (Void))
func cachedData(location: String) -> Data?
}
Run Code Online (Sandbox Code Playgroud)
通过示例实现:
/// An implementation of DataServiceType protocol returning predefined results using arbitrary queue for asynchronyous mechanisms.
/// Dedicated to be used in various tests (Unit Tests).
class DataMockService: DataServiceType {
var result : DataFetchResult
var async : Bool = true
var queue : DispatchQueue = DispatchQueue.global(qos: .background)
var cachedData : Data? = nil
init(result …Run Code Online (Sandbox Code Playgroud)