hasSuffix(_:) and .hasPrefix(_:) String methods missing on Linux

Hello to everyone!
I'm a very newbie to Swift, I'm doing first experiments on Linux & Mac and
I have the following issue: if I call the hasSuffix(_:slight_smile: or the
hasPrefix(_:slight_smile: methods on a String instance, I obtain the following error:

$ swiftc prova.swift
prova.swift:110:4: error: value of type 'String' has no member 'hasSuffix'
if pippo.hasSuffix("ao") {
   ^~~~~ ~~~~~~~~~

Same code works well on OS X.
I'm using Ubuntu 14.04 with required dependencies (clang and libicu-dev)
installed.
Any idea?

Cheers, Alessandro

···

--
Ing. Alessandro Morgantini - http://www.morgantini.org/
Email: gpz500@technologist.com
Cell.: +39-347 761 1221 - Fax: +39-390 347 761 1221

I'm a very newbie to Swift, I'm doing first experiments on Linux & Mac and I have the following issue: if I call the hasSuffix(_:slight_smile: or the hasPrefix(_:slight_smile: methods on a String instance, I obtain the following error:

$ swiftc prova.swift
prova.swift:110:4: error: value of type 'String' has no member 'hasSuffix'
if pippo.hasSuffix("ao") {
   ^~~~~ ~~~~~~~~~

Same code works well on OS X.
I'm using Ubuntu 14.04 with required dependencies (clang and libicu-dev) installed.

Looking in the standard library, the gibberish in the implementation of hasSuffix:

  public func hasSuffix(suffix: String) -> Bool {
    return _stdlib_NSStringHasSuffixNFD(
      self._bridgeToObjectiveCImpl(), suffix._bridgeToObjectiveCImpl())
  }

Makes me think this method is actually handled by NSString. Until Foundation is up and running and toll-free bridging works on Linux, I’m afraid you’re out of luck here.

···

--
Brent Royal-Gordon
Architechies

They may come as extensions from NSString, which Swift doesn’t have fully implemented on Linux yet.

Alex

···

On 11 Dec 2015, at 11:18, Alessandro Morgantini via swift-users <swift-users@swift.org> wrote:

Hello to everyone!
I'm a very newbie to Swift, I'm doing first experiments on Linux & Mac and I have the following issue: if I call the hasSuffix(_:slight_smile: or the hasPrefix(_:slight_smile: methods on a String instance, I obtain the following error:

$ swiftc prova.swift
prova.swift:110:4: error: value of type 'String' has no member 'hasSuffix'
if pippo.hasSuffix("ao") {
   ^~~~~ ~~~~~~~~~

Same code works well on OS X.
I'm using Ubuntu 14.04 with required dependencies (clang and libicu-dev) installed.
Any idea?

Cheers, Alessandro

--
Ing. Alessandro Morgantini - http://www.morgantini.org/
Email: gpz500@technologist.com <mailto:gpz500@technologist.com>
Cell.: +39-347 761 1221 - Fax: +39-390 347 761 1221
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

FYI:

https://github.com/apple/swift/blob/master/stdlib/public/core/StringLegacy.swift#L73

These two functions are (currently) only available with the Objective-C bridge, which the Linux port doesn’t have.

Alex

···

On 11 Dec 2015, at 11:48, Alex Blewitt <alex.blewitt@gmail.com> wrote:

They may come as extensions from NSString, which Swift doesn’t have fully implemented on Linux yet.

Alex

On 11 Dec 2015, at 11:18, Alessandro Morgantini via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello to everyone!
I'm a very newbie to Swift, I'm doing first experiments on Linux & Mac and I have the following issue: if I call the hasSuffix(_:slight_smile: or the hasPrefix(_:slight_smile: methods on a String instance, I obtain the following error:

$ swiftc prova.swift
prova.swift:110:4: error: value of type 'String' has no member 'hasSuffix'
if pippo.hasSuffix("ao") {
   ^~~~~ ~~~~~~~~~

Same code works well on OS X.
I'm using Ubuntu 14.04 with required dependencies (clang and libicu-dev) installed.
Any idea?

Cheers, Alessandro

--
Ing. Alessandro Morgantini - http://www.morgantini.org/
Email: gpz500@technologist.com <mailto:gpz500@technologist.com>
Cell.: +39-347 761 1221 - Fax: +39-390 347 761 1221
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

Clear.
For my needs, this chunk of code at the beginning of my file is sufficient:

#if !_runtime(_ObjC)
extension String {
    public func hasPrefix(prefix: String) -> Bool {
        return prefix ==
String(self.characters.prefix(prefix.characters.count))
    }
    public func hasSuffix(suffix: String) -> Bool {
        return suffix ==
String(self.characters.suffix(suffix.characters.count))
    }
}
#endif

Thank you guys!

···

On Fri, Dec 11, 2015 at 12:51 PM, Alex Blewitt <alex.blewitt@gmail.com> wrote:

FYI:

https://github.com/apple/swift/blob/master/stdlib/public/core/StringLegacy.swift#L73

These two functions are (currently) only available with the Objective-C
bridge, which the Linux port doesn’t have.

Alex

On 11 Dec 2015, at 11:48, Alex Blewitt <alex.blewitt@gmail.com> wrote:

They may come as extensions from NSString, which Swift doesn’t have fully
implemented on Linux yet.

Alex

On 11 Dec 2015, at 11:18, Alessandro Morgantini via swift-users < > swift-users@swift.org> wrote:

Hello to everyone!
I'm a very newbie to Swift, I'm doing first experiments on Linux & Mac and
I have the following issue: if I call the hasSuffix(_:slight_smile: or the
hasPrefix(_:slight_smile: methods on a String instance, I obtain the following error:

$ swiftc prova.swift
prova.swift:110:4: error: value of type 'String' has no member 'hasSuffix'
if pippo.hasSuffix("ao") {
   ^~~~~ ~~~~~~~~~

Same code works well on OS X.
I'm using Ubuntu 14.04 with required dependencies (clang and libicu-dev)
installed.
Any idea?

Cheers, Alessandro

--
Ing. Alessandro Morgantini - http://www.morgantini.org/
Email: gpz500@technologist.com
Cell.: +39-347 761 1221 - Fax: +39-390 347 761 1221
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Ing. Alessandro Morgantini - http://www.morgantini.org/
Email: gpz500@technologist.com
Cell.: +39-347 761 1221 - Fax: +39-390 347 761 1221

There’s a FIXME and a radar (18878343) in the source for StringLegacy:

https://github.com/apple/swift/blob/master/stdlib/public/core/StringLegacy.swift#L98

Can the information be re-created as a SwiftBug instead?

Alex

···

Begin forwarded message:

From: Alex Blewitt <alex.blewitt@gmail.com>
Subject: Re: [swift-users] hasSuffix(_:slight_smile: and .hasPrefix(_:slight_smile: String methods missing on Linux
Date: 11 December 2015 11:48:02 GMT
To: Alessandro Morgantini <gpz500@technologist.com>
Cc: swift-users@swift.org

They may come as extensions from NSString, which Swift doesn’t have fully implemented on Linux yet.

Alex

On 11 Dec 2015, at 11:18, Alessandro Morgantini via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello to everyone!
I'm a very newbie to Swift, I'm doing first experiments on Linux & Mac and I have the following issue: if I call the hasSuffix(_:slight_smile: or the hasPrefix(_:slight_smile: methods on a String instance, I obtain the following error:

$ swiftc prova.swift
prova.swift:110:4: error: value of type 'String' has no member 'hasSuffix'
if pippo.hasSuffix("ao") {
   ^~~~~ ~~~~~~~~~

Same code works well on OS X.
I'm using Ubuntu 14.04 with required dependencies (clang and libicu-dev) installed.
Any idea?

Cheers, Alessandro

--
Ing. Alessandro Morgantini - http://www.morgantini.org/
Email: gpz500@technologist.com <mailto:gpz500@technologist.com>
Cell.: +39-347 761 1221 - Fax: +39-390 347 761 1221
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

This is still affecting linux, I see the discussion is from 2015, but look at the example from Swift docs:

import Foundation


let romeoAndJuliet = [
    "Act 1 Scene 1: Verona, A public place",
    "Act 1 Scene 2: Capulet's mansion",
    "Act 1 Scene 3: A room in Capulet's mansion",
    "Act 1 Scene 4: A street outside Capulet's mansion",
    "Act 1 Scene 5: The Great Hall in Capulet's mansion",
    "Act 2 Scene 1: Outside Capulet's mansion",
    "Act 2 Scene 2: Capulet's orchard",
    "Act 2 Scene 3: Outside Friar Lawrence's cell",
    "Act 2 Scene 4: A street in Verona",
    "Act 2 Scene 5: Capulet's mansion",
    "Act 2 Scene 6: Friar Lawrence's cell"
]

var act1SceneCount = 0
for scene in romeoAndJuliet {
    if scene.hasPrefix("Act 1 ") {
        act1SceneCount += 1
    }
}

print("There are \(act1SceneCount) scenes in Act 1")

Gives me:

🏃  e[0;3m$ cd "/home/luis/.marathon/Scripts/Cache/-home-luis-Downloads-untitled/" && readlink OriginalFilee[0;23m
   e[0;3m/home/luis/Downloads/untitled.swifte[0;23m
   e[0;3m$ cd "/home/luis/Downloads/" && swift package --versione[0;23m
   e[0;3mSwift Package Manager - Swift 4.1.0e[0;23m
   e[0;3m$ cd "/home/luis/.marathon/Scripts/Cache/-home-luis-Downloads-untitled/" && swift build -C /home/luis/.marathon/Scripts/Cache/-home-luis-Downloads-untitled/ e[0;23m
   e[0;3mCompile Swift Module 'untitled' (1 sources)
   Linking ./.build/x86_64-unknown-linux/debug/untitled
   Linking ./.build/x86_64-unknown-linux/debug/libMarathonDependencies.so
   cannot find section .interp
   <unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)e[0;23m
   e[0;3mwarning: '--chdir/-C' option is deprecated; use '--package-path' instead
   error: terminated(1): /usr/lib/swift/bin/swift-build-tool -f /home/luis/.marathon/Scripts/Cache/-home-luis-Downloads-untitled/.build/debug.yaml main output:
       
   e[0;23m
[Finished in 0.8s with exit code 1]
  • Am I doing something wrong, or these properties are not implemented in Foundation still?

None of those errors seem relevant to these methods. I suggest asking for help in a separate thread. (I haven't seen this cannot find section .interp myself before.)

1 Like

The discussion earlier is from 2015, and this was fixed in https://github.com/apple/swift/pull/14390

2 Likes