Swift:读取和写入二进制数据(cookie)到磁盘?

时间:2014-12-30 16:01:49

标签: file swift cookies binaryfiles disk

具体来说,我希望命令行实用程序将NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies中的数据写入文件,然后读取数据,以便稍后调用NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie()

这是我到目前为止的代码。

if (Process.arguments.count >= 2 && Process.arguments[1] == "-deleteAll") {
    for cookie in NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies! {
        NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(cookie as NSHTTPCookie)
    }

} else if (Process.arguments.count >= 3 && Process.arguments[1] == "-restore") {
    println("restore from file: " + Process.arguments[2])

} else if (Process.arguments.count >= 3 && Process.arguments[1] == "-export") {
    println("export to file: " + Process.arguments[2])
}

for cookie in NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies! {
    cookie.writeToFile("/Users/aaron/Documents/test.txt", options: nil, error: nil)
}

给我一​​个错误:

2014-12-30 20:20:46.564 Test[17423:424845] -[NSHTTPCookie writeToFile:options:error:]: unrecognized selector sent to instance 0x100511260
2014-12-30 20:20:46.579 Test[17423:424845] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSHTTPCookie writeToFile:options:error:]: unrecognized selector sent to instance 0x100511260'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff950fc64c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff97e556de objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff950ff6bd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00007fff95046a84 ___forwarding___ + 1028
    4   CoreFoundation                      0x00007fff950465f8 _CF_forwarding_prep_0 + 120
    5   Test                                0x00000001001caa1f top_level_code + 991
    6   Test                                0x00000001001caa9a main + 42
    7   libdyld.dylib                       0x00007fff918fd5c9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我想这是将通用数据写入磁盘的类别。

0 个答案:

没有答案