Swift 3 'encode' 生成 'Void'(又名“()”),而不是预期的上下文结果类型 XYZ?
Swift 3 'encode' produces 'Void' (aka '()'), not the expected contextual result type XYZ?
目前,我正在为我的 'Note' 对象编写 'encode'() ,为它们的序列化做准备。关于如何在 Swift 3.0 中序列化以及此错误消息的教程,我一直在高低寻找,但无济于事。
此代码来自一个不相关的先前项目(从 Swift 2.2 转换为 3.0),工作绝对正常:
//Serialise the object.
func encode(with aCoder: NSCoder)
{
aCoder.encodeCInt(personIDNumber, forKey: "personIDNumber")
aCoder.encode(staffName, forKey: "staffName")
aCoder.encode(userName, forKey: "userName")
aCoder.encode(age, forKey: "age")
}
但是,如果我尝试在当前项目(在 Swift 3.0 中启动)中执行相同的操作:
public func encode(with aCoder: NSCoder)
{
noteText = aCoder.encode(noteText, forKey: "noteText")
}
这是发生的错误:
'encode' produces 'Void' (aka '()'), not the expected contextual result type String? ***Bizzarely, this also causes every 'encode' function that's available with NSCoder-type objects to be struck-through, as though deprecated, which obviously isn't true***.
我的'decode'()如下,编译器满意:
required public init (coder aDecoder: NSCoder)
{
noteText = aDecoder.decodeObject(forKey: "noteText") as! String?
}
所以,我要的是:
• 一组关于如何修复此错误的明确步骤(仅仅是因为我对 Swift 还是新手,并且完全被这条消息难住了,对于那些追随我的人)。
• 解释为什么会发生这种情况,以及为什么(如果可能的话),我的 'decode()' 在我给出的两种情况下都是完全可以接受的,但是 'encode()'
非常感谢并提前感谢任何帮助。
noteText
,一个属性的class encode(with:)
属于(Note
?),是给定的类型,比如说XYZ
.然而,aCoder
(NSCoder
类型)的 encode(, forKey:)
方法是一个 non-returning 函数, with signatures looking like:
func encode(_ realv: Double,
forKey key: String)
请注意缺少此方法的显式 return 类型。它 不是 return 类型 XYZ
的值,如您所期望的那样,并且当您尝试将对 encode(_:forKey:)
的调用结果分配给a 属性 (noteText
),你会得到预期的类型不匹配,因为 noteText
不是 Void
类型(空元组,()
),它是未指定 return 类型的默认 return 函数。
我不知道你想在这里实现什么,但是你不能将 encode(_:forKey:)
调用的 return 分配给任何类型的 属性 (无用的 属性 类型 ()
),因此删除此分配将使您摆脱问题标题的错误消息。
// change this
noteText = aCoder.encode(noteText, forKey: "noteText")
// into
aCoder.encode(noteText, forKey: "noteText")
目前,我正在为我的 'Note' 对象编写 'encode'() ,为它们的序列化做准备。关于如何在 Swift 3.0 中序列化以及此错误消息的教程,我一直在高低寻找,但无济于事。
此代码来自一个不相关的先前项目(从 Swift 2.2 转换为 3.0),工作绝对正常:
//Serialise the object.
func encode(with aCoder: NSCoder)
{
aCoder.encodeCInt(personIDNumber, forKey: "personIDNumber")
aCoder.encode(staffName, forKey: "staffName")
aCoder.encode(userName, forKey: "userName")
aCoder.encode(age, forKey: "age")
}
但是,如果我尝试在当前项目(在 Swift 3.0 中启动)中执行相同的操作:
public func encode(with aCoder: NSCoder)
{
noteText = aCoder.encode(noteText, forKey: "noteText")
}
这是发生的错误:
'encode' produces 'Void' (aka '()'), not the expected contextual result type String? ***Bizzarely, this also causes every 'encode' function that's available with NSCoder-type objects to be struck-through, as though deprecated, which obviously isn't true***.
我的'decode'()如下,编译器满意:
required public init (coder aDecoder: NSCoder)
{
noteText = aDecoder.decodeObject(forKey: "noteText") as! String?
}
所以,我要的是:
• 一组关于如何修复此错误的明确步骤(仅仅是因为我对 Swift 还是新手,并且完全被这条消息难住了,对于那些追随我的人)。
• 解释为什么会发生这种情况,以及为什么(如果可能的话),我的 'decode()' 在我给出的两种情况下都是完全可以接受的,但是 'encode()'
非常感谢并提前感谢任何帮助。
noteText
,一个属性的class encode(with:)
属于(Note
?),是给定的类型,比如说XYZ
.然而,aCoder
(NSCoder
类型)的 encode(, forKey:)
方法是一个 non-returning 函数, with signatures looking like:
func encode(_ realv: Double, forKey key: String)
请注意缺少此方法的显式 return 类型。它 不是 return 类型 XYZ
的值,如您所期望的那样,并且当您尝试将对 encode(_:forKey:)
的调用结果分配给a 属性 (noteText
),你会得到预期的类型不匹配,因为 noteText
不是 Void
类型(空元组,()
),它是未指定 return 类型的默认 return 函数。
我不知道你想在这里实现什么,但是你不能将 encode(_:forKey:)
调用的 return 分配给任何类型的 属性 (无用的 属性 类型 ()
),因此删除此分配将使您摆脱问题标题的错误消息。
// change this
noteText = aCoder.encode(noteText, forKey: "noteText")
// into
aCoder.encode(noteText, forKey: "noteText")