如何将字典作为函数参数传递?

时间:2016-01-03 11:16:35

标签: swift dictionary

我正在尝试将字典作为函数参数传递。我有以下功能

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
}

我想通过

触发
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]可行的解决方案,但不是最优的,因为我没有使用我的访客结构

func makeAndAddVisitorRecord2(visitorDict: Dictionary <Int, String>) -> 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类,它会生成一些Visitors,看起来像

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

4 个答案:

答案 0 :(得分:9)

我不确定您要对ABRecordRef <AnyObject, AnyObject>做什么,但<>语法用于指定泛型类型。就像,一个包含字符串的数组是Array<String>ABRecordRef不是通用类型。

词典需要具有在参数中指定的类型:Dictionary<String, Int>

另外,你正在处理类似数组的字典。最好使用字典,因为它意味着使用。而不是[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 framework,而不是ABAddressBook。

答案 1 :(得分:4)

Swift中的字典类型称为Dictionary。但是,它是通用的,这意味着您需要在类型名称后添加<>以指定您希望它的字典类型。在这种情况下,它是Dictionary<Int, String>。这是因为您传递给方法(visitorDict)的变量属于Dictionary<Int, String>类型。

像这样编写函数头:

func makeAndAddVisitorRecord2(visitorDict: Dictionary<Int, String>) -> ABRecordRef <AnyObject, AnyObject> {

如果您想更进一步,可以使用字典的简写类型名称:

func makeAndAddVisitorRecord2(visitorDict: [Int: String]) -> ABRecordRef <AnyObject, AnyObject> {

此外,如果您的字典键是顺序的,如1,2,3,4,5等,您可以使用数组:

func makeAndAddVisitorRecord2(visitorDict: Array<String>) -> ABRecordRef <AnyObject, AnyObject> {

数组的简写是:

func makeAndAddVisitorRecord2(visitorDict: [String]) -> ABRecordRef <AnyObject, AnyObject> {

答案 2 :(得分:1)

你必须在swift中指定它是什么类型的字典,如下所示:

func makeAndAddVisitorRecord2(visitorDict: [Int: String]) -> 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
}

然后你可以使用makeAndAddVisitorRecord2(visitorDict),一切都应该有效。

您的集合类型apple doc有更深入的示例。

答案 3 :(得分:0)

ABRecordRef不是通用的,因此你不能像你那样使用。您很可能想要将通用声明添加到Dictionary参数。