@@ -54,23 +54,49 @@ class ViewController : UIViewController, CNContactPickerDelegate, CNContactViewC
54
54
//
55
55
56
56
@IBAction func doFindMoi ( sender: AnyObject ! ) {
57
-
58
57
CNContactStore ( ) . requestAccessForEntityType ( . Contacts) {
59
58
ok, err in
60
59
guard ok else {
61
60
print ( " not authorized " )
62
61
return
63
62
}
63
+ var which : Int { return 2 } // 1 or 2
64
64
do {
65
- let pred = CNContact . predicateForContactsMatchingName ( " Matt " )
66
- var matts = try CNContactStore ( ) . unifiedContactsMatchingPredicate ( pred, keysToFetch: [
67
- CNContactFamilyNameKey, CNContactGivenNameKey
68
- ] )
69
- matts = matts. filter { $0. familyName == " Neuburg " }
70
- guard var moi = matts. first else {
71
- print ( " couldn't find myself " )
72
- return
65
+ var premoi : CNContact !
66
+ switch which {
67
+ case 1 :
68
+ let pred = CNContact . predicateForContactsMatchingName ( " Matt " )
69
+ var matts = try CNContactStore ( ) . unifiedContactsMatchingPredicate ( pred, keysToFetch: [
70
+ CNContactFamilyNameKey, CNContactGivenNameKey
71
+ ] )
72
+ matts = matts. filter { $0. familyName == " Neuburg " }
73
+ guard var moi = matts. first else {
74
+ print ( " couldn't find myself " )
75
+ return
76
+ }
77
+ premoi = moi
78
+ case 2 :
79
+ let pred = CNContact . predicateForContactsMatchingName ( " Matt " )
80
+ let req = CNContactFetchRequest ( keysToFetch: [
81
+ CNContactFamilyNameKey, CNContactGivenNameKey
82
+ ] )
83
+ req. predicate = pred
84
+ var matt : CNContact ? = nil
85
+ try CNContactStore ( ) . enumerateContactsWithFetchRequest ( req) {
86
+ con, stop in
87
+ if con. familyName == " Neuburg " {
88
+ matt = con
89
+ stop. memory = true
90
+ }
91
+ }
92
+ guard var moi = matt else {
93
+ print ( " couldn't find myself " )
94
+ return
95
+ }
96
+ premoi = moi
97
+ default : break
73
98
}
99
+ var moi = premoi
74
100
print ( moi)
75
101
if moi. isKeyAvailable ( CNContactEmailAddressesKey) {
76
102
print ( moi. emailAddresses)
@@ -91,8 +117,8 @@ class ViewController : UIViewController, CNContactPickerDelegate, CNContactViewC
91
117
print ( error)
92
118
}
93
119
}
94
-
95
120
}
121
+
96
122
97
123
@IBAction func doCreateSnidely ( sender: AnyObject ! ) {
98
124
let snidely = CNMutableContact ( )
0 commit comments