Swift 2 downcasting - swift2

My intent is to pass returningClass as an argument to this piece of code. I am receiving the compiler error -- 'returningClass' not a type. When I substitute MedicationsViewController for returningClass after as?, it works fine.
What am I doing wrong?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == screenParameters.addSequeDoneName {
let returningClass: AnyObject.Type = MedicationsViewController.self
if let destinationVController = segue.destinationViewController as? returningClass {
}}
}

Related

Cannot convert value of type 'Int' to expected argument type 'NSIndexSet'..?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showView"
{
var upcoming: NewViewController = (segue.destinationViewController as? NewViewController)!
let indexPath = self.tableView.indexPathForSelectedRow!
let titleString = self.objects.objectsAtIndexes(indexPath.row) as? String
upcoming.titleString = titleString
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
I am getting the following error
Cannot convert value of type 'Int' to expected argument type 'NSIndexSet'..
in the line where I am trying to convert the value into string:
let titleString = self.objects.objectsAtIndexes(indexPath.row) as? String
let titleString = self.objects.objectsAtIndexes(indexPath.row) as? String
You are asking for objects at indexes , but what you really want is object at index
you could just use self.objects[indexPath.row]

Pass data with segue form UITabBarItem not working

I want to pass data from one UIView to another UIView in order to go to the web site which will be different according to UITabBarItem that a user chooses. How can I do it?
func pathButton(dcPathButton: DCPathButton!, clickItemButtonAtIndex itemButtonIndex: UInt) {
switch itemButtonIndex {
case 0:
// working but can’t send data with it
self.performSegueWithIdentifier("goto", sender: self)
// not working as function is not trigger
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "goto" {
let vc = segue.destinationViewController as! WebMainViewController
vc.goURL = "http://google.com“
dcPathButton.delegate = self
println(“go to http//google.com”)
}
case 1:
println(“go to http://apple.com”)
case 2:
println(“go to http://imbd.com”)
case 3:
println(“go to http://facebook.com”)
default:
println(“go to http://cnn.com”)
}
Have you tried
func pathButton(dcPathButton: DCPathButton!, clickItemButtonAtIndex itemButtonIndex: UInt) {
switch itemButtonIndex {
case 0:
self.performSegueWithIdentifier("goto", sender: self)
// plus other cases
}
and then in a different section of your class you implement:
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "goto" {
let vc = segue.destinationViewController as! WebMainViewController
vc.goURL = "http://google.com“
}
Obviously make sure that your segue is hooked up and has "goto" set as an identifier.
Perhaps you could have some logic in prepareForSegue that sets vc.goURL instead of the switch statement that you currently have? For example, the user selects a particular value, you save that value to a variable and then when they click on a button to start the segue you check the value of that variable and set vc.goURL accordingly?

What does the underscore mean in this case?

my code before the migation to Swift 2.0:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "RhymeFavoriten") {
// pass data to next view
let dest = segue.destinationViewController as! FavoritenViewController
let source = segue.sourceViewController as! RhymeViewController // !!!!!!
dest.favoritenType = 1
dest.delegate = self
}
}
the migration told me to change it to
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "RhymeFavoriten") {
// pass data to next view
let dest = segue.destinationViewController as! FavoritenViewController
_ = segue.sourceViewController as! RhymeViewController // !!!!!!!!!!
dest.favoritenType = 1
dest.delegate = self
}
or
func textSelected(selectedText:String, selectedType:Int) {
var fullTextArr = text.componentsSeparatedByString("\n")
var myArray = [String]() // !!!!!!
to
func textSelected(selectedText:String, selectedType:Int) {
var fullTextArr = text.componentsSeparatedByString("\n")
_ = [String]() // !!!!!!!!!
I can´t see, what is _ = standing for :-(
_ is a placeholder. It means that the values assigned to _ are ignored.
Xcode's migration tool made this changes because it has detected that you didn't use source or myArray anywhere, thus replaced these variables by the placeholder.
Now instead of being assigned to a variable, the returning result of segue.sourceViewController as! RhymeViewController and the returning result of [String]() are ignored.
The returning result is ignored but the expression is still evaluated at runtime: if it has side effects, these effects will occur.
So if you actually don't need these instructions you should get rid of them entirely.

Why am I receiving these errors when trying to pass a variable on a segue in swift?

I am trying to build upon answer which I was given here. What I am trying to is very simple - I want a text field which you can enter text into. You press the go button and it takes you to a new view and replaces the text on a label on that page with whatever the user entered in the box. The is the code I am using on the first page.
import UIKit
class ViewController: UIViewController {
#IBOutlet var entry: UITextField!
let dictionary = entry.text // Line 7 ERROR
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "viewTwo"
{
if let destinationVC = segue.destinationViewController as? viewTwo{
destinationVC.dictionary = self.dictionary // Line 24 ERROR
}
}
}
#IBAction func goToViewTwo(sender: AnyObject) {
performSegueWithIdentifier("viewTwo", sender: self)
}
}
I am only including the code from the first view because i know the code from the second view is working.
I didn't encounter an error until I tried to use the text field - before when I just had a pre-choses text to transfer over it worked. Before, instead of having let dictionary = entry.text I had let dictionary = "foo" and it worked.
So my question is exactly the same thing but have a text field instead of pre-chosen text - what I really want to know is why my code didn't work before.
The errors I got were on line 7 (I have labeled the lines above which had the errors) - 'ViewController.Type' does not have member names 'entry' and there was also an error on line 24 but I suspect this is related to this error and will be fixed if this error is also fixed. Just incase though, the error on line 24 was: 'ViewController.Type' does not have member names 'dictionary'
Thank you.
You should set the dictionary to var dictionary = "" in the declaration. You use var instead of let here, so that you can change the value of the dictionary later.
Then inside your #IBAction func goToViewTwo(sender: AnyObject){} method, you set the self.dictionary = entry.text
#IBAction func goToViewTwo(sender: AnyObject) {
dictionary = entry.text
performSegueWithIdentifier("viewTwo", sender: self)
}
Alternatively, you can just do the following inside prepareForSegue() method.
This way, you dont need to declare a dictionary to hold the text value of your UITextField, you can just pass the text value from your entry to the second view controller's dictionary variable.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "viewTwo"
{
if let destinationVC = segue.destinationViewController as? viewTwo{
destinationVC.dictionary = self.entry.text
}
}
}
A dictionary is not constant, so declare it as lazy var, not let:
lazy var dictionary: String {
return entry.text
}()

Swift PrepareForSegue EXC_BREAKPOINT

I have a runtime error: Thread 1: EXC_BreakPoint(cod=EXC_I1386_BPT,subcode=0x0)
I did not set any breakpoint inside Xcode. From the debugger,error is due to PrepareForSegue that stops a thread "swift_dynamicCastClassUnconditional"
BarTableViewController1 Class
#IBAction func solve_PressedBar(sender: AnyObject) {
self.performSegueWithIdentifier("SolveBar", sender: sender)
}
override func prepareForSegue ( segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "SolveBar") {
var svcBar = segue.destinationViewController as BarTableViewController2
for items in textField1 {
length.append((items as NSString).floatValue)
}
for items in textField2 {
youngMod.append((items as NSString).floatValue)
}
for items in textField3 {
diameter.append((items as NSString).floatValue)
}
for items in textField4 {
forceBarWall.append((items as NSString).floatValue)
}
self.funcForceBarWall()
println("variables are from TableViewController2")
println(self.length)
println(self.youngMod)
println(self.forceBarWall)
println(self.diameter)
svcBar.length2 = self.length
svcBar.youngMod2 = self.youngMod
svcBar.diamter2 = self.diameter
svcBar.forceBarWall2 = self.forceBarWall
println("testing from bar viewcontroller1")
}
}
}
I'd look here first:
var svcBar = segue.destinationViewController as BarTableViewController2
Check your storyboard and make sure that the segue does connect to a BarTableViewController2 item. Sometimes when dragging, I connect the segue to something else accidentally.

Resources