I am trying to pass a dictionary as a function parameter. I have the following function
func makeAndAddVisitorRecord2(visitorDict: Dictionary) -> ABRecordRef <AnyObject, AnyObject> {
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
}
Which i like to trigger by
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)
}
But makeAndAddVisitorRecord2 compiles an error
Cannot specialize non-generic type 'ABRecordRef' (aka 'AnyObject')
[EDIT 1] workable solution but not optimal as i am not using my Visitor struct
func makeAndAddVisitorRecord2(visitorDict: Dictionary <Int, String>) -> ABRecordRef {
[EDIT 2] as @rsmoz pointed out i should use my Visitor struct
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
}
}
So i have a ListVisitors class which generates some Visitors and looks like
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: "[email protected]")
sharedInstance = [visitor]
visitor = Visitor(visitorName: "From Class John", visitorCompany: "nike", visitorPlace: "New York", visitorPhone: "94888484", visitorEmail: "[email protected]")
// ListVisitors.sharedInstance += [visitor]
sharedInstance += [visitor]
...
}
}
And in my main controller i have a table view and a selected row sends the visitor details to detailcontroller (HOW can i have the selected visitor struct in detail view controller?? Should i pass let selectedVisitor to the detail view controller?)
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
Int:String
dictionary? Just use array.let visitorArr: Array = [String]() and visitorArr["visitorName"] = visitorName
and tried to pass the array as parameter. Could you provide a simple code example??? Any why would using an array > dictionary??