Guest User

Untitled

a guest
Jun 25th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. //test func
  2. func encodeTest<T>(object: T) where T: Encodable {
  3. // The object should be AnyObject which conforms to Encodable.
  4. let encoder = JSONEncoder()
  5. if let _ = try? encoder.encode(object) {
  6. print("encode success.")
  7. // I need fix the json here.
  8. }
  9. }
  10.  
  11. class Test: Encodable {
  12. var a = "a"
  13. var b = "b"
  14. }
  15.  
  16. //test1
  17. var t: Test?
  18. t = Test()
  19. // it is ok.
  20. encodeTest(object: t)
  21.  
  22.  
  23. //test2
  24. if let tf = t as Encodable? {
  25. // Cannot invoke 'encodeTest' with an argument list of type '(object: Encodable)'
  26. encodeTest(object: tf)
  27. }
Add Comment
Please, Sign In to add comment