检查 NSURL 是否在 Swift 与 Objective-C 中实例化
Checking if an NSURL is instantiated in Swift vs. Objective-C
我目前正在参加 iOS 开发课程。作为一项任务,我的任务是在一个项目上启用 iCloud。我发现 Tim Roadley's post 有助于解释该过程。问题:我在 Swift.
中构建了我的项目
特别是 "checks" Roadley 先生用来检查是否有 iCloud 实例的功能之一不能很好地转换为 Swift。我正在尝试测试是否有一个名为 iCloud 的 NSURL 实例已实例化;如果是这样,做一件事,如果不做另一件事。
来自 Roadley 先生的教程:
NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];
if (iCloud) {
NSLog(@"iCloud is working");
// a bunch of code
} else {
NSLog(@"iCloud is working");
// a bunch of code
}
所以我尝试将该语句转换为Swift,如下:
let iCloud: NSURL = fileManager.URLForUbiquityContainerIdentifier(nil)!
// TODO: having trouble "swiftifying" this line
if (iCloud) {
println("iCloud is working!")
} else {
println("iCloud is NOT working!")
}
编译器这样说:Type 'NSURL' does not conform to protocol 'BooleanType'
我错过了什么明显的东西?
编辑:
感谢大家如此出色、快速的回复。非常感谢他们,我在这里学到了一些东西。
您可以检查它是否等于 nil,或者如果它存在,您可以使用 if let
解包。 fileManager.URLForUbiquityContainerIdentifier(nil)
returns 一个可选的 NSURL,因此您可以这样处理它:
Swift 3+
if let cloudURL = fileManager.url(forUbiquityContainerIdentifier: nil) {
print("iCloud is working!")
} else {
print("iCloud is NOT working!")
}
旧版本
let iCloud = fileManager.URLForUbiquityContainerIdentifier(nil) // Remove the "!", and this should return an optional
if iCloud != nil {
println("iCloud is working!")
} else {
println("iCloud is NOT working!")
}
或 if let
:
if let iCloud = fileManager.URLForUbiquityContainerIdentifier(nil) {
// exists
} else {
// doesn't exist
}
如果 fileManager.URLForUbiquityContainerIdentifier 不成功,这行代码将使您的应用程序崩溃。
let iCloud: NSURL = fileManager.URLForUbiquityContainerIdentifier(nil)!
在 Objective-C 中,此方法 returns 为 URL 或 nil。 Swift 中没有 nil 对象。因此方法 returns 是可选的 URL。通过使用!您强制解包可选 URL 。与 Objective-C 不同,您不会得到有效的 URL 或 nil,您会得到有效的 URL 或崩溃。
变量 iCloud 因此是一个 NSURL*,并且它保证不为零(因为你会崩溃)。检查 iCloud 是否为 nil 没有意义,因为它不可能为 nil。
如果删除 !那么 iCloud 就变成了可选的 NSURL*。在这种情况下,您可以通过检查
来检查它是否具有值
if (iCloud != nil)
语法
if (iCloud)
是非法的。 if 语句需要一个布尔值,而 Swift 不只是将所有内容都转换为布尔值(这在某些时候是合法的,但后来人们发现如果允许的话,可选的布尔值就是一场噩梦)。
在大多数情况下,最好的工具是 if-let
:
if let iCloud = fileManager.URLForUbiquityContainerIdentifier(nil) {
println("iCloud is working!") // And you can access iCloud here
} else {
println("iCloud is NOT working!") // And you don't need iCloud here
}
当然,如果它们很长,您应该将它们移到它们自己的函数中,并让 "can access" 函数使用完整的 NSURL
而不是可选的。这样您就不会在 if
中嵌套很多代码。但除此之外,if-let
是解决此类问题的常用工具。
我目前正在参加 iOS 开发课程。作为一项任务,我的任务是在一个项目上启用 iCloud。我发现 Tim Roadley's post 有助于解释该过程。问题:我在 Swift.
中构建了我的项目特别是 "checks" Roadley 先生用来检查是否有 iCloud 实例的功能之一不能很好地转换为 Swift。我正在尝试测试是否有一个名为 iCloud 的 NSURL 实例已实例化;如果是这样,做一件事,如果不做另一件事。
来自 Roadley 先生的教程:
NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];
if (iCloud) {
NSLog(@"iCloud is working");
// a bunch of code
} else {
NSLog(@"iCloud is working");
// a bunch of code
}
所以我尝试将该语句转换为Swift,如下:
let iCloud: NSURL = fileManager.URLForUbiquityContainerIdentifier(nil)!
// TODO: having trouble "swiftifying" this line
if (iCloud) {
println("iCloud is working!")
} else {
println("iCloud is NOT working!")
}
编译器这样说:Type 'NSURL' does not conform to protocol 'BooleanType'
我错过了什么明显的东西?
编辑:
感谢大家如此出色、快速的回复。非常感谢他们,我在这里学到了一些东西。
您可以检查它是否等于 nil,或者如果它存在,您可以使用 if let
解包。 fileManager.URLForUbiquityContainerIdentifier(nil)
returns 一个可选的 NSURL,因此您可以这样处理它:
Swift 3+
if let cloudURL = fileManager.url(forUbiquityContainerIdentifier: nil) {
print("iCloud is working!")
} else {
print("iCloud is NOT working!")
}
旧版本
let iCloud = fileManager.URLForUbiquityContainerIdentifier(nil) // Remove the "!", and this should return an optional
if iCloud != nil {
println("iCloud is working!")
} else {
println("iCloud is NOT working!")
}
或 if let
:
if let iCloud = fileManager.URLForUbiquityContainerIdentifier(nil) {
// exists
} else {
// doesn't exist
}
如果 fileManager.URLForUbiquityContainerIdentifier 不成功,这行代码将使您的应用程序崩溃。
let iCloud: NSURL = fileManager.URLForUbiquityContainerIdentifier(nil)!
在 Objective-C 中,此方法 returns 为 URL 或 nil。 Swift 中没有 nil 对象。因此方法 returns 是可选的 URL。通过使用!您强制解包可选 URL 。与 Objective-C 不同,您不会得到有效的 URL 或 nil,您会得到有效的 URL 或崩溃。
变量 iCloud 因此是一个 NSURL*,并且它保证不为零(因为你会崩溃)。检查 iCloud 是否为 nil 没有意义,因为它不可能为 nil。
如果删除 !那么 iCloud 就变成了可选的 NSURL*。在这种情况下,您可以通过检查
来检查它是否具有值if (iCloud != nil)
语法
if (iCloud)
是非法的。 if 语句需要一个布尔值,而 Swift 不只是将所有内容都转换为布尔值(这在某些时候是合法的,但后来人们发现如果允许的话,可选的布尔值就是一场噩梦)。
在大多数情况下,最好的工具是 if-let
:
if let iCloud = fileManager.URLForUbiquityContainerIdentifier(nil) {
println("iCloud is working!") // And you can access iCloud here
} else {
println("iCloud is NOT working!") // And you don't need iCloud here
}
当然,如果它们很长,您应该将它们移到它们自己的函数中,并让 "can access" 函数使用完整的 NSURL
而不是可选的。这样您就不会在 if
中嵌套很多代码。但除此之外,if-let
是解决此类问题的常用工具。