Mickey Mickey - 5 months ago 44
Swift Question

Swift Merge two NSMutableArray's

I'm trying to merge two NSMutableArray's, my main goal is to merge them so that i can mix two tables from the Sqlite into one Array than load it into the UITable.
Here is my code:

class WorkoutsMenuView: UIViewController, UITableViewDataSource,UITableViewDelegate {

var marrStudentData : NSMutableArray!
var marrStudentData_2 :NSMutableArray!
var mergetWorkoutData :NSMutableArray!

var selectedRow :Int!

@IBOutlet weak var tbStudentData: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

override func viewWillAppear(animated: Bool) {
self.getStudentData()
navigationItem.title = "Home Workouts Pro"
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

//MARK: Other methods

func getStudentData()
{
marrStudentData = NSMutableArray()
marrStudentData_2 = NSMutableArray()
mergetWorkoutData = NSMutableArray()

marrStudentData = ModelManager.getInstance().getAllStockWorkouts()
marrStudentData_2 = ModelManager.getInstance().getAllCustomWorkouts()

//var asd = marrStudentData.arrayByAddingObjectsFromArray(marrStudentData_2 as [AnyObject])

mergetWorkoutData = [marrStudentData, marrStudentData_2]

tbStudentData.reloadData()
}

//MARK: UITableView delegate methods

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mergetWorkoutData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:WorkoutMenuCell = tableView.dequeueReusableCellWithIdentifier("cellWorkoutMenu") as! WorkoutMenuCell
****let student:WorkoutInfo = mergetWorkoutData.objectAtIndex(indexPath.row) as! WorkoutInfo*****

cell.workoutCell_Title.text = student.workout_name
cell.workoutBenefit_1.text = student.workout_benefit_1
cell.workoutBenefit_2.text = student.workout_benefit_2
cell.workoutBenefit_3.text = student.workout_benefit_3
cell.workoutRequirments.text = student.workout_requiremnets
cell.workoutTime.text = student.workout_time

return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//selectedRow = indexPath.row
//self.performSegueWithIdentifier("editSegue", sender: self)
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "editSegue")
{
if let indexPath = tbStudentData.indexPathForSelectedRow{
let viewController : WorkoutView = segue.destinationViewController as! WorkoutView
viewController.getId = Int(indexPath.row)
viewController.workoutInfoData = mergetWorkoutData.objectAtIndex(indexPath.row) as! WorkoutInfo
navigationItem.title = ""
}
}
}


}


The * part * is a the crashing point.I have tried one way to merge
them
mergetWorkoutData = [marrStudentData, marrStudentData_2]

but it's crashing.Here is the crash log:


Could not cast value of type '__NSArrayM' (0x1a13ba3f0) to
'Home_Workout_Pro.WorkoutInfo' (0x1002b5f78). (lldb)

Answer

By doing [marrStudentData, marrStudentData_2], you're nesting arrays within a new array. Like this:

let a = [1, 2, 3] //has type [Int]
let b = [4, 5, 6] //has type [Int]

let c = [a, b] //has type [[Int]]

print(c) //prints [[1,2,3], [4, 5, 6]], not [1, 2, 3, 4, 5, 6]

To achieve what you want, you need to do:

mergetWorkoutData = NSMutableArray(array: marrStudentData)
mergetWorkoutData.addObjectsFromArray(marrStudentData_2)
Comments