AutoCompleteTextField和Firebase

时间:2016-10-03 19:05:31

标签: ios swift firebase autocomplete firebase-realtime-database

我正在尝试在Swift中集成Firebase和AutoCompleteTextField,以便我在textField中自动完成。我在尝试将字典转换为数组时遇到问题,以便我可以在autoCompleteTextField.autoCompleteStrings中设置它。这是我的代码。

func handleTextFieldInterfaces() {

    let ref = FIRDatabase.database().reference().child("Airport")
        ref.observeEventType(.Value, withBlock: { (snapshot) in
    self.autoCompleteTextField.onTextChange = {[weak self] text in

            if !text.isEmpty {
            var fbosStuff = [String]()
                if let snaps = snapshot.value as? [[String: AnyObject]] {
                for places in snaps {
                    print(places)
                    let names = places["code"] as? String
                    fbosStuff.append(names!)
                }
                 self!.autoCompleteTextField.autoCompleteStrings = fbosStuff

                }
            }

        }
    })
}

我试图在文本字段中添加来自Firebase的响应是。

Snap (Airport) {
"Long Beach" =     {
    FBOs =         {
        Atlantic =             {
            100LL = "7.0";
            freq = "120.1";
            fullname = "Atlantic, Long Beach, KLGB";
            "jet-A" = "5.5";
            "phone number" = "(800) 554-3591";
        };
        Signature =             {
            100ll = "7.0";
            email = "lgb@signatureflight.com";
            freq = "120.1";
            fullname = "Signature, Long Beach, KLGB";
            "jet-a" = "5.5";
            phonenumber = "(800) 554-3591";
        };
    };
    code = KLGB;
    fieldname = Daughtery;
    location = "Long Beach, California, USA";
};
"Santa Monica" =     {
    FBOs =         {
        "American Flyers" =             {
            100ll = "5.38";
            freq = "123.3";
            fullname = "American Flyers, Santa Monica, KSMO";
            phonenumber = "(310) 390-2099";
        };
        Atlantic =             {
            100ll = "7.79";
            freq = "122.95";
            fullname = "Atlantic, Santa Monica, KSMO";
            "jet-a" = "7.19";
            phonenumber = "(310) 396-6770";
        };
    };
    code = KSMO;
    fieldname = "Santa Monica Muni Airport";
    location = "Santa Monica, California, USA";
};

}

1 个答案:

答案 0 :(得分:0)

尝试: -

Swift 2

for places in snaps {

  print(places.key)  // Will give you the key
  let names = places["code"] as? String
  fbosStuff.append(names!)

 }

Swift 3

public void SendEmail(MyInternalSystemEmailMessage email)
{
    var mailMessage = new System.Net.Mail.MailMessage();
    mailMessage.From = new System.Net.Mail.MailAddress(email.FromAddress);
    mailMessage.To.Add(email.ToRecipients);
    mailMessage.ReplyToList.Add(email.FromAddress);
    mailMessage.Subject = email.Subject;
    mailMessage.Body = email.Body;
    mailMessage.IsBodyHtml = email.IsHtml;

    foreach (System.Net.Mail.Attachment attachment in email.Attachments)
    {
        mailMessage.Attachments.Add(attachment);
    }

    var mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mailMessage);

    var gmailMessage = new Google.Apis.Gmail.v1.Data.Message {
        Raw = Encode(mimeMessage.ToString())
    };

    Google.Apis.Gmail.v1.UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(gmailMessage, ServiceEmail);

    request.Execute();
}

public static string Encode(string text)
{
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);

    return System.Convert.ToBase64String(bytes)
        .Replace('+', '-')
        .Replace('/', '_')
        .Replace("=", "");
}