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.
Related
The source code shown in the Assistant Editor for "ViewController.swift" is different than source code shown in Main Editor for "ViewController.swift".
"ViewController.swift" in Main Editor:
// ViewController.swift
// FoodTracker
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var mainButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.delegate = self
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
mealNameLabel.text = textField.text
}
// MARK: Actions
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "DEFAULT text"
// mainButton.tintColor = UIColor.darkTextColor()
}
}
"ViewController.swift" in Assistant Editor:
//
// ViewController.swift
// FoodTracker
import UIKit
internal class ViewController : UIViewController, UITextFieldDelegate {
#IBOutlet weak internal var nameTextField: UITextField!
#IBOutlet weak internal var mealNameLabel: UILabel!
#IBOutlet weak var mainButton: UIButton!
override internal func viewDidLoad()
internal func textFieldShouldReturn(textField: UITextField) -> <<error type>>
internal func textFieldDidEndEditing(textField: UITextField) -> <<error type>>
#IBAction internal func setDefaultLabelText(sender: UIButton) -> <<error type>>
}
So, those are completely different different files, but have the same name. The one in the Assistant Editor is the interface while the file shown in the Main Editor is the implementation of the interface, right?
That seems a little weird, but the interface and implementing class have the same name? When I'm working in Xcode I need to be aware that sometimes two files can (often?) have the same name?
I had the same problem. I couldn't figure out why it was showing this "internal class" file.
I managed to get it to display the correct associated file. To do this, click the associated editor icon, the two circles. Then in the window that appears, click the '+'. The new window that appears should have the right code in it. Then close the old window and you should be left with the right one. The rest of the project appears to work be fixed now.
I found this in Xcode 9.2, Swift 4, but it may be in other versions.
In Xcode, with the option key down, hover your mouse over 'func' or another scope-describing keyword to reveal the scope with the blue bracket.
Then do a two finger tap on the trackpad to automatically open an Assistant Editor at the same point in the code. I'm frequently going somewhere else in my code but want to have a window open that stays where I just was. This trick does just that.
(Make sure your track pad is set so a 'Secondary click' is a tap with two fingers.)
(Swift 4.2) I just had the same problem. Whenever I was in the assistant editor, my ViewController was labeled internal and I couldn't make any edits to it. The above solutions didn't work for me, but they put me on the right trail. Apparently, I was in some kind of duplicate file and not in the ViewController I needed. Correcting the issue was simply a matter of navigating to the correct file via the controls at the top of the assistant editor.
I have seen what could be a solution at the video youtube.com/watch?v=wPAUKhlmW1M at time-position 1:31 - please correct if I'm wrong
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.
}
}
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.
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
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