Skip to content

Instantly share code, notes, and snippets.

@scottdelly
Created January 22, 2016 04:42
Show Gist options
  • Save scottdelly/534a6ecb31acec6c7715 to your computer and use it in GitHub Desktop.
Save scottdelly/534a6ecb31acec6c7715 to your computer and use it in GitHub Desktop.
//Protocol for spotlight indexing
protocol SpotlightIndexable {
var spotlightTitle: String {get}
var spotlightDescription: String {get}
var spotlightKeywords: [String] {get}
var spotlightImageData: NSData? {get}
var spotlightUrl: NSURL {get}
func index()
}
//Object that will be indexed
struct Article {
let title: String
let image: UIImage?
let text: String
let tags: [String]
let url: NSURL
}
//Exactly how Article will be indexed
extension Article : SpotlightIndexable{
var spotlightTitle: String{
return self.title
}
var spotlightDescription: String{
return self.text
}
var spotlightKeywords: [String]{
return self.title.words + self.text.words
}
var spotlightImageData: NSData?{
guard let image = self.image else {return nil}
return UIImagePNGRepresentation(image)
}
var spotlightUrl: NSURL{
return self.url
}
func index(){
//Code this
}
}
//Helpers
extension String{
var words: [String]{
return characters.split{$0 == " "}
.map{String($0)}.map{$0
.stringByTrimmingCharactersInSet(NSCharacterSet
.punctuationCharacterSet())}
}
}
func + (lhs: [T], rhs: [T]) -> [T]{
var array = [T]()
array.appendContentsOf(lhs)
array.appendContentsOf(rhs)
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment