0

I'm attempting to write the following code in swift, but having trouble doing so:

Objective - C:

NSArray * seachArray = [sessionSearchResults filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(ProductModel* evaluatedObject, NSDictionary *bindings) {
    return [evaluatedObject.productName rangeOfString:searchString options:NSCaseInsensitiveSearch].location != NSNotFound;
}]];

Swift (What I have so far)

func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {

    let filteredArray = searchArray.filter { (session: ProductModel) -> Bool in
        //Contains String?
        return session.productName == searchString 
    }

    var S : String
    for S in filteredArray {    
        println(S)
    }

    return true
}

Just curious. Is there a away to achieve this without importing foundation?

Greg
  • 9,068
  • 6
  • 49
  • 91
imnill
  • 59
  • 5
  • 2
    possible duplicate of [How do I check if a string contains another string in Swift?](http://stackoverflow.com/questions/24034043/how-do-i-check-if-a-string-contains-another-string-in-swift) – luk2302 May 15 '15 at 14:11
  • Yeah, I noticed that. I was wondering if there was a way to find a range of string without importing foundation. – imnill May 15 '15 at 14:12
  • your question doesn't make any sense. If you are using UISearchDisplayController then that's UIKit which depends upon Foundation as a quick look at the header file UISearchDisplayController.h will confirm: import Foundation import UIKit – Max MacLeod Oct 08 '15 at 08:47

1 Answers1

1

Short answer: No.

Swift has straightforward bridging between it's classes and foundation classes, e.g. String to NSString.

NSString has an incredibly rich set of methods. Don't be afraid of Foundation, embrace it.

However, that means importing Foundation.

Your alternative is to write your own native Swift functions that replace the Foundation APIs. You can do that for a function here or there if you want to, but it seems absurd. There are millions of lines of industrial grade, efficient, well tested code behind the Foundation classes. They are part of your runtime environment. Learn to use them.

Duncan C
  • 128,072
  • 22
  • 173
  • 272