Search code examples
iosswiftnspredicatensrange

Error: Int is not convertible to NSRange


I'm getting the error:

int is not convertible to NSRange

on var range:NSrange = 0:

   let predicate = NSPredicate(block: { (city: AnyObject!, b: [NSObject : AnyObject]!) -> Bool in
            var range:NSRange = 0;  //convert error happens here on 0
            if city is NSString {
                range = city.rangeOfString(searchBarText, options: NSStringCompareOptions.CaseInsensitiveSearch)
            }

            return range.location != NSNotFound
        })

any ideas on how to fix this?


Solution

  • NSRange is a struct with members location and length, not an Int

    use the initializer

    var range: NSRange = NSRange(location:0, length:0)
    

    or the convenience function

    var range: NSRange = NSMakeRange(0, 0)