خرید بک لینک

Vote count: 0

I'm trying to add Task objects to an array called tasks, and then filtered them into another array called filteredTasks by category.

    var tasks = [Task]()
var filteredTasks = [Task]()    

override func viewDidLoad() {
    super.viewDidLoad()
    print(categoryPass)
    loadTasks()
    filterTasks(categoryPass!)
    if filteredTasks.isEmpty {
        filterTasks(categoryPass!)
    }
    print(filteredTasks)
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.locationManager.delegate = self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    tableView.delegate = self
    tableView.dataSource = self

    // Do any additional setup after loading the view, typically from a nib.
}


func loadTasks() {
    var taskTitle: String?
    var id: String?
    var distance: Double?
    var category: String?

    var locationDict: [String:CLLocation] = [:]
    var refHandle = self.ref.child("tasks").observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
        self.tasksDict = snapshot.value as? NSDictionary
        for i in 0 ..< self.tasksDict!.count {
            let taskId = self.tasksDict!.allKeys[i] as! String
            print ("1")
            id = taskId
           print (taskId)
            let task = self.tasksDict!.objectForKey(taskId) as! NSDictionary
            print ("2")
            let lat = task.objectForKey("latitude") as! String
            let long = task.objectForKey("longitude") as! String
            let latNum = Double(lat)! as CLLocationDegrees
            let longNum = Double(long)! as CLLocationDegrees
            taskTitle = task.objectForKey("title") as? String
            category = task.objectForKey("category") as? String
            print("3")
           // print (taskTitle)
            //print (category)

            let pointLocation = CLLocation(latitude: latNum, longitude: longNum)
            locationDict[taskId] = pointLocation
            if (self.locationCurrent == nil) {
                self.locationCurrent = self.locationManager.location!
            }
            print("4")
            distance = round(self.locationCurrent!.distanceFromLocation(pointLocation))
            var taskAddition = Task(title: taskTitle, id: id, distance: distance, category: category)
            print (taskAddition)
            self.tasks += [taskAddition]

        }
    })
}

func filterTasks(searchText: String, scope: String = "All") {
    print("filter")
    for i in 0 ..< tasks.count {
        let taskId = tasksDict!.allKeys[i] as! String
        //  print (taskId)
        let task = tasksDict!.objectForKey(taskId) as! NSDictionary
        let taskCategory = task.objectForKey("category") as! String
        if (taskCategory.lowercaseString.containsString(searchText.lowercaseString)) {
            filteredTasks.append(task[i] as! Task)
        }

    }

    self.tableView.reloadData()
}

The numbers 1,2,3, and 4 all print out in the console, but if I try to print out tasks or filteredTasks, I get [] and a count of 0. Is there a reason why tasks aren't being added to the arrays?

asked 31 secs ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 10 تير 1395 ساعت: 6:43

صفحه بندی