小编Dáv*_*tor的帖子

什么`_?`在Swift中意味着什么?

几天前,我将旧的Xcode 8项目转换为Xcode 9中的Swift 4.我注意到在代码上方生成了额外的Swift代码以及解释.

这是它的样子:

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图理解代码的作用,并在代码中找到我认为有点不寻常_?的东西.

我猜它是未使用的可选项,因为这_意味着我们不会使用特定的变量,因此我们不关心变量的名称,?而是可选的语法.

谢谢您的帮助!

swift

3
推荐指数
1
解决办法
502
查看次数

RxSwift - Playground 执行失败:无法查找符号

我创建了一个新项目并使用 pods 来安装RxSwift. 然后我创建了一个游乐场并编写了以下代码:

import UIKit
import RxSwift
import PlaygroundSupport

var name = "hello"
let names = Variable(["Good"])  
Run Code Online (Sandbox Code Playgroud)

但控制台显示此错误:

Playground 执行失败:

错误:无法查找符号:__
T07RxSwift8VariableCACyxGxcfC __T07RxSwift8VariableCMa

ios cocoapods swift swift-playground rx-swift

3
推荐指数
1
解决办法
852
查看次数

Unsupported swift version (SWIFT 5)

before you mark this question as a duplicate, I checked this question

and it didn't work for me.

how to fix this error:

error: SWIFT_VERSION '5.0' is unsupported, supported versions are: 3.0, 4.0, 4.2. (in target 'DropDown')
Run Code Online (Sandbox Code Playgroud)

xcode swift

3
推荐指数
2
解决办法
4975
查看次数

Swift中Pointwise Equal,Pointwise小于和Pointwise大的功能是什么?

在阅读Apple的Swift编程语言书时,我遇到了Pointwise等于,Pointwise小于和Pointwise大于运算符。参考:https : //developer.apple.com/documentation/swift/swift_standard_library/operator_declarations

.== Pointwise equal

.!= Pointwise not equal
Run Code Online (Sandbox Code Playgroud)

我找不到有关何时使用它们的任何解释和示例。这些操作员的功能是什么?

swift

3
推荐指数
1
解决办法
156
查看次数

如何在Swift中从hashValue实现hash(into :)?

我对如何处理编译器不使用hashValue而不是实施的弃用警告一无所知hash(into:)

“ Hashable.hashValue”已作为协议要求弃用;通过实现'hash(into :)'来使类型'MenuItem'与'Hashable'一致

Swift的回答:'Hashable.hashValue'已被弃用为协议要求;有这个例子:

func hash(into hasher: inout Hasher) {
    switch self {
    case .mention: hasher.combine(-1)
    case .hashtag: hasher.combine(-2)
    case .url: hasher.combine(-3)
    case .custom(let regex): hasher.combine(regex) // assuming regex is a string, that already conforms to hashable
    }
}
Run Code Online (Sandbox Code Playgroud)

而且我有这个结构,可以自定义PagingItem羊皮纸(https://github.com/rechsteiner/Parchment)。

import Foundation

/// The PagingItem for Menus.
struct MenuItem: PagingItem, Hashable, Comparable {
    let index: Int
    let title: String
    let menus: Menus

    var hashValue: Int {
        return index.hashValue …
Run Code Online (Sandbox Code Playgroud)

hashable swift

3
推荐指数
2
解决办法
1700
查看次数

在 Swift 中从二进制文件中读取整数

我有一个代表旧打孔卡的二进制文件。该文件有以下数据:

Function(unsigned int8 min: 0, max: +255), 
Vertical Movement (signed int16 min: -32.768,  max: +32.767) 
Horizontal Movement (signed int16 min: -32.768,  max: +32.767)
Run Code Online (Sandbox Code Playgroud)

此模式将使用不同的值重复大约。100.000 次,将代表具有机器功能的 2D CAD 设计。

文件/打孔卡的每一行都有 5 个字节(1 x Uint8、2 x int16)。最好的阅读方式是什么?在 C# 中,我使用流来读取一个又一个字节,但我找不到在 Swift 5 中执行此操作的示例。

arrays byte file swift

3
推荐指数
1
解决办法
4004
查看次数

为什么 CGFloat.zero 存在?

苹果定义的 有什么特别的原因CGFloat.zero吗?我们应该什么时候使用它?和它有什么不同0

core-graphics swift

3
推荐指数
1
解决办法
179
查看次数

如何在协议中声明通用协议属性要求

挣扎了一段时间,如果你能阐明这一点,那将会非常有帮助:

我有一个APIWorkerProtocol有属性要求的属性,所需的属性是一个协议,即DataParserProtocol

protocol APIWorkerProtocol {
    var apiRequestProvider : APIRequestGeneratorProtocol {get}
    var dataParser : DataParserProtocol{get}
    func callAPI(completionHandler: @escaping (APICallResult<Self.ResultType>) -> Void)
}

protocol DataParserProtocol {
    associatedtype ExpectedRawDataType
    associatedtype ResultType
    func parseFetchedData(fetchedData : ExpectedRawDataType) -> APICallResult<ResultType>
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现这个目标?

在当前的实现中,这会导致错误Protocol 'DataParserProtocol' can only be used as a generic constraint because it has Self or associated type requirements

提前致谢

安基特

generics protocols ios swift

2
推荐指数
1
解决办法
2613
查看次数

在 Swift 4 中解码 JSON

我正在研究苹果应用程序开发指南,这是我现在正在使用的代码......

struct CategoryInfo: Codable {
    var category: String
    var description: String
    var logo: String
    var mobileCategoryName: String

    enum Keys: String, CodingKey {
        case category
        case description = "descr"
        case logo
        case mobileCategoryName = "mobileCatName"
    }

    init(from decoder: Decoder) throws {
        let valueContainer = try decoder.container(keyedBy: Keys.self)
        self.category = try valueContainer.decode(String.self, forKey: Keys.category)
        self.description = try valueContainer.decode(String.self, forKey: Keys.description)
        self.logo = try valueContainer.decode(String.self, forKey: Keys.logo)
        self.mobileCategoryName = try valueContainer.decode(String.self, forKey: Keys.mobileCategoryName)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    let categories = …
Run Code Online (Sandbox Code Playgroud)

json swift codable

2
推荐指数
1
解决办法
714
查看次数

SwiftUI - 可选计时器,重置和重新创建

通常,我会使用一个可选变量来保存我的Timer引用,因为能够在重新创建之前使其无效并将其设置为很好nil

我正在尝试使用SwiftUI并希望确保我这样做是正确的......

我声明为:

@State var timer:Publishers.Autoconnect<Timer.TimerPublisher>? = nil
Run Code Online (Sandbox Code Playgroud)

后来我:

self.timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
Run Code Online (Sandbox Code Playgroud)

要驱动 UI 文本控件,我使用:

.onReceive(timer) { time in
    print("The time is now \(time)")
}
Run Code Online (Sandbox Code Playgroud)

使用此Combine类型Timer无效并重新创建的正确方法是什么?

我读过一个应该调用:

self.timer.upstream.connect().cancel()

但是,我是否还需要无效或简单地nil退出?

nstimer ios swift swiftui combine

2
推荐指数
1
解决办法
767
查看次数