标签: swift4

Swift 4协议组成一致性

[ 首先,如果有人对问题有更好的名称,提案就会被广泛接受.到目前为止,我没有找到更好的名称.]

所以,这就是问题所在.

假设我有一个协议ProtocolA,协议ProtocolB和协议ProtocolX定义如下:

protocol ProtocolA {
  func doSomethingA()
}

protocol ProtocolB {
   func doSomethingB()
}

protocol ProtocolX {
   var handler: ProtocolA { get }
}
Run Code Online (Sandbox Code Playgroud)

然后我在ProtocolX的一个类中有一个正确的实现,如下所示:

class Donald: ProtocolX {
   ...
   var handler: ProtocolA { ... }
   ...
}
Run Code Online (Sandbox Code Playgroud)

然后一切正常,协议要求得到正确履行.

如果我这样实现:

class Donald: ProtocolX {
   ...
   var handler: ProtocolA & ProtocolB
   ...
}
Run Code Online (Sandbox Code Playgroud)

我有一个编译时问题,报告我的类Donald不符合ProtocolX(根据规范,要求var handler必须符合ProtocolA).

从理论上讲,var handlerIS符合ProtocolA(但它也符合ProtocolB …

swift swift4

11
推荐指数
1
解决办法
850
查看次数

KV在Swift 4中保留一个协议

我正在努力在Swift 4中使用新的强类型KVO语法来观察仅通过协议可见的属性:

import Cocoa

@objc protocol Observable: class {
    var bar: Int { get }
}

@objc class Foo: NSObject, Observable {
    @objc dynamic var bar = 42
}

let implementation = Foo()

let observable: Observable = implementation

let observation = observable.observe(\.bar, options: .new) { _, change in
    guard let newValue = change.newValue else { return }

    print(newValue)
}

implementation.bar = 50
Run Code Online (Sandbox Code Playgroud)
error: value of type 'Observable' has no member 'observe'
let observation = observable.observe(\.bar, options: .new) { _, change …
Run Code Online (Sandbox Code Playgroud)

key-value-observing swift-protocols swift4

11
推荐指数
1
解决办法
893
查看次数

Swift URL appendingPathComponent 将 `?` 转换为 `%3F`

let url = URL(string: "https://example.com")
let path = "/somePath?"
let urlWithPath = url?.appendingPathComponent(path)
Run Code Online (Sandbox Code Playgroud)

追加后,路径/somePath?变为somePath%3F

?成为一个%3F。问号替换为百分比编码的转义字符。

如果我使用,URL 会正确输出:

let urlFormString = URL(string:"https://example.com/somePath?")
Run Code Online (Sandbox Code Playgroud)

为什么appendingPathComponent转换?%3F

appendingPathComponent如果路径组件包含问号,我该如何使用?

url ios swift swift4

11
推荐指数
2
解决办法
6990
查看次数

如何在Swift 4中使用Realm?

我正在尝试在新的Xcode 9测试版中运行我当前的项目,但是当我这样做时它会说Module compiled with Swift 3.1 cannot be imported in Swift 4.0.我怎么解决这个问题?我不是在使用cocoapods.

xcode realm swift swift4 xcode9-beta

10
推荐指数
1
解决办法
6600
查看次数

如何在swift 4中正确使用选择器

我已经阅读了很多教程甚至是官方的Apple文档,并且不能理解这段代码有什么问题.

var dueDatePicker = UIDatePicker()

@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    textField.inputView = dueDatePicker
    dueDatePicker.addTarget(self, action: #selector(datePickerValueChanged(_:)), for: UIControlEvents.valueChanged)
}

func datePickerValueChanged(_ sender: UIDatePicker){
    //Do Stuff
}
Run Code Online (Sandbox Code Playgroud)

在运行时,我单击textField并显示UIDatePicker.执行选择器指向的功能.只要我点击UIDatePicker之外的UI对象,应用程序就会崩溃并显示以下错误:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [YourApp.PromiseViewController dueDateChanged:]:无法识别的选择器发送到实例0x100b12ae0'

我不明白的是,最初识别"选择器"或指向所需功能的指针.但是,当我从另一个UI对象触发另一个事件时,抛出此异常.

为什么会这样?

datePickerValueChanged()最初调用时不应触发此异常吗?

uidatepicker uiview ios swift swift4

10
推荐指数
2
解决办法
8424
查看次数

迁移到swift 4后对泛型方法的"模糊使用"

我试图将我的代码从xcode 8.2 swift 3.0.2迁移到xcode 9 swift 4,我有这个代码的问题:

func test<T0, TRet>(_ fn: (T0) -> TRet) -> Void {
    print("foo1")
    print(T0.self)
}

func test<T0, T1, TRet>(_ fn: (T0, T1) -> TRet) -> Void {
    print("foo2")
    print(T0.self)
    print(T1.self)
}

let fn2 : (Int, Int) -> Int = { (x:Int, y:Int)->Int in
    return x+y
}

test(fn2)
Run Code Online (Sandbox Code Playgroud)

xcode 8.0.2,swift 3.0.2结果:

foo2
Int
Int
Run Code Online (Sandbox Code Playgroud)

xcode 9,swift 4结果:

Playground execution failed:

error: MyPlayground.playground:12:1: error: ambiguous use of 'test'
test(fn2)
^

MyPlayground.playground:1:6: note: found this candidate
func test<T0, …
Run Code Online (Sandbox Code Playgroud)

generics swift swift4 xcode9

10
推荐指数
1
解决办法
745
查看次数

Google SignIn不适用于iOS 10

我已将GoogleSinIn API我的项目集成到了Swift 4.0.它正在进行中,iOS 11.0但是当我正在测试时,iOS 10.0它正在打开浏览器或设备Google上的登录页面,Safari并且在成功签名后打开Google搜索页面.

  1. 当我单击GoogleSignIn下面显示的按钮时,它会打开下一个图像中显示的浏览器.

  2. 然后我填写凭据.

  3. 成功登录后,它会重定向到Google页面而不是应用程序页面.

ios google-signin ios10.3 swift4

10
推荐指数
3
解决办法
1718
查看次数

声明作为子类的metatype并符合Swift 4中的协议

这不是In Swift的副本,我如何声明符合一个或多个协议的特定类型的变量?.这个问题是关于一个特定的用例,我需要一个元类型,并且如何做到这一点绝对不明显.

Swift 4允许声明一个变量,它是一个子类并符合多个协议:

var myVariable: MyClass & MyProtocol & MySecondProtocol
Run Code Online (Sandbox Code Playgroud)

我需要这样的一致性但不是实例,而是类型本身.但是对于以下语法:

var classForCell: UICollectionViewCell.Type & AdditionalHeightable.Type
Run Code Online (Sandbox Code Playgroud)

给我这个错误:

非协议,非类型类型'UICollectionViewCell.Type'不能在协议约束类型中使用

如何声明作为子类的元类型并符合Swift 4中的协议?

swift swift4

10
推荐指数
1
解决办法
1181
查看次数

在Swift 4中解码没有键的JSON

我正在使用一个返回这个非常可怕的JSON的API:

[
  "A string",
  [
    "A string",
    "A string",
    "A string",
    "A string",
    …
  ]
]
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用JSONDecoder解码嵌套数组,但它没有单个键,我真的不知道从哪里开始......你有什么想法吗?

非常感谢!

arrays json swift swift4 jsondecoder

10
推荐指数
2
解决办法
3960
查看次数

如何使用本机崩溃报告在Swift 4中实现异常/错误处理?

我想在我的iOS Swift 4代码中实现本机崩溃报告(不使用推荐的第三方库),并在App崩溃或生成任何异常时通过API调用通知我的后端服务器.我发现App Store/iTunes Connect提供了多次崩溃,但它没有提供崩溃信息,例如UIViewController或者由于它崩溃的错误.有谁可以帮我解决这个问题?提前致谢.

crash ios swift swift4

10
推荐指数
1
解决办法
754
查看次数