gpt4 book ai didi

Swift:将用户在 ABPersonViewController 中选择的信息复制到字典

转载 作者:可可西里 更新时间:2023-11-01 01:39:21 26 4
gpt4 key购买 nike

我正在尝试实现 func personViewController(personViewController: ABPersonViewController!,
shouldPerformDefaultActionForPerson person: ABRecord!,
property property: ABPropertyID,
identifier valueIdentifier: ABMultiValueIdentifier) -> Bool
函数,它是 ABPersonViewControllerDelegate 的一部分协议(protocol),每当用户点击 ABPersonViewController 中的项目时调用, 这样用户选择的任何信息都将被复制到 [String : String]字典使得属性名称将成为属性值的键:[..."kABPersonFirstNameProperty" : "Alexander"...] , 说。

我还想避免使用开关或一长串条件来测试某个属性是这个还是那个;我宁愿尽可能笼统地处理它——我只尝试两种不同的情况:属性是单值还是多值。如果它是一个多值,我想复制所有可用的信息。

例如,如果用户点击地址,结果可能是这样的:[..."kABPersonAddressStreetKey" : "1 Infinite Loop", "kABPersonAddressCityKey" : "Cupertino" , "kABPersonAddressStateKey", "California (or CA?)"...] .

经过数小时搜索 Apple Developer Library 和相关的 SO 问题(我知道这很可悲),这就是我所拥有的:

func personViewController(personViewController: ABPersonViewController!,
shouldPerformDefaultActionForPerson person: ABRecord!,
property property: ABPropertyID,
identifier valueIdentifier: ABMultiValueIdentifier) -> Bool {

s["kABPersonFirstNameProperty"] = ABRecordCopyValue(person, kABPersonFirstNameProperty) as! String //the name can't actually be selected by the user, but I want to capture it anyway
s["kABPersonLastNameProperty"] = ABRecordCopyValue(person, kABPersonLastNameProperty) as! String

if valueIdentifier == 0 { //property is a single property, not a multivalue property

let record = ABRecordCopyValue(person, property)
s[property as! String!] = record as! String

} else { //property is an ABMultiValue

let multiRecord = ABRecordCopyValue(person, property) as! ABMultiValueRef
s[property as! String] = ABMultiValueGetIndexForIdentifier(multiRecord, valueIdentifier)

}

return false

}

我可以看出缺少了很多部分——是否有可能将其浓缩成一本字典?

提前致谢(并向提供完整、正确答案的人提供 100 声望)。

最佳答案

以 vadian 的回答为起点,我编写了以下内容,它将收集并复制到字典中几乎所有用户可以选择的信息(以及一些用户无法点击的信息 - 名字,姓氏名称、组织——自动)。

这是一项正在进行的工作,随着我继续添加对更多属性的支持,我将对其进行更新,但目前它适用于名字、姓氏、电子邮件、电话号码、地址和一些社交资料。

在某些情况下我不得不使用间接方法:将标签没有指定键的电话号码存储为 kAB_Label s,例如,它还没有采用电子邮件地址的标签。

s自类型 Dictionary<String, AnyObject> ,这里是:

func personViewController(personViewController: ABPersonViewController!, shouldPerformDefaultActionForPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) -> Bool {

addPropertyForKey("kABPersonFirstNameProperty", person : person, property: kABPersonFirstNameProperty)
addPropertyForKey("kABPersonLastNameProperty", person : person, property: kABPersonLastNameProperty)
addPropertyForKey("kABPersonOrganizationProperty", person: person, property: kABPersonOrganizationProperty)

if (property == kABPersonAddressProperty) {
let multiRecord : ABMultiValueRef = ABRecordCopyValue(person, property).takeUnretainedValue()
let index = ABMultiValueGetIndexForIdentifier(multiRecord, identifier)
let addressRecord :AnyObject = ABMultiValueCopyValueAtIndex(multiRecord, index).takeUnretainedValue()
addValuesFromRecord(addressRecord, forKeys:["Street", "City", "State", "ZIP", "Country", "CountryCode"])

}

if (property == kABPersonEmailProperty) {

let multiRecord: ABMultiValueRef = ABRecordCopyValue(person, property).takeUnretainedValue()
let index = ABMultiValueGetIndexForIdentifier(multiRecord, identifier)
let email = ABMultiValueCopyValueAtIndex(multiRecord, index).takeUnretainedValue() as! String

if (s["kABPersonEmailProperty(1)"] == nil) {

s["kABPersonEmailProperty(1)"] = email

} else {

s["kABPersonEmailProperty(2)"] = email

}

}

if (property == kABPersonSocialProfileProperty) {

let multiRecord: ABMultiValueRef = ABRecordCopyValue(person, property).takeUnretainedValue()
let index = ABMultiValueGetIndexForIdentifier(multiRecord, identifier)
let profile = ABMultiValueCopyValueAtIndex(multiRecord, index).takeUnretainedValue()
let profileType = profile["service"] as! String
let profileName = profile["username"] as! String

switch profileType {
case "facebook" : s["kABPersonSocialProfileServiceFacebook"] = profileName
case "twitter" : s["kABPersonSocialProfileServiceTwitter"] = profileName
case "sinaweibo": s["kABPersonSocialProfileServiceSinaWeibo"] = profileName
default: break
}



}

if (property == kABPersonPhoneProperty) {

let multiRecord : ABMultiValueRef = ABRecordCopyValue(person, property).takeUnretainedValue()
let index = ABMultiValueGetIndexForIdentifier(multiRecord, identifier)
let number = ABMultiValueCopyValueAtIndex(multiRecord, index).takeUnretainedValue() as! String

let locLabel: CFStringRef = (ABMultiValueCopyLabelAtIndex(multiRecord, index) != nil) ? ABMultiValueCopyLabelAtIndex(multiRecord, index).takeUnretainedValue() as CFStringRef : ""
let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))

var cfStr:CFTypeRef = locLabel
var nsTypeString = cfStr as! NSString
var a:String = nsTypeString as String

var b = a

if (a.rangeOfString("_$!<") != nil) {

b = a.substringFromIndex(a.rangeOfString("_$!<")!.endIndex)

b = b.substringToIndex(b.rangeOfString(">!$_")!.startIndex)

}

switch b {

case "Mobile" : s["kABPersonPhoneMobileLabel"] = number
case "iPhone" : s["kABPersonPhoneIPhoneLabel"] = number
case "Main" : s["kABPersonPhoneMainLabel"] = number
case "Home" : s["kABHomeLabel"] = number
case "Work" : s["kABWorkLabel"] = number
case "Other" : s["kABOtherLabel"] = number
default: break

}

}

println(s)

return false
}

拜托,任何人都可以随意复制其中的任何/所有部分,这将有所帮助。

关于Swift:将用户在 ABPersonViewController 中选择的信息复制到字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31822329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com