Please, help needed swift 3 code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please, help needed swift 3 code

I am building my TODo list app following the instruction in Sololearn but the problem it is not updated with Xcode 8 .I am stuck in adding new item code everything is ok except when I write items.append(item!) a message appears ( Cannot convert value of type 'item' to expected argument type 'itemTableController.Item' the code is : @IBAction func unwindToList(sender: UIStoryboardSegue) { let srcViewCon = sender.source as? ViewController let item = srcViewCon?.item if (srcViewCon != nil && item?.name != "") { //Add a new item let newIndexPath = NSIndexPath(row: items.count, section: 0) items.append(item!) tableView.insertRows(at: [newIndexPath as IndexPath], with: UITableViewRowAnimation.bottom) } }

9th Aug 2017, 9:54 AM
Sundas Alduraihem
Sundas Alduraihem - avatar
11 Answers
+ 4
'Xcode' is an Apple IDE (Integrated Development Environment) that can handle a bunch of languages (C, C++, Objective-C, Objective-C++, Java, AppleScript, Python, Ruby, ResEdit (Rez), and Swift) to develop apps for macOS, iOS, watchOS and tvOS... There's no specific sololearn course for Xcode, so you should first specify the language used ^^ Anyway, Xcode 8 seems to support for Swift 3 (I guess that you use Swift, as Apple development prefered language), and Swift 4 seems planified for Xcode 9 (according to wikipedia: https://en.m.wikipedia.org/wiki/Xcode )... However, I doesn't really know Apple development and Swift versions specifities, no more than which version is teached in Sololearn, but this should have importance ;P
9th Aug 2017, 6:20 AM
visph
visph - avatar
+ 4
Sorry, I'm not Swift skilled at all, so I cannot help you further: I hope someone else could answer you ;)
9th Aug 2017, 8:28 AM
visph
visph - avatar
+ 2
Yes ! It finally works I wrote it twice item.swift and the ItemTableViewController . Thanks a lot. You are a great help.Thanks again.
10th Aug 2017, 8:08 AM
Sundas Alduraihem
Sundas Alduraihem - avatar
+ 1
Thanks a lot for your quick response I am learning swift in sololearn and i am in level 4 trying to build my ToDo Item .Sololearn I guess using swift 2 and Xcode 8 use swift 3 .I am using I am stuck in this code@IBAction func unwindToList(sender: UIStoryboardSegue) { let srcViewCon = sender.source as? ViewController let item = srcViewCon?.item if (srcViewCon != nil && item?.name != "") { //Add a new item let newIndexPath = NSIndexPath(row: items.count, section: 0) items.append(item!) tableView.insertRows(at: [newIndexPath as IndexPath], with: UITableViewRowAnimation.bottom) } } everything is ok in the code except when I write: items.append(item!) it gives an error: ( Cannot convert value of type 'item' to expected argument type 'itemTableController.Item' thanks again
9th Aug 2017, 8:09 AM
Sundas Alduraihem
Sundas Alduraihem - avatar
+ 1
It looks like you have a type mismatch. items seems to take itemTableController.item and it looks like you're trying to pass in a ViewController.item. If the sender is also an itemTableController change the cast to such instead of ViewController. Without seeing the rest of the code that is my best guess.
9th Aug 2017, 10:24 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thanks a lot for your response the whole code is from the swift course Adding Items to the Do list: Store New Items The next step in creating the unwind segue is to add an action method to the destination view controller. In this method, you'll write the logic to add the new item (that's passed from ViewController, the source view controller) to the items list data and add a new row to the table view in the items list scene. Open ItemTableViewController.swift and add the following method: @IBAction func unwindToList(sender: UIStoryboardSegue) { let srcViewCon = sender.sourceViewController as? ViewController let item = srcViewCon?.item if (srcViewCon != nil && item?.name != "") { // Add a new item let newIndexPath = NSIndexPath(forRow: items.count, inSection: 0) items.append(item!) tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom) } } This code uses the optional type cast operator (as?) to try to downcast the segue's source view controller to type ViewController. This adds the new item to the existing list of items in the data model. It also animates the addition of a new row to the table view for the cell containing information about the new item as I explained all the code is ok except one line items.append(item!) it gives an error: ( Cannot convert value of type 'item' to expected argument type 'itemTableController.Item' thanks again
9th Aug 2017, 12:10 PM
Sundas Alduraihem
Sundas Alduraihem - avatar
+ 1
Ok went through the tutorial and built the app to that point and here is my code for this portion: @IBAction func unwindToList(sender: UIStoryboardSegue) { let srcViewController = sender.source as? ViewController let item = srcViewController?.item let newIndexPath = IndexPath(row: items.count, section: 0) items.append(item!) tableView.insertRows(at: [newIndexPath], with: .bottom) } Make sure that this code is in your ItemTableViewController.swift file and that you linked the navigation save button back to the exit node and selected unwindToListWithSender. If you have everything linked up correctly and all your other code is correct it should work ok up to this point. Otherwise I would suggest you go through the tutorial again and read over carefully making sure your code is all swift 3 compatible. If you need any more help just ask.
9th Aug 2017, 7:54 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
I really appreciate your great help and quick response .I rewrite the code but still, the same message is given Cannot convert value of type 'Item' to expected argument type 'ItemTableViewController.Item).I am coding with Xcode 8 which is compatible with Swift 3. I will go through the tutorial again as you advised hope I find the debugging. Thanks again for your help.
10th Aug 2017, 6:34 AM
Sundas Alduraihem
Sundas Alduraihem - avatar
+ 1
The class Item should be in its own file Item.swift outside of the ItemTableViewController class and not as a subclass. Other than that this code looks ok.
10th Aug 2017, 6:57 AM
ChaoticDawg
ChaoticDawg - avatar
0
This is my (ItemTableViewController.swift) : import UIKit class ItemTableViewController: UITableViewController { var items = [Item]() func loadSampleItems() { items += [Item(name:"item1"), Item(name:"item2"), Item(name:"item3")] } class Item { var name: String init(name: String) { self.name = name } } override func viewDidLoad() { super.viewDidLoad() loadSampleItems() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentifier = "ItemTableViewCell" let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as!ItemTableViewCell let item = items[indexPath.row] cell.nameLabel.text = item.name return cell
10th Aug 2017, 6:44 AM
Sundas Alduraihem
Sundas Alduraihem - avatar
0
I'm getting errors in ItemTableViewController.swift file. Can anyone upload the complete code for this swift file?
25th May 2019, 1:25 AM
Randil Tennakoon