如何在swift NSMutableArray

时间:2015-10-23 06:37:17

标签: ios xcode swift nsmutablearray nsmutabledictionary

我想得到所有医生的名字,其中部门名称是"内科。\ n"来自{{1>} Swift语言

NSMutableArray

我可以使用 ( { DepartmentName = "Internal Medicine.\n "; DoctorName = "Dr. A. K. Kundu\n "; }, { DepartmentName = "Internal Medicine.\n "; DoctorName = "Dr. Arvind K. Minz\n "; }, { DepartmentName = "Internal Medicine.\n "; DoctorName = "Dr. Rajeev Choudhary\n "; }, { DepartmentName = "Obstetrics And Gynaecology.\n "; DoctorName = "Dr. Gouri Jain\n "; }, { DepartmentName = "Obstetrics And Gynaecology.\n "; DoctorName = "Dr. Poonam Kataria\n "; }, { DepartmentName = "Obstetrics And Gynaecology.\n "; DoctorName = "Dr. Ila Jha\n "; }, { DepartmentName = "Internal Medicine.\n "; DoctorName = "Dr. Ram Niwas Gupta\n "; }, { DepartmentName = "Gasteroenterology.\n "; DoctorName = "Dr. Sanjay Kumar\n "; }, { DepartmentName = "Neurology.\n "; DoctorName = "Dr. Sameer Gupta\n "; }, { DepartmentName = "Physiotherapy And Rehabilitation.\n "; DoctorName = "Dr. Kapil Chauhan\n "; }, { DepartmentName = "X­Ray.\n "; DoctorName = "Dr. Gagan Sharma\n "; } ) 轻松获取所有部门和医生的姓名(例如posts.valueForKey(" DoctorName")或valueforkey("部门名称&#34 ;))和现在我想得到所有医生名称,从部门名单中选择部门名称

我的完整代码:

posts.valueForKey

2 个答案:

答案 0 :(得分:2)

我建议你创建一个名为Doctor

的新对象
class Doctor: NSObject {

    var doctorName = ""
    var departmentName = ""
}

这样您就可以轻松地将数据解析为新的Doctor并将其存储为数组。过滤,查找或执行任何操作都会更容易。

希望这有帮助。

答案 1 :(得分:0)

我通过简单的步骤解决了这个问题:

这里的部门名称和医生名称在同一索引上,所以我所做的就是 只需从DepartmentArrray获取indexOfObject(),然后从DoctorArray获取objectAtIndex()。

这是@IBAction函数中的代码SelectDoctorButton(sender:AnyObject):

var indexOfObject = 0
            for var index = 0 ; index < self.DepartmentArray.count ; index++ {
                if self.PickValue == self.DepartmentArray[index] as NSString{
                    indexOfObject = index

                    println(" pickValue is at index : \(indexOfObject) ")
                    println(self.DoctorArray.objectAtIndex(index))


                    self.DoctorArrayAsDepartment.addObject(self.DoctorArray.objectAtIndex(index))