标签: swift2

覆盖Swift扩展中的方法

我倾向于只将必需品(存储属性,初始化器)放入我的类定义中,并将其他所有内容移动到自己的extension类中,有点像extension我将与之合并的每个逻辑块// MARK:.

例如,对于UIView子类,我最终会得到与布局相关的东西的扩展,一个用于订阅和处理事件等等.在这些扩展中,我不可避免地必须覆盖一些UIKit方法,例如layoutSubviews.我从未注意到这种方法有任何问题 - 直到今天.

以此类层次结构为例:

public class C: NSObject {
    public func method() { print("C") }
}

public class B: C {
}
extension B {
    override public func method() { print("B") }
}

public class A: B {
}
extension A {
    override public func method() { print("A") }
}

(A() as A).method()
(A() as B).method()
(A() as C).method()
Run Code Online (Sandbox Code Playgroud)

输出是A B C.这对我来说没什么意义.我读到有关静态分派的协议扩展,但这不是协议.这是一个常规类,我希望在运行时动态调度方法调用.显然,呼叫C至少应该动态调度和产生C

如果我删除继承NSObject …

swift swift-extensions swift2

119
推荐指数
4
解决办法
6万
查看次数

斯威夫特:守卫vs如果让

我一直在阅读关于Swift中的Optionals,我已经看过if let用于检查Optional是否包含值的示例,以及它是否存在 - 使用unwrapped值执行某些操作.

但是,我已经看到在Swift 2.0中,关键字guard主要用于.我想知道是否if let已从Swift 2.0中删除或是否仍然可以使用它.

我应该改变我的计划包含if letguard

conventions optional control-flow swift swift2

105
推荐指数
8
解决办法
4万
查看次数

在swift中没有换行符打印

在swift 2.0中,print()自动添加换行符.在迅速1.2,println()print()以前是独立的功能.那么如何打印一些文本而不添加换行符,因为swift不再具有不附加换行符的打印功能.

swift swift2 swift3

100
推荐指数
3
解决办法
6万
查看次数

Swift模态视图控制器具有透明背景

我知道这个主题很受欢迎,但我在编程语言中有点问题,事实是我仍然不明白我把代码放在哪里.好吧,我会告诉整个案子:

在此输入图像描述

我试图使模态Swift与正常情况略有不同:通过单击按钮,ViewController在屏幕上显示(按照模态类型),但具有透明背景.仅显示带标签的蓝色视图.当呈现此ViewController时,它具有透明背景,但是一旦完成转换,它将保持黑色背景.已经停用了opaque选项,并测试了一些选项,但没有这个故障排除.

有人能帮帮我吗?

视频是在案例模拟器中进行的测试(https://www.youtube.com/watch?v=wT8Uwmq9yqY).

我开始使用swift,我仍然很失落如何在Xcode中编程,我读了一个问题的答案,该问题有以下代码来解决这个问题:

self.presentingViewController.providesPresentationContextTransitionStyle = YES;
self.presentingViewController.definesPresentationContext = YES;
modal.modalPresentationStyle = UIModalPresentationOverCurrentContext;
Run Code Online (Sandbox Code Playgroud)

我在哪里放这个代码?

modalviewcontroller ios swift swift2 swift2.1

97
推荐指数
1
解决办法
12万
查看次数

如何在迅速中遏制警告

我有一段代码产生了很多警告(不推荐使用的API)

使用clang*我能做到

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    ...
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)

然而,这在swift中不起作用.

如何在swift中完成?

注意:我不想全局禁用警告,甚至也不想在文件范围内禁用警告,而只是在源代码的特定部分禁用特定警告.

编辑:我看起来我的笔记不够清楚:我不想要条件编译(这是所谓的复制的建议答案).我只想在不使用新API的情况下使警告静音.

pragma swift swift2

90
推荐指数
4
解决办法
2万
查看次数

如何在Swift中打印'catch all'异常的详细信息?

我正在更新我的代码以使用Swift,我想知道如何打印与'catch all'子句匹配的异常的错误详细信息.我稍微修改了这个Swift语言指南页面中的示例来说明我的观点:

do {
    try vend(itemNamed: "Candy Bar")
    // Enjoy delicious snack
} catch VendingMachineError.InvalidSelection {
    print("Invalid Selection.")
} catch VendingMachineError.OutOfStock {
    print("Out of Stock.")
} catch VendingMachineError.InsufficientFunds(let amountRequired) {
    print("Insufficient funds. Please insert an additional $\(amountRequired).")
} catch {
    // HOW DO I PRINT OUT INFORMATION ABOUT THE ERROR HERE?
}
Run Code Online (Sandbox Code Playgroud)

如果我遇到意外的异常,我需要能够记录导致它的原因.

ios swift swift2

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

找不到框架GoogleToolboxForMac

通过"pod update"更新我的Firebase后,我收到如下错误:

ld: warning: directory not found for option '-F/Users/bennysantoso/Library/Developer/Xcode/DerivedData/FCM-atfcxuircoryufazlomgwfgmvaqm/Build/Products/Debug-iphonesimulator/GoogleToolboxForMac'
ld: framework not found GoogleToolboxForMac
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

这是我的Podfile:

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
pod 'Firebase/Core'
pod 'Firebase/Messaging'

target 'BB' do
  # Comment this line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for BB

  target 'BBTests' do
    inherit! :search_paths
    # …
Run Code Online (Sandbox Code Playgroud)

google-toolbox-for-mac ios cocoapods firebase swift2

76
推荐指数
6
解决办法
5万
查看次数

从常规方法调用协议默认实现

我想知道是否有可能实现这样的目标.
我有一个像这样的游乐场:

protocol Foo {
    func testPrint()
}

extension Foo {
    func testPrint() {
        print("Protocol extension call")
    }
}

struct Bar: Foo {
    func testPrint() {
        // Calling self or super go call default implementation
        self.testPrint()
        print("Call from struct")
    }
}


let sth = Bar()
sth.testPrint()
Run Code Online (Sandbox Code Playgroud)

我可以提供一个默认实现,extension但如果Bar需要默认实现中的所有内容以及其他内容,该怎么办?
它在某种程度上类似于调用es中的super.方法class来满足实现每个属性等的要求,但我认为没有可能实现相同的structs.

oop protocols swift swift2

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

协议只能用作通用约束,因为它具有Self或associatedType要求

我有一个协议RequestType,它有关联类型模型,如下所示.

public protocol RequestType: class {

    associatedtype Model
    var path: String { get set }

}

public extension RequestType {

    public func executeRequest(completionHandler: Result<Model, NSError> -> Void) {
        request.response(rootKeyPath: rootKeyPath) { [weak self] (response: Response<Model, NSError>) -> Void in
            completionHandler(response.result)
            guard let weakSelf = self else { return }
            if weakSelf.logging { debugPrint(response) }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试为所有失败的请求排队.

public class RequestEventuallyQueue {

    static let requestEventuallyQueue = RequestEventuallyQueue()
    let queue = [RequestType]()

}
Run Code Online (Sandbox Code Playgroud)

但我得到的错误是let queue = [RequestType](),Protocol RequestType只能用作通用约束,因为它具有Self或associatedType要求.

generics ios swift swift-protocols swift2

73
推荐指数
6
解决办法
3万
查看次数

在Xcode 8中使用Swift 2.2?

可以在Xcode 8中使用Swift 2.2吗?

来自Xcode 8发行说明:

"Xcode 8支持切换工具链,例如来自swift.org的工具链,无需重新启动Xcode.(23135507)"

我一直试图在swift.org网站上找到swift 2.2工具链,但不能.在配置Xcode 8以使用swift 2.2方面取得任何成功

swift swift2 xcode8

72
推荐指数
3
解决办法
5万
查看次数