I'm an experienced Java and Visual Studio developer, but I am just starting out with Swift & Xcode. I am an utter noob, who has never written a line of objective-c. I want to print "Hello World" when I click on the button that I have added in the "Storyboard" view for my application. But I cannot figure out how to get Xcode to insert an event method anywhere in the swift source code files. I’ve read %60 of "The Swift Programming Language", but I don't know any of the cocoa apis yet. Most examples reference objective-c files that do not exist in a Swift application. I also am not using the iOS platform, so I suspect some examples I've pasted (i.e. @IBAction func buttonTapped ) fail because they are not applicable to OS X applications.
I would appreciate a little help :)
Writing Output with print() You use the print() function in Swift to write a string to standard output, which is typically the Console in Xcode. Here's an example: print(“So long and thanks for all the fish!”) The above example can't get any easier.
In Swift, you can print a variable or a constant to the screen using the print() function.
You just need to add a label and a button to your window. Then you need to connect your label outlet to your code.
You need to connect your button to your viewController also:
import Cocoa
class ViewController: NSViewController {
@IBOutlet var strLabel: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
@IBAction func printHello(sender: AnyObject) {
strLabel.stringValue = "Hello World !!!"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With