全局变量不保留内容

时间:2015-12-02 19:47:20

标签: ios swift

我的班级中有一个全局变量,称为联系人,初始化为 - var contacts:[Name]

走向全班同学。我想知道为什么数组不保留填充值。数组重置的位置在下面代码的注释中。

func extractFromPhone()->[Name]{

  swiftAddressBook?.requestAccessWithCompletion({ (success, error) -> Void in
        if success {
            if let people : [SwiftAddressBookPerson]? = swiftAddressBook?.allPeople {
                for person in people! {

                    // Get Names
                    var name: Name = Name();
                    {
                        name.nickName = nickname;
                    }
                    if let fn = person.firstName
                    {
                        name.firstName = fn;
                    }
                    if let mn = person.middleName
                    {
                        name.middleName = mn;
                    }
                    if let ln = person.lastName
                    {
                        name.lastName = ln;
                    }


                     contacts.append(name);
                }
            }
        }
        else {
        }
// contacts array properly filled here
    })

// contacts array becomes empty here
}

0 个答案:

没有答案