Skip to content
This repository was archived by the owner on Jul 5, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
NSPredicate/NSPredicate.playground/playground.xcworkspace/xcuserdata/dfreniche.xcuserdatad
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//: [Previous](@previous)
/*:

# Basic Compound Predicates
*/
/*:- `AND, &&: Logical AND`
- `OR, ||: Logical OR`
- `NOT, !: Logical NOT`

*/
import Foundation

class City: NSObject {
let name: String

init(name: String) {
self.name = name
}

// description lets you pretty print this class' instances in the sidebar
override var description: String {
return self.name
}
}

class Person: NSObject { // Must inherit from NSObject
let name: String
let age: Int
let city: City?

init(name: String, age: Int, city: City) {
self.name = name
self.age = age
self.city = city
}

// description lets you pretty print this class' instances in the sidebar
override var description: String {
if let city = self.city {
return self.name + " - \(self.age) years old - \(city)"
}
return self.name + " - \(self.age) years old"
}
}

let sf = City(name: "San Francisco")
let nyc = City(name: "New York")

let groucho = Person(name: "Groucho", age: 50, city: nyc)
let chicco = Person(name: "Chicco", age: 61, city: nyc)
let harpo = Person(name: "Harpo", age: 45, city: sf)
let zeppo = Person(name: "Zeppo", age: 61, city: sf)

let people: NSArray = [groucho, chicco, harpo, zeppo]

/*:

All people with age 61 AND who lives in NYC

*/

let pred = NSPredicate(format: "%K = %@ AND age = %d", "city.name", "New York", 61)
people.filteredArrayUsingPredicate(pred)


//: [Next](@next)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//: [Previous](@previous)

import Foundation

class Province: NSObject { // Must inherit from NSObject
let name: String

init(name: String) {
self.name = name
}

// description lets you pretty print this class' instances in the sidebar
override var description: String {
return self.name
}
}

let cadiz = Province(name: "Cádiz")
let malaga = Province(name: "Málaga")
let logrono = Province(name: "Logroño")
let madrid = Province(name: "Madrid")
let lugo = Province(name: "lugo")

let provinces: NSArray = [cadiz, malaga, logrono, madrid, lugo]


/*:

Contains give you even more power!

*/


let provincesStartingWithM = NSPredicate(format: "name contains [cd] 'a'")

provinces.filteredArrayUsingPredicate(provincesStartingWithM)

//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=61&amp;CharacterRangeLoc=694&amp;EndingColumnNumber=62&amp;EndingLineNumber=34&amp;StartingColumnNumber=1&amp;StartingLineNumber=34&amp;Timestamp=459152880.070413"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=61&amp;CharacterRangeLoc=650&amp;EndingColumnNumber=62&amp;EndingLineNumber=29&amp;StartingColumnNumber=1&amp;StartingLineNumber=29&amp;Timestamp=459153925.64804"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=9&amp;CharacterRangeLoc=510&amp;EndingColumnNumber=14&amp;EndingLineNumber=22&amp;StartingColumnNumber=5&amp;StartingLineNumber=22&amp;Timestamp=459153925.64804"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//: [Previous](@previous)
/*:

## NSPredicate Hello World

Given the Person class:
*/
import Foundation

class Person: NSObject { // Must inherit from NSObject or NSPredicate will fail at runtime
let name: String
let age: Int

init(name: String, age: Int) {
self.name = name
self.age = age
}

// description lets you pretty print this class' instances in the sidebar
override var description: String {
return self.name + " - \(self.age) years old"
}
}

/*:

and a bunch of People

*/

let groucho = Person(name: "Groucho", age: 50)
let chicco = Person(name: "Chicco", age: 61)
let harpo = Person(name: "Harpo", age: 45)
let zeppo = Person(name: "Zeppo", age: 61)

let people: NSArray = [groucho, chicco, harpo, zeppo]
// using a NSArray here because predicates work with them, not with regular Swift Arrays

/*:

we can get __all people of age == 61__ with a simple predicate

*/

let allAge61 = NSPredicate(format: "age = 61")

people.filteredArrayUsingPredicate(allAge61)

/*:

we can also __filter by name__

*/


let namesPredicate = NSPredicate(format: "name = 'Harpo'")

people.filteredArrayUsingPredicate(namesPredicate)


/*:

💻 __Your turn__: try to filter all people aged 50 years old, then everyone called _Zeppo_

*/



/*:

💻 __SOME DESTRUCTIVE FUN__: remove the NSObject parent class from Person and see what happens (__save before__)

*/


/*:

💻 More destructive fun__: change the NSArray from people, to create a Swift Array and see what happens
*/



/*:

💻 You can use `=` or `==` in comparisons. Try it

*/



//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/dfreniche/BTSync/Trabajo/Formacio%CC%81n/iOS/Curso%20iOS%20-%20Intermedio/Keynote/NSPredicate/NSPredicate.playground/Pages/HelloWorld.xcplaygroundpage#CharacterRangeLen=52&amp;CharacterRangeLoc=950&amp;EndingLineNumber=49&amp;StartingLineNumber=49&amp;Timestamp=459152495.90949"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=44&amp;CharacterRangeLoc=1004&amp;EndingColumnNumber=45&amp;EndingLineNumber=51&amp;StartingColumnNumber=1&amp;StartingLineNumber=51&amp;Timestamp=459152495.909787"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1164&amp;EndingColumnNumber=46&amp;EndingLineNumber=62&amp;StartingColumnNumber=9&amp;StartingLineNumber=62&amp;Timestamp=459152495.910064"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=50&amp;CharacterRangeLoc=1152&amp;EndingColumnNumber=51&amp;EndingLineNumber=62&amp;StartingColumnNumber=1&amp;StartingLineNumber=62&amp;Timestamp=459152495.910301"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//: [Previous](@previous)

/*:
# How to use this Playground

- press ⌘ + 1 to show / hide all pages in this Playground

![](pages.gif)

- click on a Page to jump into it. Read & start writing code!
- use the Previous / Next links to navigate through Pages
- you can see the results of your code in the right bar or add add a quick look

![](preview.gif)


*/

//: [Next](@next)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//: [Previous](@previous)


/*:
# Keypaths

A keypath is, as its name implies, a way to traverse your object graph from object to object. For example, if you have a Person class and every person lives in a different Town, you can get the town's name using `somePerson.city.cityName`

*/

import Foundation

class City: NSObject {
let name: String

init(name: String) {
self.name = name
}

// description lets you pretty print this class' instances in the sidebar
override var description: String {
return self.name
}
}

class Person: NSObject { // Must inherit from NSObject
let name: String
let age: Int
let city: City?

init(name: String, age: Int, city: City) {
self.name = name
self.age = age
self.city = city
}

// description lets you pretty print this class' instances in the sidebar
override var description: String {
if let city = self.city {
return self.name + " - \(self.age) years old - \(city)"
}
return self.name + " - \(self.age) years old"

}
}

let sf = City(name: "San Francisco")
let nyc = City(name: "New York")

let groucho = Person(name: "Groucho", age: 50, city: nyc)
let chicco = Person(name: "Chicco", age: 61, city: nyc)
let harpo = Person(name: "Harpo", age: 45, city: sf)
let zeppo = Person(name: "Zeppo", age: 61, city: sf)


let people: NSArray = [groucho, chicco, harpo, zeppo]

/*:

Filter all people who lives in San Francisco, using a KeyPath

*/

let allInSF = NSPredicate(format: "%K = %@", "city.name", "San Francisco")
people.filteredArrayUsingPredicate(allInSF)


/*:

💻 __EXERCISE__: filter all people living in New York

*/




/*:

💻 __EXERCISE__: add a new class (State) with property name. Add State to the city. Filter all people living in the California State

*/


//: [Solved exercise - try to do it, it's better 😉](Solutions)


//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=43&amp;CharacterRangeLoc=1609&amp;EndingColumnNumber=44&amp;EndingLineNumber=64&amp;StartingColumnNumber=1&amp;StartingLineNumber=64&amp;Timestamp=459153553.75978"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
Loading