创建列表对象Swift

时间:2016-06-02 10:37:58

标签: ios arrays swift

:我有问题。我创建列表医生show firstname和lastname。如何对象列表医生

 func autoCompleteTextField(textField: MLPAutoCompleteTextField!, possibleCompletionsForString string: String!, completionHandler handler: ([AnyObject]!) -> ()) {

    let  appDelegate: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    var requestParameters: [NSObject:AnyObject] = [NSObject: AnyObject]()
    requestParameters["name"] = textField.text
    // Wykonanie żądania
    appDelegate.objectManager.getObjectsAtPath("/doctors", parameters: requestParameters, success: {
        (rkoperation: RKObjectRequestOperation!, rkmap: RKMappingResult!) -> Void in

        if let doctors = rkmap.array as? [Doctor] {
            for doctor:Doctor in doctors {
                // show error Experession resolves to an unused I-value
                doctor.firstname
                self.listDoctors.append(doctor.firstname)
            }
        }
        }, failure: {
            (rkoperation: RKObjectRequestOperation!, error: NSError!) -> Void in
            print("Load filed with error: @", error!)
            self.performSelectorOnMainThread(#selector(AppDelegate.showFetchError), withObject: nil, waitUntilDone: true)
    })
    handler(listDoctors)

3 个答案:

答案 0 :(得分:1)

   var listDoctors: [String] = []

    func autoCompleteTextField(textField: MLPAutoCompleteTextField!, possibleCompletionsForString string: String!, completionHandler handler: ([AnyObject]!) -> ()) {

        let  appDelegate: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
        var requestParameters: [NSObject:AnyObject] = [NSObject: AnyObject]()
        requestParameters["name"] = textField.text
        // Wykonanie żądania
        appDelegate.objectManager.getObjectsAtPath("/doctors", parameters: requestParameters, success: {
            (rkoperation: RKObjectRequestOperation!, rkmap: RKMappingResult!) -> Void in

            if let doctors = rkmap.array as? [Doctor] {
                for doctor:Doctor in doctors {
                    var nameDoctoers = doctor.firstname

                    self.listDoctors.append(nameDoctors)
                }
            }
            }, failure: {
                (rkoperation: RKObjectRequestOperation!, error: NSError!) -> Void in
                print("Load filed with error: @", error!)
                self.performSelectorOnMainThread(#selector(AppDelegate.showFetchError), withObject: nil, waitUntilDone: true)
        })
        handler(listDoctors)
    }
}

答案 1 :(得分:0)

你应该创建String数组。

    //Array with dummy names
    var nameArray = ["Red","Raain","Rozi","Rozina","Rozinava", "Roy","Roy son","Rediss","Raj","Blue", "Yellow"]

    //Get name from the Doctor object
    var name = doctor.firstname
    name = ((name ?? "").isEmpty ? "" : name!)//if string is empty return default value
    print("name is \(name)")

    //append to array
    self.nameArray.append(name!)

    print("names are \(nameArray)")

答案 2 :(得分:0)

我现在更正了我的代码我添加姓氏arry listDoctors应该是返回名称和姓氏

  var listDoctors: [String] = []

    func autoCompleteTextField(textField: MLPAutoCompleteTextField!, possibleCompletionsForString string: String!, completionHandler handler: ([AnyObject]!) -> ()) {

        let  appDelegate: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
        var requestParameters: [NSObject:AnyObject] = [NSObject: AnyObject]()
        requestParameters["name"] = textField.text
        // Wykonanie żądania
        appDelegate.objectManager.getObjectsAtPath("/doctors", parameters: requestParameters, success: {
            (rkoperation: RKObjectRequestOperation!, rkmap: RKMappingResult!) -> Void in

            if let doctors = rkmap.array as? [Doctor] {
                for doctor:Doctor in doctors {

                    var nameDoctors = doctor.firstname
                    var lastnameDoctors = doctor.lastname
                    self.listDoctors.append(nameDoctors)
                }
            }
            }, failure: {
                (rkoperation: RKObjectRequestOperation!, error: NSError!) -> Void in
                print("Load filed with error: @", error!)
                self.performSelectorOnMainThread(#selector(AppDelegate.showFetchError), withObject: nil, waitUntilDone: true)
        })
        handler(listDoctors)
    }