错误:'String' 类型的值在 Swift 开关中没有成员 'hasSuffix' [英] Error: value of type 'String' has no member 'hasSuffix' in Swift switch

查看:26
本文介绍了错误:'String' 类型的值在 Swift 开关中没有成员 'hasSuffix'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Swift.org

下面的 switch 语句示例抛出错误.

让蔬菜=红辣椒"切换蔬菜{案例芹菜":print("加些葡萄干,在木头上做蚂蚁.")案例黄瓜"、豆瓣":print("那会是一个很好的茶三明治.")case let x where x.hasSuffix("pepper"):print("是辣的(x)吗?")默认:print("汤里什么都好吃.")}

得到的错误是 :错误:'String' 类型的值没有成员'hasSuffix'case let x where x.hasSuffix("pepper"):我正在使用 ubuntu 14.04 而我的 Swift 版本是 swift --versionSwift 3.0 版 (swift-3.0-PREVIEW-2)

解决方案

StringhasSuffix(_:)成员是从NSString(来自基金会).在 Xcode Playgrounds 和项目中使用 Swift 时,此方法可从 Swift 标准库中获得,而在从例如编译 Swift 时.IBM 沙箱/您的本地 Linux 机器,Swift std-lib 版本不可访问,而 core-libs Foundation 的版本是.

要访问后一个实现,您需要显式导入 Foundation:

import Foundation//<---让蔬菜=红辣椒"切换蔬菜{案例芹菜":print("加些葡萄干,在木头上做蚂蚁.")案例黄瓜"、豆瓣":print("那会是一个很好的茶三明治.")case let x where x.hasSuffix("pepper"):print("是辣的(x)吗?")默认:print("汤里什么都好吃.")}

大多数 Swift 教程将假定通过 Xcode 执行,并且 import Foundation 可以作为很好的第一次尝试补救任何 "... has no member ..." 错误时在 Xcode 之外编译 Swift.

<小时>

详情

正如@Hamish 在下面的评论中指出的那样(谢谢!),hasSuffix(_:) 的一种实现可从标准库中获得,需要 _runtime(_ObjC),然而.

来自 swift/stdlib/public/core/StringLegacy.swift:

<块引用>

#if _runtime(_ObjC)//...public func hasSuffix(_ suffix: String) ->布尔 {//...}#别的//FIXME:在没有 objc 的情况下实现 hasPrefix 和 hasSuffix//rdar://problem/18878343#万一

如果上述实现不可访问(例如从 Linux),则可以使用另一种实现.然而,访问这个方法需要一个隐式的 import Foundation 语句.

来自 swift-corelibs-foundation/Foundation/NSString.swift:

<块引用>

#if !(os(OSX) || os(iOS))扩展字符串 {//...public func hasSuffix(_ suffix: String) ->布尔 {//... 核心基础实现}}#万一

I'm following tutorials from Swift.org

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.")
}

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)

解决方案

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.

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.")
}

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.


Details

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.

From 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

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.

From 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆