在Swift中使用CFReadStreamCreateWithFTPURL测试FTP连接 [英] Test FTP connection using CFReadStreamCreateWithFTPURL in Swift
问题描述
func OpenAndTestFTPConn(user:NSString,pass: NSString) - > Bool {
var ftpstring =ftp:// \(user):\(pass)@ \(txtTarget.stringValue)
var ftpNSURL = NSURL(string:ftpstring )
var FTPStream = CFReadStreamCreateWithFTPURL(nil,ftpNSURL)
var status:Bool
var cfstatus:Boolean = CFReadStreamOpen(FTPStream)as Boolean b $ b if cfstatus == 0 {
status = false
}
else {
status = true
}
println(status)
返回状态
}
当我尝试构建它时,行 var cfstatus:Boolean = CFReadStreamOpen( FTPStream)as Boolean
返回错误无法将表达式的类型'Boolean'转换为类型'CFReadStream!'
。如果我删除了表达式两边的类型声明,则返回的错误是无法将表达式的类型'Boolean'转换为键入'$ T3'
。
我在哪里出错?
注意: cstatus
定义和声明基于这篇文章。
CFReadStreamCreateWithFTPURL
方法返回一个非托管< CFReadStream> $ c $ (每个文档),这意味着这个特定的函数还没有Swift兼容的注释,指示是否保留了返回的值。有关更多详细信息,请参阅此内容中的非托管对象部分Swift书。
对我来说,这段代码不会编译超过 CFReadStreamCreateWithFTPURL
因此而致电。我看不到布尔错误;奇怪的是,您在 CFReadStreamCreateWithFTPURL
调用中看到该错误并且没有错误。你在Beta3上吗?
我可以用以下代码编译代码:
let FTPStream = CFReadStreamCreateWithFTPURL(nil,ftpNSURL).takeRetainedValue()
通过声明该函数已经保留了该值(再次,根据它的文档),将一个非托管值添加到兼容ARC的指针中。
顺便说一句,这是一个挑剔的问题,但所有您的 var
的可以是 let
的。
I am using the following code to try to test an FTP connection:
func OpenAndTestFTPConn(user:NSString, pass:NSString) -> Bool {
var ftpstring = "ftp://\(user):\(pass)@\(txtTarget.stringValue)"
var ftpNSURL = NSURL(string: ftpstring)
var FTPStream = CFReadStreamCreateWithFTPURL(nil, ftpNSURL)
var status:Bool
var cfstatus:Boolean = CFReadStreamOpen(FTPStream) as Boolean
if cfstatus == 0 {
status = false
}
else {
status = true
}
println(status)
return status
}
When I try building this, the line var cfstatus:Boolean = CFReadStreamOpen(FTPStream) as Boolean
returns the error Cannot convert the expression's type 'Boolean' to type 'CFReadStream!'
. If I remove the type declarations on both sides of the expression, the error returned is Cannot convert the expression's type 'Boolean' to type '$T3'
.
Where am I going wrong?
Note: the cstatus
definition and statements are based on this post.
The CFReadStreamCreateWithFTPURL
method returns an Unmanaged<CFReadStream>
(per the docs), meaning that this particular function doesn't yet have Swift-compatible annotations indicating whether returned values have been retained or not. For more details, see "Unmanaged Objects" in this section of the Swift book.
For me, this code doesn't compile past the CFReadStreamCreateWithFTPURL
call because of this issue. I don't see the Boolean error; it's strange that you're seeing that error and no error on the CFReadStreamCreateWithFTPURL
call. Are you on Beta3?
I was able to get the code to compile with:
let FTPStream = CFReadStreamCreateWithFTPURL(nil, ftpNSURL).takeRetainedValue()
which converts the unmanaged value to an ARC-compatible pointer by asserting that the function has already retained the value (again, per its docs).
BTW, it's a nitpick, but all of your var
's can be let
's.
这篇关于在Swift中使用CFReadStreamCreateWithFTPURL测试FTP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!