Swift Application Initialization not Working - macos

I am making a Cocoa app, and previously, all my code was in AppDelegate.swift. I wanted to write an extension but it wouldn't let me do it there, so I created another class, and pasted my code in it. After reconnecting all my objects to their respective Xibs (sorry if my technical jargon is a bit off) the code is the exact same except for the initialization. I used
func applicationDidFinishLaunching(aNotification: NSNotification)
which worked previously in AppDelegate.Swift but now no longer gets executed. Is there a reason for this?

Well it turns out I just needed to add a delegate from my new class to AppDelegate.swift. Now at least the launching works, but now unfortunately everything I write there is crashing. Oh well.

I did not understand well what your problem is, anyway you could follow these steps:
Create your class (suppose to be a UIViewController)
Connect your class with a ViewController in your main storyboard (make sure your app uses that storyboard in Project Settings)
To get started with your code use methods like viewDidLoad, viewDidAppear and so on, which are triggered automatically, like didFinishLaunchingWithOptions you used before.

Related

NativeScript importing iOS framework classes and downcast

I have a custom Swift framework in a Cocoapod, wrapped in a NativeScript plugin. My NativeScript TypeScript code can do everything it needs to access classes and methods in the classes in the framework. That in itself was a lot of work to figure out all the nuances. Now I'm at the last part, which is to downcast a custom viewController in the framework from UIViewController. The following code is working, in that NativeScript actually loads my storyboard and controller:
var podBundle = NSBundle.bundleForClass(CustomViewController)
var bundleUrl = podBundle.URLForResourceWithExtension("MyFramework", "bundle")
var bundle = NSBundle.bundleWithURL(bundleUrl)
var storyboard = UIStoryboard.storyboardWithNameBundle("Main", bundle)
var viewController = storyboard.instantiateViewControllerWithIdentifier("CustomViewControllerID")
console.log(viewController)
console.log(viewController instanceof CustomViewController)
application.ios.rootController.presentViewControllerAnimatedCompletion(viewController, true, null)
The first console.log actually logs the full class, as in MyFramework.CustomViewController: hexcodehere
The second console.log logs true as well, meaning it knows the type of the viewController variable is indeed CustomViewController.
The problem is, I need to call a function in CustomViewController, so I need to downcast:
storyboard.instantiateViewControllerWithIdentifier("CustomViewControllerID") as CustomViewController
But the compiler says "Cannot find name" to CustomViewController. If I try MyFramework.CustomViewController, it says "Cannot find name" to MyFramework. Notice NSBundle.bundleForClass(CustomViewController) works. So that means CustomViewController is known to the compiler.
If I import the plugin at the top of the file:
import { CustomViewController } from "myplugin", it says "cannot find module" to myplugin. Mind you I can actually access all the classes in this plugin. So the plugin is definitely properly installed. It is installed from a local file path, not git.
It seems I'm missing something devilishly simple. But I can't figure it out for the life of me.
I'm happy to help others who want to use native iOS storyboard and Swift classes, etc.
Attesting to the brain's ability to work on a problem in the background, it suddenly came to me. Since the runtime is aware of the type, all I have to do is to cast to "any" to get around the compiler issue:
var viewController = storyboard.instantiateViewControllerWithIdentifier("CustomViewControllerID") as any
Then the compiler allows me to call whatever function on my CustomViewController viewcontroller instance without a complaint, and runtime is fine too. To be extra safe, wrap the calls to subclass methods in a if instanceof block.

IBOutlet connect fails to connect to renamed target

So, I was following along a core-data tutorial RayWenderlich.com part 1/3 circa 2012 using Xcode 8. Naturally some things are different now, like the default view controller and storyboards vs xib being used.
So rather than explicitly creating a xib for the MasterViewController, etc. (I instead did things the Xcode 8 way), I just renamed the supplied ViewController, and the associated .h and .m contents to MasterViewController. All was fine until I got to the step to add its outlet in the app delegate.
No joy.
Snippet shows the outlet is there but I cannot link to it.
I uploaded the project to ScaryBugsMac on github as I'm stuck what's wrong. Tried suggestions on similar question to remove/add, import vs include for the header, and to publish the outlet by putting it in the app delegate header.
I'm thinking the rename isn't being handled but I want to know how it's broken and how to fix it.
Well I punted and restarted, this time without renaming the controller. Find "part 1" of the tutorial in ScaryBug on github.

Xcode not autocompleting Parse functions in Swift

I'm trying to build an app written in Swift that uses Parse, but I've realized that my Xcode no longer provides autocomplete for any of the Parse functions. The app still compiles and builds with no problem on the iOS simulator so I know the framework is being read/compiled correctly but it's very frustrating to develop without being able to use the autocomplete features, especially since the API Reference doesn't yet have Swift code.
For my current project, I added pod Parse to my Podfile and thus added the sdk that way. I've also followed the instructions here: http://blog.parse.com/2014/06/06/building-apps-with-parse-and-swift/ to create a Appname-Bridging-Header.h file, adding #import <Parse/Parse.h> there to import the header file. So I'm not sure what the problem is.
There is definitely some support for autocompleting bridged Obj-C libraries into swift but it's not perfect yet.
For example, if you subclass PFQueryTableViewController and type 'tableview' on a new line, all the standard UITableViewController protocol methods appear as autocomplete, in addition to the PFQueryTableViewController-specific method:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell!
However, this isn't really a Parse-specific issue and will probably improve with new releases of swift/Xcode.
If I understood question correctly, try this:
var gameScore : PFObject = PFObject(className: "GameScore")
or
var gameScore = PFObject(className: "GameScore") as PFObject
I had issue with swift's methods' autocompletion after the object was created, because XCode couldn't recognize class of new object if you don't write it manually.

Adding multiple Cib / Xib files in Cappuccino

Currently I'm working on a product that uses the Cappuccino Framework and the Objective-J language.
I created my project using this command after installing Cappuccino: capp gen -t NibApplication Myapp
The Problem I'm facing is that I want to keep my code and GUI clean.
By doing so I wanted to split the GUI up in separate Xib / Cib (compiled version Cappuccino can read) and separate controllers, like I do with iOS and Mac apps.
I couldn't find on the internet or on the docs how to do so.
Also, all the examples (including the NibApplication template) contains only 1 xib file.
In short what I'm after is to load a controller, based on a XIB file which holds the window. All the outlets are connected to the controller I loaded.
Is there anyone who knows how to do it?
Thanks in advance and have a nice day!
The typical way to do this is to use a CPWindowController or a CPViewController for each xib you want to load. Here's what it might look like:
var myViewController = [[CPViewController alloc] initWithCibName:"mynib" bundle:[CPBundle mainBundle]];
This would load Resources/mynib.cib from your main bundle. Normally you'd also subclass CPViewController so as to give yourself a convenient spot for your control code.
Fixed it myself! I used this tutorial: http://vimeo.com/8948460
But instead of using atlas I used XCode. Just follow the steps but with XCode and you'll be fine if you want the above to happen.

Why does Xcode 5 assistant editor defaults to .m file

I realized that when launching assistance editor while working on a XIB file no longer opens the header file by default, instead it shows the implementation file. Is there a new workflow going on I am not aware of? While in the implementation file I can only seem to be able to add IBActions. What is the "new" way to create IBOutlets? Switching to header file each time??
My 2 Cents:
When you create a new class using the Xcode templates, it usually provides a class extension in the implementation file for private properties. It makes a lot of sense to actually link the IBOutlets and IBActions there, because in most cases they are only ever used within the class itself.
The only exception to this that crosses my mind right now is UIView subclasses, and especially UITableViewCell. A lot of developers access the IBOutlets directly in cellForRowAtIndexPath:.
Apple engineers' message:
Encapsulate your IBOutlets & IBActions whenever possible!

Resources