字符串未转换为 NSURL [SWIFT]
String is not convert to NSURL [SWIFT]
为什么会出现此错误?我粘贴代码,我的第一次尝试在运行时中断了一个线程。天哪,现在我明白为什么苹果招聘人员问 CS 学生的第一个问题是你知道苹果代码吗,或者 'swift'...(meh)
错误提示 - lastPathComponent 不可用:改用 NSURL 上的 lastPathComponent
// get a list of all the png files in the app's images group
let paths = NSBundle.mainBundle().pathsForResourcesOfType(
"png", inDirectory: nil)
// get image filenames from paths
for path in paths {
if !path.lastPathComponent.hasPrefix("AppIcon") { //error
allCountries.append(path.lastPathComponent) //error
}
}
尝试 1:
for path in paths as [AnyObject] {
if !path.lastPathComponent.hasPrefix("AppIcon") {
allCountries.append(path.lastPathComponent)
}
}
使用URL相关API
if let imageURLs = NSBundle.mainBundle().URLsForResourcesWithExtension("png", subdirectory: nil) {
// get image filenames from paths
for url in imageURLs {
if !url.lastPathComponent!.hasPrefix("AppIcon") {
allCountries.append(url.lastPathComponent!)
}
}
}
或者有点"Swiftier"
if let urls = NSBundle.mainBundle().URLsForResourcesWithExtension("png", subdirectory: nil) {
allCountries = urls.filter {[=11=].lastPathComponent!.hasPrefix("AppIcon") == false} .map {[=11=].lastPathComponent!}
}
为什么会出现此错误?我粘贴代码,我的第一次尝试在运行时中断了一个线程。天哪,现在我明白为什么苹果招聘人员问 CS 学生的第一个问题是你知道苹果代码吗,或者 'swift'...(meh)
错误提示 - lastPathComponent 不可用:改用 NSURL 上的 lastPathComponent
// get a list of all the png files in the app's images group
let paths = NSBundle.mainBundle().pathsForResourcesOfType(
"png", inDirectory: nil)
// get image filenames from paths
for path in paths {
if !path.lastPathComponent.hasPrefix("AppIcon") { //error
allCountries.append(path.lastPathComponent) //error
}
}
尝试 1:
for path in paths as [AnyObject] {
if !path.lastPathComponent.hasPrefix("AppIcon") {
allCountries.append(path.lastPathComponent)
}
}
使用URL相关API
if let imageURLs = NSBundle.mainBundle().URLsForResourcesWithExtension("png", subdirectory: nil) {
// get image filenames from paths
for url in imageURLs {
if !url.lastPathComponent!.hasPrefix("AppIcon") {
allCountries.append(url.lastPathComponent!)
}
}
}
或者有点"Swiftier"
if let urls = NSBundle.mainBundle().URLsForResourcesWithExtension("png", subdirectory: nil) {
allCountries = urls.filter {[=11=].lastPathComponent!.hasPrefix("AppIcon") == false} .map {[=11=].lastPathComponent!}
}