Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_to,会覆盖原来的对象 #14

Closed
biancheng347 opened this issue Aug 23, 2019 · 7 comments
Closed

write_to,会覆盖原来的对象 #14

biancheng347 opened this issue Aug 23, 2019 · 7 comments

Comments

@biancheng347
Copy link

write_to,会覆盖原来的对象

@CoderMJLee
Copy link
Collaborator

??你是说覆盖原来的文件内容??

@biancheng347
Copy link
Author

biancheng347 commented Aug 23, 2019 via email

@biancheng347
Copy link
Author

let file: string = "......./test.data"
let p1 = person()
p1.name = "person1"
wirte(p1,to: file)

let p2 = person()
p2.name = "person2"
wirte(p2,to: file)

应该是[p1,p2],但是只有p2

@coderflower
Copy link

这是合理的

@CoderMJLee
Copy link
Collaborator

  • 是的哦,我们设计的就是完全覆盖,并非提供追加内容的接口
  • 你的需求可以这样实现
let p1 = Person()
var ps = [p1]
write(ps, to: file)

let p2 = Person()
ps.append(p2)
write(ps, to: file)
  • 或者
let p1 = Person()
write([p1], to: file)

let p2 = Person()
var ps = read([Person].self, from: file)
ps.append(p2)
write(ps, to: file)

@biancheng347
Copy link
Author

我建议可以提供一个接口 write(addOrUpdate: p2,to: file) ,内部代码是

var ps = read([Person].self, from: file)
ps.append(p2)
write(ps, to: file)

上层代码不管这些细节

@CoderMJLee
Copy link
Collaborator

谢谢建议,下个版本考虑下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants