使用Mantle在Swift中反序列化和序列化嵌套模型时遇到了很多麻烦。我相信我已经正确设置了所有内容,但我甚至无法解决编译错误。为了给出一些观点,我成功地转换了没有嵌套模型对象的类。这是我的班级:
class TheClass : MTLModel, MTLJSONSerializing
{
var person:Person?
static func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject]!
{
return ["person" : "person"]
}
static func personJSONTransformer() -> NSValueTransformer!
{
return MTLValueTransformer.reversibleTransformerWithForwardBlock(
{ (person:[NSObject : AnyObject]!) -> AnyObject! in
do
{
return MTLJSONAdapter.modelOfClass(Person.self, fromJSONDictionary: person)
}
catch
{
return Person()
}
},
reverseBlock:
{ (person:Person) -> AnyObject! in
return MTLJSONAdapter.JSONDictionaryFromModel(person)
})
}
}
这段代码无法编译,我无法编译。这是我收到的错误消息:
Cannot convert return expression of type 'AnyObject!' to expected return type 'Person'
我已尝试将反向块的返回类型更改为Person
和Person!
,但我收到相同的错误消息。我一直在努力解决这个问题很长一段时间,并且在Swift中找不到一个有效的例子,所以任何帮助都会非常感激。
答案 0 :(得分:4)
我发现了怎么做!实际上我只需要对我的模型进行反序列化以从JSON中读取它们。我是这样做的:
WARN 2015-07-31 07:22:12,994 [1584] - Association with remote system akka.tcp://SystemName@Server:8081 has failed; address is now gated for 5000 ms. Reason is: [Disassociated]
ERROR 2015-07-31 07:22:12,994 [1584] - Disassociated
Akka.Remote.EndpointDisassociatedException: Disassociated
at Akka.Remote.EndpointWriter.PublishAndThrow(Exception reason, LogLevel level)
at Akka.Remote.EndpointWriter.Unhandled(Object message)
at Akka.Remote.EndpointWriter.Writing(Object message)
at Akka.Actor.ActorCell.<>c__DisplayClass3e.<Akka.Actor.IUntypedActorContext.Become>b__3d(Object m)
at Akka.Actor.ActorBase.AroundReceive(Receive receive, Object message)
at Akka.Actor.ActorCell.ReceiveMessage(Object message)
at Akka.Actor.ActorCell.AutoReceiveMessage(Envelope envelope)
at Akka.Actor.ActorCell.Invoke(Envelope envelope)
DEBUG 2015-07-31 07:22:12,996 [1494] - Disassociated [akka.tcp://SystemName@Client:57284] -> akka.tcp://SystemName@Server:8081
DEBUG 2015-07-31 07:23:13,033 [1469] - Drained buffer with maxWriteCount: 50, fullBackoffCount: 1,smallBackoffCount: 0, noBackoffCount: 0,adaptiveBackoff: 10000
ERROR 2015-07-31 07:24:13,019 [1601] - No response from remote. Handshake timed out or transport failure detector triggered.
DEBUG 2015-07-31 07:24:13,020 [1569] - Disassociated [akka.tcp://SystemName@Client:57284] -> akka.tcp://SystemName@Server:8081
WARN 2015-07-31 07:24:13,020 [1601] - Association with remote system akka.tcp://SystemName@Server:8081 has failed; address is now gated for 5000 ms. Reason is: [Disassociated]
ERROR 2015-07-31 07:24:13,021 [1601] - Disassociated
如果您有一组嵌套模型:
class TheClass : MTLModel, MTLJSONSerializing
{
var person:Person?
static func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject]!
{
return ["person" : "person"]
}
static func personJSONTransformer() -> NSValueTransformer!
{
return MTLJSONAdapter.dictionaryTransformerWithModelClass(Person.self)
}
}
这就是它的全部!