
要明确我要求Swift原生接口不是因为我避免使用Objective C或Cocoa,而是为了描述Swift到Swift代码的预期行为.
这是Swift文档很安静的东西,我想了解更多,所以我调查了它.有一个协议,它叫做Streamable:
protocol Streamable { func writeto<Target : OutputStream>(inout target: Target)} 的OutputStream:
protocol OutputStream { func write(string: String)} write允许写入对象.
String符合两者,使得写入和从中都很容易:
var target = String()"this is a message".writeto(&target)println(target)// this is a message
写入文件:
var msg = "this will be written to an output file"msg.writetofile("output.txt",atomically: false,enCoding: NSUTF8StringEnCoding,error: nil)// creates 'output.txt' in the same folder as the executable 还有writetoUrl.
我假设这些函数都是基于Cocoa流构建的,它们具有类似的功能:
var os = NSOutputStream(tofileAtPath: "output.txt",append: true)os.scheduleInRunLoop(NSRunLoop.currentRunLoop(),forMode: NSDefaultRunLoopMode)var msg = "a truly remarkable message"var ptr:CConstPointer<UInt8> = msg.nulTerminatedUTF8os.open()os.write(ptr,maxLength: msg.lengthOfBytesUsingEnCoding(NSUTF8StringEnCoding))os.close()总结
以上是内存溢出为你收集整理的io – swift是否有写入字节流的协议?全部内容,希望文章能够帮你解决io – swift是否有写入字节流的协议?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)