Xcode 6.2 beta 2 WKInterfaceController watchkit error - xcode

This code:
class InterfaceController: WKInterfaceController {
#IBOutlet weak var petTable: WKInterfaceTable!
var petnames = ["Luna", "dylan", "Mery", "Mady", "Paul Newman", "heidi"]
override init(context: AnyObject?) {
// Initialize variables here.
super.init(context: context)
return two errors:
" Initializer does not override a designated initializer from its superclass" in the line override
" Must call a designated initializer of the superclass 'WKInterfaceController'" in the line super.init
the error is in Xcode 6.2 beta 2 (launch today)
In the previous version of Xcode no errors show

Replace
override init(context: AnyObject?) {
// Initialize variables here.
super.init(context: context)
// Configure interface objects here.
NSLog("%# init", self)
}
with
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
NSLog("%# awakeWithContext", self)
}
Clean your project then build and run

init(context: AnyObject?) does not exist anymore. For initialization just use init and then use awakeWithContext to initialize from context.

Related

Passing data forward to destination VC (with framework) in Swift?

I'm writing an application that takes data from a QR code scanner. I am just implementing the basics, and have a second view controller that is triggered when a new QR code is detected. The following code is in a custom view controller from RSBarcodes and implemented using CocoaPods.
It won't compile with the error
"Use of undeclared type SecondViewController".
I tried making my SecondViewController public, restarting XCode, and nothing has worked. Any help is appreciated. Thank you!
override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! SecondViewController
destinationVC.label = "test"
}
SOLUTION:
Added to "Copy Bundle Resources" and then my VC was immediately recognized by the compiler.
SOLUTION:
Added to "Copy Bundle Resources" and then my VC was immediately recognized by the compiler.
First you have to verify which segue you're pushing to by checking the segue identifier name. Then you have to check if you can even create the destinationVC variable, and then if you can unwrap it using '?' instead of '!'.
override public func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject?) {
if segue.identifier == "nameOfMySegue" {
if let destinationVC = segue.destinationViewController as? SecondViewController {
destinationVC.label = "test"
}
}
}

Parse SDK 1.7.1 not working in Xcode 6.3

My code worked fine in Xcode 6.2. After the update to Xcode 6.3 I had some Nullabilty Errors.
I could solve these errors after I downloaded the Parse SDK 1.7.1. So I deleted the old Parse framework files in my project and pasted the new ones into it. Additional I convert my code to the latest swift syntax "Edit/Convert/latest swift syntax". Now I haven't problems with Nullabilty Errors but several others.
In my project I have a simple Tableviewcontroller with the following code:
import UIKit
class HaendlerTableViewController: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init!(style: UITableViewStyle, className: String!) { //1. Falialbe initialize init/style:className:)' cannot override a non-failable initializer
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.parseClassName = "Haendler"
self.textKey = "name"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery! { //2. Ovverriding method with selector queryForTable has incompatitble typ () -> PFQuery
var query = PFQuery(className: "Haendler")
query.orderByAscending("name")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell { //3. Ovverriding method with selector 'tableView:cellForRowAtindexPath:object:' has incompatible type '(UITableView, NSIndexPath, PFObject) -> PFTableViewCell
var cell = tableView.dequeueReusableCellWithIdentifier("HaendlerCell") as! HaendlerCell!
if cell == nil {
cell = HaendlerCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
// Extract values from the PFObject to display in the table cell
cell.haendlerName.text = object["name"] as! String!
var thumbnail = object["logo"] as! PFFile
var initialThumbnail = UIImage(named: "haendler")
cell.haendlerBild.image = initialThumbnail
cell.haendlerBild.file = thumbnail
cell.haendlerBild.loadInBackground()
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var detailScene = segue.destinationViewController as! HaendlerDetailViewController
// Pass the selected object to the destination view controller.
if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
detailScene.currentObject = objects[row] as? PFObject //4. Could not find an overload for 'subscript' that accepts the supplied agruments
}
}
}
I wrote the errors in a comment on the right side of the code and
below.
Falialbe initialize init/style:className:)' cannot override a non-failable initializer
Ovverriding method with selector queryForTable has incompatitble typ () -> PFQuery
Ovverriding method with selector 'tableView:cellForRowAtindexPath:object:' has incompatible type '(UITableView, NSIndexPath, PFObject) -> PFTableViewCell
Could not find an overload for 'subscript' that accepts the supplied agruments
I have the same errors when I make a new Swift project from the Parse Quickstart and add one Tableviewcontroller. In my old project was an objective-C bridging header which one I deleted because I had the oppurtunity to add the Parse SDK 1.7.1 directly in my Swift project.
Now I need help because I don't see what I have to change..
PS: Sorry for the mix of German and English code I'll adjust it once the project is running again
I had the same issue as I just updated Xcode to 6.3 about 20 minutes ago.
For your 2nd error, remove the '!' after 'PFQuery'. So it should now look like..
override func queryForTable() -> PFQuery {
This solved my problem in regards to that specific error.
I never used an init method as you did in your first error, but try removing it and see what you get. My PFQueryTableViewController works fine without it.
Had the same issues.
To solve the first initialise issue remove the '!' after 'override init'. Should look like this:
// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) { //1. Falialbe initialize init/style:className:)' cannot override a non-failable initializer
super.init(style: style, className: className)
}
Do the same for the 2nd error after 'PFQuery'
override func queryForTable() -> PFQuery {
Hope its helpful. Since the latest update unwrapping elements usually needs to be revised for possible errors.

how do I switch between NSViewControllers using NSPageController?

I have had bad luck finding any examples on the web that closely match what I am trying to do. I am trying to using NSPageController to view and switch between multiple NSPageControllers. My steps.
I create a new OS X swift project
I add an object to the ViewController and make it of NSPageController class.
I add two buttons, one I label "Next" and the other one I label "Back" for the transitions.
I link the buttons to the NSPageController object as navigateForward and navigateBack actions.
I create an outlet in the custom NSViewController class for the NSPageController object and add the specific NSPageController delegate methods.
I add two additional view controllers in storyboard and create an identifier for them to reference back in my custom view controller class: Wizard1, Wizard2.
import Cocoa
class ViewController: NSViewController, NSPageControllerDelegate {
#IBOutlet var myPageController: NSPageController!
override func viewDidLoad() {
super.viewDidLoad()
let vc1: AnyObject? = self.storyboard!.instantiateControllerWithIdentifier("Wizard1")
let vc2: AnyObject? = self.storyboard!.instantiateControllerWithIdentifier("Wizard2")
self.myPageController.arrangedObjects.append(vc1!)
self.myPageController.arrangedObjects.append(vc2!)
// Do any additional setup after loading the view.
}
override init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
myPageController = NSPageController()
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil?)
}
required init?(coder aDecoder: NSCoder) {
myPageController = NSPageController()
super.init(coder:aDecoder)
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func pageController(pageController: NSPageController, identifierForObject object: AnyObject!) -> String! {
return "View"
}
func pageController(pageController: NSPageController, viewControllerForIdentifier identifier: String!) -> NSViewController! {
let vc1: AnyObject? = self.storyboard!.instantiateControllerWithIdentifier("Wizard1")
return vc1 as NSViewController
}
func pageController(pageController: NSPageController, prepareViewController viewController: NSViewController!, withObject object: AnyObject!) {
viewController.representedObject = object
}
func pageControllerDidEndLiveTransition(pageController: NSPageController) {
pageController.completeTransition()
}
func pageControllerWillStartLiveTransition(pageController: NSPageController) {
self.presentViewControllerAsModalWindow(self.storyboard?.instantiateControllerWithIdentifier("Wizard2") as NSViewController)
}
}
The error I get when pressing the Next button is:
-[NSNib initWithNibNamed:bundle:] could not load the nibName: NSPageController in bundle (null).
Perhaps you are trying to load a nib with the wrong name in AppDelegate.m or wherever you are initializing your page controller.
Otherwise you have missed creating a .xib file and to name it NSPageController. When creating a Cocoa Touch Class there is a checkbox to also create an xib file for your class if needed.
This line is responsible for the error:
myPageController = NSPageController()
You're trying to initialize a view controller without a nib, that's why it does not work. By default the NSViewController's name is taken to identify the nib that corresponds to it. In your case it is "NSPageController".

UIView inheritance and custom initializers throws EXC_BAD_ACCESS in swift

I have classes with inheritance like:
UIView -> TestSubclass -> Test
class Test has some properties and methods. It works until I add some initializers into class TestSubclass:
class TestSubclass : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
print("CustomActivityInit")
}
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
print("CustomActivityInitCoder")
}
}
After this I can't access properties of my Test class
override func viewDidLoad() {
super.viewDidLoad()
var test = Test(frame: CGRect.nullRect)
test.test = "zzz"
}
and get EXC_BAD_ACCESS error on line test.test = "zzz"
Could you help me understand the reason, please?
This is the test project to see the issue https://www.dropbox.com/s/1d8fvxm0es9b5n4/TestInit.zip
I use XCode 6 beta 5, deployment target iOS7+, iOS8 SDK
You must override init() because of inheritance from UIView.
class TestSubclass : UIView {
override init() {
super.init()
}
}
The reference from Apple:
Default Initializers
Swift provides a default initializer for any structure or base class that provides default values for all of its properties and does not provide at least one initializer itself. The default initializer simply creates a new instance with all of its properties set to their default values.
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html

What's the proper way to access a delegate via Swift?

I'm attempting to emulate the Objective-C pattern of accessing an object's delegate using Swift. Normally I would put the protocol in a .h that is shared between the two UIViewControllers.
Purpose: to call a delegate (host) to dismiss a (pushed) UIViewController.
Problem: Unable to access the delegate's hello().
The following codes compile, but I get a run-time error: unknown delegate method (see below).
The Host/Calling (Delegate) Object:
import UIKit
class MainViewController: UIViewController, ProtocolNameDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
// ---------------------------------------------------------------------------------------------------------------
// Protocol Delegate
func hello() {
println("Hello");
}
// ---------------------------------------------------------------------------------------------------------------
#IBAction func greenAction(sender : AnyObject) {
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("GreenViewController") as GreenViewController
secondViewController.delegate = self;
self.presentViewController(secondViewController, animated: true, completion: nil)
}
// ---------------------------------------------------------------------------------------------------------------
#IBAction func exitAction(sender : AnyObject) {
exit(0)
}
The pushed (second or 'green') UIViewController that is to be dismissed:
import UIKit
#class_protocol protocol ProtocolNameDelegate {
func hello()
}
class GreenViewController: UIViewController {
weak var delegate: ProtocolNameDelegate?
// ---------------------------------------------------------------------------------------------------------------
#IBAction func returnAction(sender : UIBarButtonItem) {
println("Inside returnAction")
delegate?.hello()
}
}
Revision: corrected delegate access to: delegate?.hello()...
And re-run the application. The following is via debugger:
(lldb) po delegate
Some
{
Some = {
payload_data_0 = 0x0d110b90 -> 0x00009a78 (void *)0x00009b70: OBJC_METACLASS_$__TtC9RicSwift218MainViewController
payload_data_1 = 0x0d110b90 -> 0x00009a78 (void *)0x00009b70: OBJC_METACLASS_$__TtC9RicSwift218MainViewController
payload_data_2 = 0x00000000
instance_type = 0x00000000
}
}
(lldb) po delegate.hello()
error: <REPL>:1:1: error: 'ProtocolNameDelegate?' does not have a member named 'hello'
delegate.hello()
(lldb) po delegate?.hello()
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0xb3145b0).
The process has been returned to the state before expression evaluation.
Question: What am I missing or doing wrong?
The issue here is that delegate is an Optional. So to access it you should do
delegate?.hello()
This makes it so you unwrap the optional, and call hello on it if it is non nil
ProtocolNameDelegate? is a distinct type from ProtocolNameDelegate, specifically Optional.
I had the same problem. I've been told that this is a bug in Swift (currently present in Xcode 6 beta 5). Strong protocols work well, but weak protocols not yet. The workaround is to declare the protocol as an Objective-C protocol with #objc protocol ProtocolNameDelegate instead of plain protocol ProtocolNameDelegate.
Since delegate is optional, you have to unwrap it, quickest way will be to use optional chaining:
delegate?.hello()
You'll also need to make sure that the delegate protocol is flagged as #objc (rather than #class_protocol in this case)

Resources