我试图将字典作为函数参数传递.我有以下功能
func makeAndAddVisitorRecord2(visitorDict: Dictionary) -> ABRecordRef{ let visitorRecord: ABRecordRef = ABPersonCreate().takeRetainedValue() ABRecordSetValue(visitorRecord, kABPersonFirstNameProperty, visitorDict[1], nil) ABRecordSetValue(visitorRecord, kABPersonLastNameProperty, visitorDict[2], nil) //ABRecordSetValue(visitorRecord, kABPersonEmailProperty, visitorDict[5], nil) let phoneNumbers: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue() ABMultiValueAddValueAndLabel(phoneNumbers, visitorDict["visitorPhone"], kABPersonPhoneMainLabel, nil) ABRecordSetValue(visitorRecord, kABPersonPhoneProperty, phoneNumbers, nil) ABAddressBookAddRecord(addressBookRef, visitorRecord, nil) saveAddressBookChanges() return visitorRecord }
我喜欢触发哪个
func addVisitorToContacts(sender: AnyObject) { //let visitor = ListVisitors[visitorButton.tag] var visitorDict:[Int:String] = [1:"\(visitorName)", 2:"\(visitorCompany)", 3:"\(visitorCity)", 4:"\(visitorPhone)", 5:"\(visitorEmail)"] let visitorRecord: ABRecordRef = makeAndAddVisitorRecord2(visitorDict) let contactAddedAlert = UIAlertController(title: "\(visitorName) was successfully added.", message: nil, preferredStyle: .Alert) contactAddedAlert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil)) presentViewController(contactAddedAlert, animated: true, completion: nil) }
但makeAndAddVisitorRecord2编译错误
Cannot specialize non-generic type 'ABRecordRef' (aka 'AnyObject')
[编辑1]可行的解决方案,但不是最佳的,因为我没有使用我的Visitor结构
func makeAndAddVisitorRecord2(visitorDict: Dictionary) -> ABRecordRef {
[编辑2]正如@rsmoz指出我应该使用我的Visitor结构
class Visitor { var visitorName : String var visitorCompany : String var visitorPlace : String var visitorPhone : String var visitorEmail : String init(visitorName: String, visitorCompany: String, visitorPlace: String, visitorPhone: String, visitorEmail: String) { self.visitorName = visitorName self.visitorCompany = visitorCompany self.visitorPlace = visitorPlace self.visitorPhone = visitorPhone self.visitorEmail = visitorEmail }
}
所以我有一个ListVisitors类,它生成一些访问者,看起来像
class ListVisitors{ static var sharedInstance = [Visitor]() static func load() { // @todo: stored and loaded data var visitor = Visitor(visitorName: "From Class Matt", visitorCompany: "Google", visitorPlace: "San Diego", visitorPhone: "94888484", visitorEmail: "matt@google.com") sharedInstance = [visitor] visitor = Visitor(visitorName: "From Class John", visitorCompany: "nike", visitorPlace: "New York", visitorPhone: "94888484", visitorEmail: "john@nike.com") // ListVisitors.sharedInstance += [visitor] sharedInstance += [visitor] ... } }
在我的主控制器中,我有一个表视图和一个选定的行将访问者详细信息发送到detailcontroller(如何在详细视图控制器中具有所选的访问者结构?我应该将selectVisitor传递给详细视图控制器吗?)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ if (segue.identifier == "visitorDetails") { if let indexPath = tableView.indexPathForCell(sender as! UITableViewCell) { let selectedVisitor = lVisitors[indexPath.row] as Visitor let detailVC = segue.destinationViewController as! DetailViewController detailVC.visitorName = selectedVisitor.visitorName detailVC.visitorCompany = selectedVisitor.visitorCompany detailVC.visitorPlace = selectedVisitor.visitorPlace detailVC.visitorPhone = selectedVisitor.visitorPhone detailVC.visitorEmail = selectedVisitor.visitorEmail } // .end accessory select } // .end segue
rsmoz.. 9
我不确定你要做什么ABRecordRef
,但<>
语法是指定泛型类型.就像,一个包含字符串的数组是Array
.ABRecordRef
不是通用类型.
Dictionary需要具有在参数中指定的类型: Dictionary
此外,您正在处理类似数组的字典.最好使用字典,因为它意味着使用.而不是[1:"\(visitorName)"]
,为什么不["visitorName":visitorName]
呢?这样你就可以访问它dict["visitorName"]
了,"\(visitorName)"
如果visitorName是一个开头的字符串,你也不需要这样做.只需直接使用变量即可.
但是,将Visitor表示为结构,而不是数组或字典会更好:
struct Visitor { let name: String let company: String let city: String let phone: String //Yes, this should be a String and not an Int let email: String }
你可以像这样设置它:
let v = Visitor(name: "Joe", company: "A Corp", city: "New York", phone: "+44 392 39275 22", email: "joe@smith.org")
并像这样访问它:
v.name
这就是更清洁,更安全.现在,您的代码不会因意外访问字典上的错误密钥而出现任何错误.
哦,你现在应该使用Contacts框架,而不是ABAddressBook.
我不确定你要做什么ABRecordRef
,但<>
语法是指定泛型类型.就像,一个包含字符串的数组是Array
.ABRecordRef
不是通用类型.
Dictionary需要具有在参数中指定的类型: Dictionary
此外,您正在处理类似数组的字典.最好使用字典,因为它意味着使用.而不是[1:"\(visitorName)"]
,为什么不["visitorName":visitorName]
呢?这样你就可以访问它dict["visitorName"]
了,"\(visitorName)"
如果visitorName是一个开头的字符串,你也不需要这样做.只需直接使用变量即可.
但是,将Visitor表示为结构,而不是数组或字典会更好:
struct Visitor { let name: String let company: String let city: String let phone: String //Yes, this should be a String and not an Int let email: String }
你可以像这样设置它:
let v = Visitor(name: "Joe", company: "A Corp", city: "New York", phone: "+44 392 39275 22", email: "joe@smith.org")
并像这样访问它:
v.name
这就是更清洁,更安全.现在,您的代码不会因意外访问字典上的错误密钥而出现任何错误.
哦,你现在应该使用Contacts框架,而不是ABAddressBook.