Does Not Have Member named indexPathForSelecedRow().row - iphone

I am new in swift I am trying to send some information on click on cell of Table view using segue,but when i try to compile i get this error..
I am using Xcode 6.1 and SDK 8.1
override func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject?) {
if segue.identifier == "update"{
let selectedItems : NSManagedObject = arr[self.tableView.indexPathForSelectedRow().row] as NSManagedObject
let IVC : ViewController = segue.destinationViewController as ViewController
IVC.itemVar = selectedItems.valueForKey("item") as String
IVC.qtyVar = selectedItems.valueForKey("qty") as String
IVC.discVar = selectedItems.valueForKey("disc") as String
IVC.selectedItem = selectedItemse
}
}

You have to unwrap the returned indexPath
self.tableView.indexPathForSelectedRow()!.row
but do this only if you are sure, returned indexPath will always have value(it wont be nil)
Othrewise make the unwrapping this way:
if let indexPath = self.tableView.indexPathForSelectedRow(){
indexPath.row
}

Related

Cannot compare NSString with Swift's static let string

I have a ViewController subclass which segues off into multiple different other views (using a single ViewController type). I have put identifiers on the segues in the Storyboard and I want to compare them at runtime so I can pass data to them, like so:
class MyListViewController : UITableViewController {
static let segueNameFoo : String = "segueFoo"
static let segueNameBar : String = "segueBar"
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue( segue, sender: sender )
if segue.identifier == segueNameFoo {
let vc : MyDetailViewController = segue.destinationViewController as! MyDetailViewController
vc.someData = "foo"
} else if segue.identifier == segueNameBar {
let vc : MyDetailViewController = segue.destinationViewController as! MyDetailViewController
vc.someData = "bar"
}
}
}
Problem is, Xcode 7.3 gives me these errors:
Static member segueNameFoo cannot be used on instance of type MyListViewController
Static member segueNameBar cannot be used on instance of type MyListViewController
When I change it to just let segueNameFoo : String = "segueFoo" it builds and runs fine.
I don't understand why it's complaining - segue.identifier is an NSString* and Swift supports == comparisons between them. My use of static let is so that segueNameFoo isn't allocated for every instance of MyListViewController. What's up?
Because if you use like this it means segueNameFoo is instance member
segue.identifier == segueNameFoo
is equal to
segue.identifier == self.segueNameFoo
So you should use MyListViewController.segueNameFoo. so it will be look as class member(Similarly to static)
The problem is unrelated to String vs NSString.
You have defined type properties, these are referenced as MyListViewController.segueNameFoo etc.
in swift 2 you can use this:
let str = "string"
let nsstr:NSString = "string"
if nsstr.containsString(str){
print(true)
}else{
print(false)
}

Thread 1: Signal SIGABRT with segue in swift

I have several tableview. I am trying to make a simple application and after adding more view when I get the third tableView this error "Thread 1 Signal SIGABRT" and the app wont open in iOS Simulator. The error points to this line of code:
let VC :DetailCityTableViewController = segue.destinationViewController as! DetailCityTableViewController
and display error :
Could not cast value of type 'UITableViewController' (0x102af47f8) to 'balen.DetailCityTableViewController' (0x1013d7560).
(lldb)
Full Code :
tableview2 pass to tableView3
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let path : NSIndexPath = self.tableView.indexPathForSelectedRow!
if segue.identifier == "DetailCitySegue" {
let VC :DetailCityTableViewController = segue.destinationViewController as! DetailCityTableViewController
VC.urlDetailCity = urlDetail!// pass url city
VC.cityIdSelectet = cityId[path.row] //pas CountryID selected
print(cityId[path.row])
}
}
I am , I think the problem is segue .What do you think?
No, the problem is '
Could not cast value of type 'UITableViewController' (0x102af47f8) to
'balen.DetailCityTableViewController''
You didn't change the class type of the view controller to DetailCityTableViewController in interface builder or the segue has a different destination than you think.

Query for data to add data to prepareForSegue function

I am attempting to query for data from a Parse table to add it to a prepareForSegue function. But once I go into the newViewController the label is blank. Here's my line of code.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "marathonDetail"){
var upcoming: marathonDetailViewController = segue.destinationViewController as! marathonDetailViewController
let indexPath = self.marathonsTableView.indexPathForSelectedRow!
let currentCell = marathonsTableView.cellForRowAtIndexPath(indexPath) as! marathonTableViewCell
let marathonEvents = currentCell.marathonName.text
upcoming.nameMarathon = marathonEvents
self.marathonsTableView.deselectRowAtIndexPath(indexPath, animated: true)
var query = PFQuery(className: "marathons")
query.whereKey("marathonName", equalTo: marathonEvents!)
query.findObjectsInBackgroundWithBlock{
(marathonPickeds: [PFObject]?, error: NSError?) -> Void in
if (error == nil){
if let marathonPicked = marathonPickeds as? [PFObject]?{
for marathonPicked in marathonPickeds!{
var selectedDescription = marathonPicked.description
upcoming.marathonDescription = selectedDescription
print(selectedDescription)
}
}
}else {
print(error)
}
}
}
}
The marathonsEvents= currentCell.marathonName.text works well but the marathonDescription is blank.
Any advice? I am using Parse as my backend XCODE 7, and swift
You're performing a network call on a background thread so by the time its finished you've already completed the segue. What you probably want to do is:
Pull that query logic out into a separate class, get it out of your view controllers.
In this instance perform the request in the view controller that is being pushed to. You can start it in viewWillAppear and refresh your view when its finished. It looks like it has all the information it needs to perform the request using just the marathonEvents.

Queue of functions - Swift

I have a table in which each cell there is the profile name of a user. I want that when the profile name is clicked, it will go to the profile page of the user. I created a delegate from the cell for this. But the problem is, the variable "goToProfileVar" is empty at the starting point. Then I assign a value to it with the "goToCell103" function. Then the assigned value should be used in the "prepareforsegue" to pass the username data to the profile page. The problem is that, prepareforseque functions works first and the "goToProfileVar" becomes empty and passed so, then the variable is assigned from the cell.username.text.
How can I queue these functions?
var goToProfileVar = String()
var goToProfileVar2 = String()
func goToCell103(cell: mainCell)
{
var goToCell103:mainCell? = cell as mainCell
var indexPath: NSIndexPath = self.resultsTable.indexPathForCell(goToCell103!)!
goToProfileVar = cell.usernameLbl.text!
goToProfileVar2 = cell.objectid.text!
println("\(goToProfileVar) first.")
println("\(goToProfileVar2) first.")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "goToProfile8") {
var svc = segue.destinationViewController as! generalProfileLast;
svc.dataPassed = goToProfileVar}
else if (segue.identifier == "goToLikers1") {
var svc = segue.destinationViewController as! likers;
svc.dataPassed = goToProfileVar2}
else if (segue.identifier == "goToComments1") {
var svc = segue.destinationViewController as! enterCommentVC;
svc.dataPassed = goToProfileVar2}
else if (segue.identifier == "goToComments2") {
var svc = segue.destinationViewController as! commenters;
svc.dataPassed = goToProfileVar2}
println("\(goToProfileVar) second.")
println("\(goToProfileVar2) second.")
}
It is in general bad approach to use current view state as a model. Likely your UI populated from somewhere, so use the same source of data to initialize your controllers in prepareForSegue.
If you do not want to change a lot of your architecture and still prefer your way you can use goToCell103 right in prepareForSegue with a sender as an input parameter. In a cell segues sender would be a taped cell.

error "secondViewController does not have a member named "mastername"

I am using a sample from IOS 8 App Development Essentials. I added a variable to my second controller but keep getting this error.
Second controller code:
Class SocondDetailController: UIViewController{
var mastername: String?
...
}
First controller code:
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?)
{
if segue.identifier == "ShowDetails"
{        
let detailViewController = segue.destinationViewController as SocondDetailController
let myIndexPath = self.tableView.indexPathForSelectedRow()
let row = myIndexPath?.row
SocondDetailController.mastername = tableData[row!]
}
}
I am new to Swift and IOS development. Just starting at age 71.
I have been using VB.Net for a long time.
Please help.
Thanks.
This row:
SocondDetailController.mastername = tableData[row!]
should be:
detailViewController.mastername = tableData[row!]
mastername is an instance property, and as such you have to access to it through an instance of SocondDetailController, and not the SocondDetailController type itself.
Also, although probably not needed due to the logic in your view controller, I'd avoid using the forced unwrapping operator !, preferring a safer optional binding:
let row = myIndexPath?.row
if let row = row {
detailViewController.mastername = tableData[row]
}
or, more concisely:
if let row = myIndexPath?.row {
detailViewController.mastername = tableData[row]
}