错误:类型'String'的值在Swift开关中没有成员'hasSuffix' [英] Error: value of type 'String' has no member 'hasSuffix' in Swift switch
问题描述
我正在学习下面的switch语句示例引发错误.
The below example of switch statement throws an error.
let vegetable = "red pepper"
switch vegetable {
case "celery":
print("Add some raisins and make ants on a log.")
case "cucumber", "watercress":
print("That would make a good tea sandwich.")
case let x where x.hasSuffix("pepper"):
print("Is it a spicy \(x)?")
default:
print("Everything tastes good in soup.")
}
得到的错误是: error: value of type 'String' has no member 'hasSuffix'
case let x where x.hasSuffix("pepper"):
我正在使用ubuntu 14.04
,而我的Swift版本是swift --version
Swift version 3.0 (swift-3.0-PREVIEW-2)
The error am getting is : error: value of type 'String' has no member 'hasSuffix'
case let x where x.hasSuffix("pepper"):
Am using ubuntu 14.04
and my version of Swift is swift --version
Swift version 3.0 (swift-3.0-PREVIEW-2)
推荐答案
String
的hasSuffix(_:)
成员是从NSString
桥接的(也是Foundation的桥梁).当在Xcode Playgrounds和项目中使用Swift时,此方法可从Swift标准库中获取,而当从例如Windows编译Swift时.在IBM沙箱/本地Linux机器上,无法访问Swift std-lib版本,而从core-libs Foundation可以访问该版本.
The hasSuffix(_:)
member of String
is bridged from NSString
(and in so from Foundation). When working with Swift in Xcode Playgrounds and projects, this method is available from the Swift standard library, whereas when compiling Swift from e.g. the IBM sandbox/your local Linux machine, the Swift std-lib version is not accessible, whereas the one from core-libs Foundation is.
要访问后面的实现,您需要显式导入Foundation:
To have access to the latter implementation, you need to explicitly import Foundation:
import Foundation // <---
let vegetable = "red pepper"
switch vegetable {
case "celery":
print("Add some raisins and make ants on a log.")
case "cucumber", "watercress":
print("That would make a good tea sandwich.")
case let x where x.hasSuffix("pepper"):
print("Is it a spicy \(x)?")
default:
print("Everything tastes good in soup.")
}
大多数Swift教程将假定通过Xcode执行,并且import Foundation
是在Xcode之外编译Swift时对任何"...没有成员..." 错误的良好尝试.
Most Swift tutorials will assume execution via Xcode, and import Foundation
can be good first attempted remedy for any "... has no member ..." errors when compiling Swift outside of Xcode.
正如@Hamish在下面的注释中指出的(谢谢!),标准库中提供了hasSuffix(_:)
的一种实现,但是需要_runtime(_ObjC)
.
As @Hamish points out in a comment below (thanks!), one implementation of hasSuffix(_:)
is available from the standard library, with the requirement of _runtime(_ObjC)
, however.
来自 swift/stdlib/public/core/StringLegacy .swift :
#if _runtime(_ObjC)
// ...
public func hasSuffix(_ suffix: String) -> Bool {
// ...
}
#else
// FIXME: Implement hasPrefix and hasSuffix without objc
// rdar://problem/18878343
#endif
在无法访问上述情况的情况下,可以使用另一种实现方式(例如,从Linux).但是,访问此方法需要隐式的import Foundation
语句.
Another implementation can be used in case the one above is not accessible (e.g. from Linux). Accessing this method requires an implicit import Foundation
statement, however.
来自 swift-corelibs-foundation/Foundation/NSString .swift :
#if !(os(OSX) || os(iOS))
extension String {
// ...
public func hasSuffix(_ suffix: String) -> Bool {
// ... core foundation implementation
}
}
#endif
这篇关于错误:类型'String'的值在Swift开关中没有成员'hasSuffix'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!