Swift 2.0 / iOS Unwrapping Error - xcode

I am new to Swift and am trying to make a basic application that displays a string based on entered text.
I understand that with iOS 9 and Swift 2.0, there are several new changes. I am watching a tutorial from iOS 8 and Swift 1.2 and noticed that the code I am trying to run is not compiling, stating that I need an "!" to unwrap a variable. What is the best way to learn about unwrapping and when it is necessary? A tutorial or video would be extremely helpful:
import UIKit
class ViewController: UIViewController {
#IBOutlet var resultLabel: UILabel!
#IBOutlet var age: UITextField!
#IBOutlet var convertedAge: UILabel!
#IBAction func checkAge(sender: AnyObject) {
var enteredAge = Int(age.text)
//exclamation mark "unwraps" a variable
//a way for the programmer to ensure that a value will be an int
var catYears = enteredAge! * 7
resultLabel.text = "Your cat is \(catYears)"
print(age.text)
}
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.
}
}

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html
I am suggesting apple's own source for this purpose. If you can pay for the course, also Lynda.com has a nice Swift essentials course

Related

Programming my first app in Swift - "error: attach by pid '2423' failed -- unable to attach"

When I go to run my first program in Swift (I have only been studying the language for about a month - my first programming language), I receive the error: "error: attach by pid '2423' failed -- unable to attach". Here is my code if anyone can point out the problem for me. This is also my first question in Stackoverflow, so please let me know if this is an appropriate question. Thanks!
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var nameText: UITextField!
#IBOutlet weak var nameDisplayed: UILabel!
#IBAction func submitButton(_ sender: AnyObject) {
nameText.text = nameDisplayed.text
}
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.
}
}

UISlider error XCode 7 Swift 2

I'm trying to build a very simple app just to see how the UISlider works. I've seen several tutorials and followed them to the letter but nothing seems to work.
I keep getting this error exactly when I try to move the Slider in the simulator, after I've successfully built
This is my whole code. It's really simple but I can't understand why the Slider won't work.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var noteBottomLabel: UILabel!
#IBOutlet weak var sliderNoteBottom: UISlider!
#IBAction func changedSliderNoteBottom(sender: AnyObject) {
var noteBottomValue = Int(sliderNoteBottom.value)
noteBottomLabel.text = "\(noteBottomValue)"
}
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.
}
Can you see any problems with this? I'm probably noobing out, but I've looked everywhere and can't find an explanation
import UIKit
import CoreFoundation
class ViewController2 : UIViewController {
override func viewDidLoad() {
super.viewDidLoad();
}
#IBOutlet weak var slider: UISlider!
#IBAction func slidervaluechanged(sender: UISlider) {
print(Int(slider.value))
}
}
I have tried your code just made the print of slider.value rather than putting it on label .
But I have used Xcode 7.3 , I don't think this should be problem with Xcode .Use the sender as UISlider when making an #IBAction that looks better . Brings up the value of 0 at bottom and 1 at top.

Xcode Mac application (Swift) problems

I am working on a Mac application using Swift.
And I got a lot of problems there cause I thought it would be the same as I did it in the iOS application
I am really sorry about this long post :(
The first problem:
The error: "Method does not override any method from its superclass"
The code:
import Cocoa
class CreateAccountViewController: NSViewController {
#IBOutlet weak var emailTextField: NSTextField!
#IBOutlet weak var passwordTextField: NSSecureTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
The next one:
Error:Value of type 'CreateAccountViewController' has no member 'dismissViewControllerAnimated'
Code:
self.dismissViewControllerAnimated(true, completion: nil)
The last one:
The error: Use of unresolved identifier 'UIAlertController'
The code:
let alert = UIAlertController(title: "Error", message: "Enter Email and Password", preferredStyle: .Alert)
I am so sorry but this is my first time I wrote a Mac application.
UIAlertController is part of UIKit Library which is only available on iOS.
You'll need to use NSAlert to pop an alert in a window on Mac OS X. You also need to do an import AppKit at the top of your Swift file.

Detection of WebView load finish

I am creating a simple app with a WebView basing on this tutorial.
I would like to display a progress indicator while loading the page, but the methods didStartProvisionalLoadForFrame and didFinishLoadForFrame are never called. What am I doing wrong?
ViewController.swift
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WebView!
#IBOutlet weak var webViewProgressIndicator: NSProgressIndicator!
let messengerUrl = "https://www.messenger.com/"
override func viewDidLoad() {
super.viewDidLoad()
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: messengerUrl)!))
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func webView(sender: WebView!, didStartProvisionalLoadForFrame frame: WebFrame!)
{
self.webViewProgressIndicator.startAnimation(self)
}
func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!)
{
self.webViewProgressIndicator.stopAnimation(self)
}
}
I am using Xcode 7 Beta and OS X 10.11 Beta.
Rich's answer works on Xcode 7.0.1 ElCaptain Swift2
BUT I had to connect it in the connections inspector as Swift2 did not accept
self.view.frameLoadDelegate = self
as it gave the following error
Cannot assign a value of type 'ViewController' to a value of type
'WebFrameLoadDelegate!'
So as long as you connect it up as follows, all works great.
The tutorial missed out something, Need to add:
self.webView.frameLoadDelegate = self
(can be done in connections inspector)
then call the load..
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: messengerUrl)!))
Also add the WebFrameLoadDelegate interface to your ViewController.. (thanks tjv)
class ViewController: NSViewController, WebFrameLoadDelegate{ .....
You need to assign the delegate of your UIWebview ie: webview.delegate = self

Setting Label text in swift

My first question here. I'm making the jump from Delphi and Pascal to Xcode and Swift and feeling totally overwhelmed by the change.
I'm simply attempting to change the text of a label when a button is clicked. Here is my code from ViewController.swift
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var Label1: NSTextField!
#IBOutlet weak var ChangeText: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func ButtonPressed(sender: AnyObject) {
Label1.stringValue = "Hello World"
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
When I run the application as soon as I click the button the application hangs.
I've also tried exchanging stringValue with text but NSTextField does not use this. I can't seem to make the label show up as UITextField.
What am I doing wrong?

Resources