Thread 1: signal SIGABRT when switching from Obj-c to Swift file - objective-c

I have changed from Obj-c to swift. However in my view controller identity inspector, only Obj-c files are available when I try to set the custom class.
Any ideas how to get the Swift files to appear?

My issue was that my class was a subclass of UIViewController despite it being a UITableView. Changing this in the class worked.

Related

AppDelegate instantiated last on launch

I'm very comfortable with writing iOS apps, but OS X unexpectedly seems somewhat alien.
Here's the question upfront (read on for context):
When my application launches using the .xib set in the Main Interface field under the deployment info of my apps target, why does the AppDelegate get instantiated after the ViewControllers?
Context (no pun intended):
The reason I ask is because I'm using Core Data (spare me any heckling for this decision), and typically you keep a pointer to the MOC (Managed Object Context) in AppDelegate. One of my controllers is trying to get this MOC instance variable but the AppDelegate instance isn't around yet and therefore my app doesn't present data just after launch.
The AppDelegate and the two ViewControllers are in the .xib. The VCs are hooked to views inside a split view. They're trying to use the MOC in viewDidLoad to make queries. They are accessing the AppDelegate like this:
let delegate = NSApplication.sharedApplication().delegate as AppDelegate
let moc = delegate.managedObjectContext
This will crash as the .delegate property of the sharedApplication() returns nil.
I tried making an NSWindowController from the .xib in applicationDidFinishLaunching and removing the .xib from the Main Interface field, but then applicationDidFinishLaunching doesn't get called at all.
I've ensured that all the connections in IB for from the Application and the Files Owner (NSApplcation) delegate IBOutlets to the AppDelegate have been made.
UPDATE - 31/03/15
Stephen Darlington's answer below offers a good solution for my/this case. And as I understand it's actually better to setup the MOC in the way he's suggested.
If a correct answer arrives that explains why the AppDelegate is being instantiated at some later time in the launch process, I'll mark it correct instead of Stephen's. Thanks Stephen!
The "easy" solution would be to have managedObjectContext create a MOC if one doesn't exist (i.e., change it from a property to a method). That way which ever code gets there first the stack will be available.
(I'll spare the lectures about both creating the Core Data stack in the app delegate and accessing the app delegate like that!)
Here's another option without having to subclass NSApplication:
Don't put your view controllers in the .xib that you set as the Main Interface, just have the Main Menu (menu bar), AppDelegate and Font Manager in there.
Then make your view controllers in other .xibs.
Then in the applicationDidFinishLaunching method init your view controllers from their .xib files.
I also faced this issue with setting up Parse. To get around it, I simply subclassed NSApplication and set it as the Principle class in the Info.plist. In your NSApplication subclass, override the init methods and initialise Parse or anything else you need to, there.

Change of AppDelegate class on OSX

I have a cocoa project on OSX. For that, I had to change the class of the Appdelegate.
I did this in two steps: First I implemented the new appdelegate class (I didn't implement the NSApplicationDelegate protocol yet) and checked if it worked and compiled. In the new class I already implemented the applicationDidFinishLaunching method. Everything was still fine!
After that I changed the protocol implementation. I removed the prorocol from the old Appdelegate and inserted it in the new one. Even after this step everything seemed to be fine! The application compiled and worked as it should. But as I was somewhat sceptical, I inserted log statements in the new and the old class.
After inserting this, I had to see, that the application still used the old appdelegate!
Obviously, there is something where the name of the old appdelegate is still known! But where is that? Or do I have to do something else to reach my goal? Does anyone know what I have to do?
There are two things that have to happen: 1) an instance of your new class has to be instantiated. 2) That instance needs to be assigned to the delegate property of the application object (instance of NSApplication or a subclass).
In a typical Mac app, both of those things are done in the MainMenu NIB. That NIB contains a freeze-dried instance of the appropriate class. If you were to build the NIB from scratch, you would drag an "Object" (blue cube) from the Object library to the NIB document. By default, that would represent an instance of NSObject. You would then select it and bring up the Identity inspector where you would change its class to your new app delegate class. Since you're not building the NIB from scratch, there's already an Object in the NIB. You can select it and change its class.
The other step, assigning it to the delegate property of the application object, is done by connecting the delegate outlet of the placeholder for the application object in the NIB to the object. Again, since you're not building the NIB from scratch, that's already done.
So, in summary, you just need to change the class of the app delegate object in the MainMenu NIB.
Update: Here's a screen shot of what you have to change:

Property anchorPoint cannot be found in forwar class object CALayer

I downloaded this sample: https://developer.apple.com/library/ios/#samplecode/Touches/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007435
If I start the sample app everything is working fine.
I made my own app and copied most of the code into it. There is a method called adjustAnchorPointForGestureRecognizer: But in this method there is a error but only in my app (see screenshot): "Property anchorPoint cannot be found in forwar class object CALayer". I am wondering how I can solve this issue, I don't know what I did wrong. It is exactly the same code from the sample. Anyone can help? Thanks!
Did you import the QuartzCore framework into your project? Maybe that's why it can find the CALayer class
This error is thrown by XCode when it can't find a definition for CALayer. You should add the framework to your project and add #import <QuartzCore/QuartzCore.h> to wherever you are using the class

Compiler Error in a Blank Project

I've started a new Navigation-based project in X-Code. I delete the default grid view, add a new blank view, and connect it to the File's Owner via ctrl+click+drag, and it builds fine, but before anything happens on the iPhone simulator or the real thing, I get "Thread 1: Program recieved signal: "SIGABRT"" when the program hits "[self.window makeKeyAndVisible]". I'm really new to Objective-C and X-Code, but I know quite a bit of C# and C++. I don't really understand if I've set something in the IDE wrong, or what...any help would be greatly appreciated. Thanks!
When you create a new navigation project, the RootViewController class is a subclass of UITableViewController. Your error is due to removing the UITableView from the xib file, and replacing it with a UIView.
To fix this, you should change the super class of RootViewController to UIViewController. (Don't forget to remove the UITableView datasource and delegate methods from the implementation file for clarity.)

How can I initialize a custom GUI class?

I'm developing an iPad app.
Now I ran into the following problem. I created a "Custom Class" for a UIScrollView. So in my nib file I have added a default UIScrollView and set the Custom Class to MultiSelectView (which is the custom class I created). Screenshot here.
So in my MultiSelectView I have added some methods that I want to use. This works!
The issue is that I'm wondering how I can initialize certain objects that I need in these methods. It seems like - (id)initWithFrame:(CGRect)frame {} is not called, and neither is - (void) viewDidLoad {}.
Thus, is there a way to initialize a custom (GUI) class?
When you unarchive a view from a .xib file, it is not sent -initWithFrame:, as you've noticed. Instead, it's sent -initWithCoder:.
So, if you've got any UIView subclass in a .xib file that needs custom initialization, you'll need to override -initWithCoder: as well as (or instead of) -initWithFrame:.
Looks like you need initWithCoder, it is called when object is loaded from NIB
Or, better, awakeFromNib. The difference is that awakeFromNib is called when all outlets are connected.