Reputation: 5
I have a notifications class with the following function (ed of post) that returns an array, I call the function (getListOfNotifications) below and get an array of data from my main ViewController. In my main controller I define the array like such:
var arr = []
arr = getListOfNotifications(“\(userIdInt)”)
Then in my prepareForSegue I pass the array to a tableView
let secondVC = segue.destinationViewController as NotificationsTableViewController
secondVC.notificationsArray = arr
Then in the Tableview I have the following, which was populated from above,
var notificationsArray = []
then in my tableview when I try to do the following delete, I can’t use the removeObjectAtIndex on the array, but I also can’t define the array as NSMutableArray. What can I do to remove the item from the array? I tried redefining it any which way possible and I can;t get anything to work.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
notificationsArray.removeObjectAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
This is the function in my Notifications class that I populate from a web service:
func getListOfNotifications(item:String)->Array<Notifications> {
var notificationArray:[Notifications] = []
println("==== Notifications ====")
var url=NSURL(string:”http://www.mywebsite.com/NotificationListJSON.php?id="+item)
var data=NSData(contentsOfURL:url!)
if let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as? NSDictionary {
if let feed = json["notifications"] as? NSArray {
for entry in feed {
var notifications = Notifications()
notifications.desc = entry["description"] as String
notifications.name = entry["name"] as String
notifications.iid = entry["iid"] as String
notifications.distance = entry["distance"] as String
notificationArray.append(notifications)
}
}
}
return notificationArray
}
Upvotes: 0
Views: 1350
Reputation: 3352
If you know what the array is going to be, why are you declaring it as an NSArray in the first place. Why not:
var notificationsArray = [Notifications]()
Then you'll have a swift array of Notifications, which is mutable and you should be able to remove.
Upvotes: 1
Reputation: 80265
removeObjectAtIndex
is a method of NSMutableArray
, but you are working with a Swift array (declared as var
, so also mutable).
Use the standard remove method described in the Swift Standard Library References.
notificationsArray.removeAtIndex[indexPath.row]
Upvotes: 0