Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中附加两个关键路径_Swift - Fatal编程技术网

在Swift中附加两个关键路径

在Swift中附加两个关键路径,swift,Swift,我试图在Swift(4)中附加两个键路径,但编译器要么不允许,要么结果是“nil”,尽管是有效的键路径 课程: import Foundation @objcMembers class Foo: NSObject { @objc public dynamic var items = [1, 2, 3] } @objcMembers class Bar: NSObject { @objc public dynamic var source: NSObject? } 例1: l

我试图在Swift(4)中附加两个键路径,但编译器要么不允许,要么结果是“nil”,尽管是有效的键路径

课程:

import Foundation

@objcMembers
class Foo: NSObject
{
    @objc public dynamic var items = [1, 2, 3]
}

@objcMembers
class Bar: NSObject
{
    @objc public dynamic var source: NSObject?
}
例1:

let keyPath1 = \Bar.source
let keyPath2 = \Foo.items
keyPath1.appending(path: keyPath2) // error: cannot convert value of type 'ReferenceWritableKeyPath<Foo, [Int]>' to expected argument type 'WritableKeyPath<_, _>' keyPath1.appending(path: keyPath2)
let keyPath1=\Bar.source
让keyPath2=\Foo.items
keyPath1.appending(路径:keyPath2)//错误:无法将“ReferenceWritableKeyPath”类型的值转换为预期的参数类型“WritableKeyPath”keyPath1.appending(路径:keyPath2)
例2:

let keyPath1: WritableKeyPath<Bar, NSObject?> = \Bar.source
let keyPath2: ReferenceWritableKeyPath<Foo, [Int]> = \Foo.items
keyPath1.appending(path: keyPath2) // error: ambiguous reference to member 'appending(path:)' 
let keyPath1:writeablekeypath=\Bar.source
让keyPath2:ReferenceWritableKeyPath=\Foo.items
keyPath1.appending(路径:keyPath2)//错误:对成员“appending(路径:)”的引用不明确
例3:

let keyPath1: AnyKeyPath = \Bar.source
let keyPath2: ReferenceWritableKeyPath<Foo, [Int]> = \Foo.items
keyPath1.appending(path: keyPath2) // returns nil
let keyPath1:AnyKeyPath=\Bar.source
让keyPath2:ReferenceWritableKeyPath=\Foo.items
keyPath1.appending(路径:keyPath2)//返回nil
这里的类故意使用类型为
NSObject
的可选成员。生成的键路径应该是
source.items
,它在
Foo
的实例上是有效的键路径


新的Swift 4键路径类型的正确组合是什么?

首先要使用Swift 4键路径,对象不需要继承自
NSObject
(甚至不是类)并具有符合KVC的属性

其次,
NSObject
类似于
AnyObject
,但Swift 4键路径需要具体的静态类型

最后,属性
source
是可选的,因此必须展开密钥路径才能生效


为了能够连接密钥路径,第一个密钥路径的最后一个组件必须与第二个密钥路径的第一个组件具有相同的类型

这是中的.appending规则

您可以通过这种方式附加关键路径

class Foo {
    var items = [1, 2, 3]
}


class Bar {
    var source: Foo?
}

let bar = Bar()
bar.source = Foo()

let keyPath1 = \Bar.source!
let keyPath2 = \Foo.items
let keyPath = keyPath1.appending(path: keyPath2)

let items = bar[keyPath:keyPath]
print(items)

首先,要使用Swift 4键路径,对象不需要继承自
NSObject
(甚至不是类)并具有符合KVC的属性

其次,
NSObject
类似于
AnyObject
,但Swift 4键路径需要具体的静态类型

最后,属性
source
是可选的,因此必须展开密钥路径才能生效


为了能够连接密钥路径,第一个密钥路径的最后一个组件必须与第二个密钥路径的第一个组件具有相同的类型

这是中的.appending规则

您可以通过这种方式附加关键路径

class Foo {
    var items = [1, 2, 3]
}


class Bar {
    var source: Foo?
}

let bar = Bar()
bar.source = Foo()

let keyPath1 = \Bar.source!
let keyPath2 = \Foo.items
let keyPath = keyPath1.appending(path: keyPath2)

let items = bar[keyPath:keyPath]
print(items)

首先,你必须使用正确的类型
source
应声明为
Foo
,而不是
NSObject
。如果使用此选项,您的应用程序将崩溃,
source
将不会成为
Foo
实例。首先,您必须使用正确的类型
source
应声明为
Foo
,而不是
NSObject
。如果你使用这个,你的应用程序就会崩溃,而且
source
不会是
Foo
实例。嗯,糟糕透了。如果它与隐式泛型一起工作,那也没关系,但是。看来我现在得用老式的Objective-C键路径了。嗯,糟糕透了。如果它与隐式泛型一起工作,那也没关系,但是。看来我现在不得不使用老式的Objective-C键路径。