1

I have the following instance function in a String extension:

func numberOfCharactersInATweet(withShortURLLength shortURLLength:UInt, andShortURLLengthHTTPS shortURLLengthHTTPS:UInt) -> NSInteger {
    var numCharacters = 0
    let stringLength = self.count
    self.enumerateSubstrings(in: Range(NSMakeRange(0, stringLength))!, options: NSString.EnumeratingOptions.byComposedCharactersSequences) { (subString, subStringRange, enclosingRange, stop) in
        numCharacters = numCharacters + 1
        (Ambiguous reference to member '+')
    }

    return numCharacters
}

I think I know why I'm getting that message. It's because I want to increment an integer variable but that member '+' is also defined as a concatenation operator in the String class I'm extending. How can I tell to use that '+' member for integers?

pedroremedios
  • 763
  • 1
  • 11
  • 39

1 Answers1

2

I doubt that this has anything to do with any + override. Even as it stands, with no such override, I couldn't get your code to compile; it seems to be in a very strange version of Swift. I was able to get it to compile in Swift 5 in a String extension by making a few corrections:

func numberOfCharactersInATweet(withShortURLLength shortURLLength:UInt, andShortURLLengthHTTPS shortURLLengthHTTPS:UInt) -> NSInteger {
    var numCharacters = 0
    let stringLength = self.utf16.count
    let range = self.startIndex..<self.endIndex
    self.enumerateSubstrings(in: range, options: .byComposedCharacterSequences) { (subString, subStringRange, enclosingRange, stop) in
        numCharacters = numCharacters + 1
    }
    return numCharacters
}

However, your code is extremely weird. None of the parameters (shortURLLength, shortURLLengthHTPPS, subString, subStringRange, enclosingRange, stop) is used for anything. As Rob Napier has pointed out in a comment, it is difficult to see what purpose this String extension serves; all you're doing is counting characters. Is this supposed to be a Swift translation of an Objective-C method? If so, it isn't needed; if you merely want to know the character count in composed Unicode, that, in modern Swift, is the string's count:

let s = "This is a string  with some emoji in it ."
print(s.count)
print(s.numberOfCharactersInATweet(withShortURLLength: 0, andShortURLLengthHTTPS: 0))
// both give "43"
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I could reproduce the error on Xcode 10.1 (after fixing other compilation errors first). – Sulthan May 05 '19 at 15:17
  • The function doesn't only do that. It sees if the tweet has a short URL and adjusts the tweet count accordingly. I haven't put that code in yet. – pedroremedios May 05 '19 at 15:19
  • @pedroremedios It is only possible to answer the question you actually ask. If you do not expose all your relevant code when you ask your question, you won't get a good answer. I answered the question you _actually asked_. You were getting a compile error and I showed how to get rid of it. – matt May 05 '19 at 15:21
  • Moreover, the code you showed was full of other errors. Just to give an example, in no language whatever is there something called `self.enumerateSubStrings` on a String or NSString. It looks like you tried to type your code by hand into your question, and you typed it wrong. Never do that. Copy and paste _complete, real code_ from your problematic project directly into the browser when you ask a code question on Stack Overflow. It is up to you to do your homework _before_ you ask, creating an MCVE (https://stackoverflow.com/help/mcve). – matt May 05 '19 at 15:26
  • 1
    Older Swift versions (<= 3) did not treat “Emoji sequences” as a single grapheme cluster, and enumerating the string with the byComposedCharacterSequences option was a possible workaround (https://stackoverflow.com/q/39104152/1187415). – Martin R May 05 '19 at 15:51
  • @MartinR Very good point; I'll add "in modern Swift". (However, that paragraph is not the point of my answer. The point is merely to translate the OP's rather mysterious non-code into something that compiles.) – matt May 05 '19 at 15:53
  • Yes, I am aware of that. Another (irrelevant) nitpicking: stringLength and the NSRange are not needed: `self.enumerateSubstrings(in: startIndex...,` – Martin R May 05 '19 at 15:56
  • @matt I was only answering what you asked about if the function I posted only did that. I posted my question at that stage because I'm I stop when I get a syntax error like that. – pedroremedios May 05 '19 at 16:05
  • @MartinR Of course, but again, I just wanted to get the code he actually showed past the compiler. I really shouldn't even have corrected `stringLength = self.count`; I should have let the code stew in its own juices. I suppose I should correct this too... – matt May 05 '19 at 16:10