简体   繁体   中英

Create list object Swift

:I have problem. I create list doctor show firstname and lastname. How objects to the listdoctor

 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)
   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)
    }
}

You should create String array.

    //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)")

I correct my code now I add lastname arry listDoctors should be return name and lastname

  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)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM