11

I'm writing an app that uses bridgeToObjectiveC() on a String object. Since Beta 5 this is no longer available.

I'm trying to do this:

self.myList.filter{($0 as MyClass).name.bridgeToObjectiveC().localizedCaseInsensitiveContainsString(searchText)}

Which gives me the error:

'String' does not have a member named 'bridgeToObjectiveC'

What is the equivalent code in Beta 5?

Tristan Warner-Smith
  • 9,631
  • 6
  • 46
  • 75
Patrick Bassut
  • 3,310
  • 5
  • 31
  • 54
  • possible duplicate of [bridgeToObjectiveC and makeObjectsPerformSelector in Swift beta 5](http://stackoverflow.com/questions/25126188/bridgetoobjectivec-and-makeobjectsperformselector-in-swift-beta-5) – Martin R Aug 05 '14 at 04:28
  • @MartinR He's talking about arrays, I'm talking about strings. No? – Patrick Bassut Aug 05 '14 at 04:31
  • 1
    The problem is the same (bridgeToObjectiveC not available anymore) and the solution is the same ("... Instead, cast to/from the appropriate Foundation type"). – Martin R Aug 05 '14 at 04:40

2 Answers2

16

Use as to cast to NSString for the same effect:

("string" as NSString).localizedCaseInsensitiveCompare("other string")

Or like this with optional chaining:

("string" as NSString?)?.localizedCaseInsensitiveCompare("other string")
jstn
  • 2,326
  • 1
  • 17
  • 15
3

try

_bridgeToObjectiveC()

instead of

bridgeToObjectiveC()

as follows:

self.myList.filter{($0 as MyClass).name._bridgeToObjectiveC().localizedCaseInsensitiveContainsString(searchText)}
SteveFerg
  • 3,466
  • 7
  • 19
  • 31
Timothy B
  • 41
  • 5
  • 1
    Swift 2.0 fix this problem. And this behaviour is working. var yearlyWorkDays = workDaysTxt.text._bridgeToObjectiveC().float – Durul Dalkanat Aug 31 '15 at 12:02