Skip to content

Instantly share code, notes, and snippets.

@sooop
Created October 8, 2015 01:28
Show Gist options
  • Select an option

  • Save sooop/34b95752c3927af2bd93 to your computer and use it in GitHub Desktop.

Select an option

Save sooop/34b95752c3927af2bd93 to your computer and use it in GitHub Desktop.
#!/usr/bin/swift
import Contacts
func update()
{
func makeNickname(name: String) -> String
{
let transform: UnicodeScalar -> String = { u in
let c = Int(u.value)
guard case 0xac00...0xd7a3 = c else { return "" }
return String(UnicodeScalar((c - 0xac00) / 28 / 21 + 0x1100))
}
return name.unicodeScalars.map(transform).joinWithSeparator("")
}
let store = CNContactStore()
let fetchRequest = CNContactFetchRequest(keysToFetch:[
CNContactGivenNameKey, CNContactNicknameKey])
let saveRequest = CNSaveRequest()
var updateCount: Int = 0
var mContact: CNMutableContact?
do {
try store.enumerateContactsWithFetchRequest(fetchRequest){
contact, stop in
let nickname = makeNickname(contact.givenName)
guard nickname.utf8.count > 0 && nickname != contact.nickname else {
return
}
print("업데이트: \(nickname) -> \(contact.givenName)")
mContact = contact.mutableCopy() as? CNMutableContact
mContact?.nickname = nickname
saveRequest.updateContact(mContact!)
updateCount += 1
}
try store.executeSaveRequest(saveRequest)
if updateCount > 0 {
print("\(updateCount)개의 연락처 별명을 업데이트했습니다.")
} else {
print("업데이트할 연락처를 찾지 못했습니다.")
}
} catch {
print("Error")
}
}
update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment