2017-09-24 288 views
0

我使用HandyJSOn框架对Swift3中的对象进行序列化和反序列化。现在我有这个问题,我想排除这个过程中的一些属性。我试图按照GithHub页面上给出的步骤,但我不能让工作:与错误排除HandyJSON中的属性

class MyClass : HandyJSON { 
    private var excludeThisProperty : String 

    public func mapping(mapper: HelpingMapper) { 
    mapper >>> self.excludeThisProperty 
    } 
} 

编译器fials:

binary operator >>> cannot be applied to operands of type HelpingMapper and String 

+++例+++

class MyClass : HandyJSON { 
    private let myPropertyDefault : String? = "example" 
    private var myProperty : String 

    public required init() { 
     myProperty = myPropertyDefault! 
    } 

    public func reset() { 
     myProperty = myPropertyDefault! 
    } 

    public func mapping(mapper: HelpingMapper) { 
     mapper >>> self.myPropertyDefault 
    } 
} 

回答

0

请您的字符串改为可选:

private var excludeThisProperty : String? 

示例代码:

let jsonString = "{\"excludeThisProperty\":\"sdfsdf\"}" 

if let myclass = MyClass.deserialize(from: jsonString) { 
    print(myclass) 
} 

class MyClass : HandyJSON { 
    private var myPropertyDefault : String? = "example" // changed from let to var 
    private var myProperty : String 

    public required init() { 
     myProperty = myPropertyDefault! 
    } 

    public func reset() { 
     myProperty = myPropertyDefault! 
    } 

    public func mapping(mapper: HelpingMapper) { 
     mapper >>> self.myPropertyDefault 
    } 
} 
+0

对不起,我太鲁莽了:不幸的是,问题依然存在。现在错误是:“二元运算符>>>不能应用于HelpingMapper和String类型的操作数?” – altralaser

+0

您可以添加更多代码来调试问题吗? –

+0

我发布了一个新的例子。有了这个,你应该能够重现这个问题。 – altralaser