Xcode builds app correctly, but unable to launch? - swift

I'm running 10.10 with Xcode 6.1.
And I'm having trouble getting an app I wrote to work. After hours and hours I finally got it to build correctly after accidentally messing with something, but now I'm getting an error I don't have permission to view the file (if I open directly in Xcode) or if I try to open it from the Finder it says the App is damaged or corrupt and can't be open. If it matters it's written in Swift.
I've literally spent 6 hours on this (Just trying to get it to build) so any help would be greatly appreciated! Austen
Here's the Xcode workspace, project, files, and a build if anyone needs to take a look: http://pattersoncode.ca/ServerChecker.zip

There are a several issues here. I can't help you with all of them, but I can put you on the right path, at least.
The app won't launch because it wasn't built correctly. Specifically, there's not any actual binary executable in the built .app. You can see this by right-clicking on the built Server Checker.app and choosing Show Package Contents. In there is a Contents folder; open that. Inside that should be a folder called MacOS, in which your actual binary executable would be. But it's not there. This isn't really an app at all.
Reason is, you have a custom build rule for swift files that's making them not compile at all. Just remove that, you don't need it.
In Xcode, click on the top-level item of the project (it says something like ServerChecker, 1 target, OS X SDK 10.10). Then, under TARGETS, select ServerChecker. Click Build Rules. The thing that says "Swift source files using Script", that we don't need. Click the little X in the top-right corner, and click Delete when prompted. Now try to build again.
You'll get an error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
Since you don't have a main.swift file, your app doesn't have a main entry point. In Swift, you must either provide an NSApplicationMain function (usually in main.swift), or you can declare your app delegate as NSApplicationMain, which has the effect of calling NSApplicationMain with your delegate class. I recommend the latter.
Open up AppDelegate.swift, and add #NSApplicationMain to your AppDelegate class. It should look like:
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
...
}
Now your app should build again. Right-click it and Show Package Contents to verify that there's actually an executable binary in there.
The app still won't run. It looks like on launch there's an error that a library couldn't be loaded.
Hopefully that helps you get moving, anyway. I really need to get back to work now. :)

Related

Actions broken in Xcode 9 project

OK, so here's the deal...
I'm trying to connect even the simplest actions (the typical ctrl+drag from the UI builder to the corresponding file), the actions are created fine, but they're not working.
What the UI (with a test action) looks like:
And the corresponding part in AppDelegate.swift (which doesn't get called no matter what)
In case it makes any difference:
macOS : High Sierra 10.13.2
Xcode : 9.1 (9B55)
Project platform : macOS
Project language : Swift
Storyboards : NO
Auto Layout : NO
Which files should I have a look into, in case something has been corrupted?
I have had a look into MainMenu.xib but the <connection> and <action>s there seem to be linking the correct things. Maybe it's the project.pbxproj file?
Note: The project in question is a result of (automatically) duplicating a previous Xcode (+Swift) project, so chances are something might have gone wrong during the process. (although it normally works for me). Let's see...
The first and possibly only place to sanity check Outlets,Segues,Actions is this right panel menu in Xcode:
I am thinking that the #IBAction line for buttonClicked should be in the viewController file instead of the AppDelegate file

iPhone app opens to black screen after refactor proj name in xCode 4

Guys, i wonder if any one can help - My project was fully working in both the simulator and on a device, however, i renamed the project in xcode 4 (double clicking at the root of the project navigation pane, it was happy to rename any references - but now when i launch the app it only opens to the main window (yes it has outlets, all have outlets etc etc),
no code in the app delegate stops at a break point (even in didFinishLaunchingWithOptions)
so unsure what i could have missed, i have looked at other answers and everyone writes and says "missing outlet" etc.
I have dragged some backed up version of the app delegate that used to work and no difference, what could i be missing.
I do have source control, however currently in the process of merging from VSS to GIT.
thanks
lee
Check your Info.plist for what it uses as it's Main Nib File. Perhaps something changed there? Also, within the nib file itself you might have problematic connections to classes that are still named with the old name.

XCode uses an .xib not inside the project folder

I went back to an old project and recompiled it for OS 4.1 succesfully and ran it. I was asked to add a fourth tab to the Tab Bar, but I could not set its class to the newly added ModelsVC.h/.m. [edit: because it did not appear in the class dropdown, but the other viewcontrollers were there.]
I moved the project folder to a backup folder and unzipped a months old backup of the same project. It compiles and runs until the end of viewWillAppear for the first (default) view connected to the first tab, and then does nothing more. It is still running, but nothing is shown and nothing happens.
In the console, it says "Unknown class ModelsVC in Interface Builder file."
The problem is, that class didn't exist two months ago, so why should this way old project complain about it or even know about it?
I'd like to know where to look / tell XCode to use the .xib that is inside the project folder it is currently compiling.
And if it's path-related, how do I check what paths XCode looks in when a project is loaded/compiled?
For Xcode to run a .xib file inside the project folder, You can just open your xcode, add existing file and it is there.
I think the problem why the old project still runs is that it builds incrementally. That means when it builds the first time, the .xib file was there, and when it build the second time, it doesn't need to add and build the .xib file any more. So, in the second build, even if you delete and move out the .xib file, I think it still can build
Seems a 'sloppy copy' for whatever reason (such as a quick backup to try something experimental) of a project folder causes confusion. See this.

Xcode duplicate symbol _main

I'm getting the following error in Xcode 3.2.1 on Snow Leopard 10.6.2 whenever I try to compile any iPhone application generated by Appcelerator Titanium. However, the build error only appears when I select iPhone simulator on the architecture menu and if I select the iPhone device, I am able to run the app on my device .
Further more, the iPhone simulator launches successfully and executes the program directly from the Titanium environment which uses Xcode to build .
Why is this happening ?
ld: duplicate symbol _main in Resources/libTitanium.a(main.o) and /Users/prithviraj/Documents/project/Final/build/iphone/build/Final.build/Debug-iphonesimulator/Final.build/Objects-normal/i386/main.o
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
I can't tell you why this is happening, but I can suggest a workaround. When I commented out the entire contents of the main.m file that was generated for my Titanium project, I was able to compile successfully and run on the Simulator. Let me know if that works for you.
Check to see if you have multiple declarations of main function in your project.
I just spent a couple hours battling this one. It was because I was using the -all_load linker flag. If you are using that flag to get around the category bug, there are some other solutions -- see here.
I had a similar problem. A unit test class was accidentally included in my build. If you search your project for "main(" you'll probably find the duplicate functions.
This happened to me for 2 reasons:
1: Class A calls class B and both had imported the same class. Fix it by importing the class in .m file.
2: Two classes have a constant with the same name (even if the constant is defined in the .m file). Fix it by changing the name of the constants.
I had this problem because I define a file as such:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
...
}
And also had a file main.m:
int main(int argc, char* argv[])
{
#autoreleasepool {
int retVal = UIApplicationMain(argc, argv, nil, #"AppDelegate");
return retVal;
}
}
Seems as though there are multiple ways to get into this state. Mine was different. I read a hint where you could drag from a .xib event selector into the .h implementation of your view controller and it would auto-gen your methods. It did - which was cool. I immediately started getting duplicate symbol errors - which was not cool.
I did not have time to dig deep into the linker to see what happened. I created a new view controller, copied the context of my old .xib into the new guy. Deleted the old .h, .m and .xib and built and it worked again. Very odd, very annoying time waste.
There is obviously some bug with this xcode "convenience".
I found this happened when i had an implementation file with a main function in it (say abc.m) and also had another main.m. Once I commented out the main function in abc.m, the project compiled successfully.
Based off what I can tell from these other answers, I'm going to need to be remove a bunch of main methods.
But to do that easily I first need to remove all the gd comments from my files because they are assiduously documented with comments at nearly every other line
This regular expression matches all C multi-line comments including their delimeters & may help you on your journey
/\*((?!\*/).)*\*/
I spent more than an hour in searching for a correct answer but nothing worked for me.
Finally the xcode it self telling something is duplicated, so go to that particular folder(in this case: /Users/prithviraj/Documents/project/Final/build/iphone/build/Final.build/Debug-iphonesimulator/Final.build/Objects-normal/i386/main.o) and delete all the files and also check same in your project whether that particular class/interface is declared twice if yes delete it.
After deletion clean and run the project.
It worked for me hope this helps(-_-).
Delete /Users/{username}/Library/Developer/Xcode/DerivedData folder and build again.

Rename a class in Xcode: Refactor... is grayed out (disabled). Why?

Why is Refactor... grayed out (disabled) in Xcode?
I'd like to rename a class.
Select the class's symbol in its header file - i.e. the bit just after #interface. Then the refactoring stuff should be enabled.
If you are using Objective-C++ (i.e. mix Objective-C with C++ code) then refactoring is disabled in xcode since it does not support refactoring of C++ code.
Refactor might also be disabled if affected files (most likely the file with your class in it) are not saved.
I've been using Xcode for 5 years now, and refactoring has never worked correctly (even xcode 4.6 has major bugs where it WILL corrupt your source code!).
The workaround has always been (still works 100%, even in cases where Apple's code fails)
use shift-command-f to find all uses of the file
select "replace" in the search settings
"replace-all"
do the following for the .h file, and REPEAT IT FOR THE .m FILE (if you have one):
right click the original file, and select "show in finder"
delete the file from xcode (select "delete references only" when asked)
rename the .h (and .m if you have one) in Finder
in Xcode, select "Product -> (hold down Alt) -> Clean Build Folder"
quit xcode (you can usually get away with not doing this - but NOTE: there are some other MAJOR bugs in Xcode where it crashes itself if you don't do this)
re-open xcode
drag/drop the .h and .m back into Xcode
wait a few seconds (some of Xcode's core methods are asynchronous - allowing it to corrupt your project)
finally, when it seems to be doing nothing (and your hard disk isn't making any noise any more): cmd-b to re-build
I have a 100% success rate with this method. I just tried refactoring with Apple's "Refactor -> Rename" in latest xcode and it failed - again!
(this time with the incorrect error: "Unable to determine the language of", one of those error messages where Apple put the wrong text in place)
I'm going to my project at finder, then change files name by get info.
After that, at xcode -> Project Navigator I delete the files.
At end, I click right on the class and Add files to ..., and add these files again.
It worked for me.
For me I realized Refactor was disabled because the Xcode project I had opened was referencing a Base SDK that was missing. Edit Project Settings and in the Build tab set the Base SDK to one that you have (like for me this was iOS 4.2). This enabled Refactor for me.
Also, it could happen that you renamed the filename for the class, either outside xcode or by ctrl-clicking the filename and then renaming it.
xcode refuses to refactor if filename does not match with the class name.
Go to your class' header file and find the line that looks similar to this:
#interface YourClassName
Right click on the class symbol (e.g. YourClassName) and you should be able to select Refactor -> Rename...
I just tried this and it works in Xcode 5.
This may be a bit late, but I stumbled across this post because I was unable to refactor my "ViewController.swift" file to "WhateverViewController.swift". I tried selecting the file in the Project Navigator and then selecting "Editor -> Refactor" from the top menu, but 'rename' is always greyed out.
Instead, what worked was selecting the ViewController name from the editor. So if you have:
class ViewController: UIViewController {
// Code here...
}
Highlight the "ViewController" word and then select Refactor from the menu or right-click and select Refactor -> rename.
Hopefully that helps...
Had this problem as well. I ran through trying to find missing SDKs, saving files, and looking for Objective-C++ code as mentioned above, and all it took to fix my machine was rebooting XCode.
Seems a little buggy still.
BTW, this was for XCode 4.0.1
Refactoring works If you first change the file name in the project navigator.