Migrate and add Swift to existing Objective-C project - objective-c

I have an old project that I built on iOS 7 with Xcode 5. Recently I upgraded to Xcode 6 GM. I opened that project in new Xcode and tried adding this swift code to one of my method in v1Album.m file.
let alertController = UIAlertController(title: "Hey AppCoda", message: "What do you want to do?", preferredStyle: .Alert)
I get an error saying Use of undeclared identified let
I have been searching, trying to see what I need to do, what settings I need to change in my project in order for it to start recognizing swift code. Every tutorial online I see is explaining how to start writing swift code in a brand new project. All I need is to be pointed in the right direction.
EDIT:
What I have read so far on migrating project to swift from Apple Doc here is that
in order to write swift code I need a brand new swift class
I need to rewire all my objective-c code from v1Album.m to v1Album.swift
Then I deselect v1Album.m from my project.
I cannot use swift code directly in v1Album.m without first defining that method in v1Album.swift file and then using a bridging header.
Basically I cannot do what I am trying to do i.e. write straight up swift code in *.m file.
Please correct me, I maybe completely wrong.

Related

Why the Xcode objective c interface header name isn't found for Swift 4 language

I am currently working on an Objective C project and need few swift files support in it. According to Apple official guidelines, after I create a bridging header, I did this
#import "ProjectName-Swift.h"
to access the swift classes. It didn't work. So, I changed the "Defines Module" to "YES", rewrote the header prefixing with the module name, all to vain. Then I thought of switching the compiling language to "Swift 3" and voila, it works! App runs on the phone.
However, there are few plugins and frameworks that I am also using which don't work with Swift 3. So, no peace here. I need a solution. Help!
So finally after searching the web an another SO post (answer) helped me out. It could be because of bridging issues or compiling thing, I have to "BUILD" the project to get the header generated or something. The solution was from this post https://stackoverflow.com/a/45338549/8102549 or here
Check out your Build settings for Objective-C generated Interface Header Name make sure that the Install Objective-C Compatibility Header is set to Yes. Also, ensure the file name there is as you expect.

Detect Xcode project development language

We can create a new Xcode project using Objective-C or Swift.
I want to detect, which language was selected when a project was created?
Projects can be a mix of Objective-C and Swift, but I am concerned about the language, selected during project creation.
May be it can be through pbxproj file or other better way.
Thanks.
ObjC project always contains the entry point that is main function.
Usually it is inside main.m file but it is not necessary. Developer can replace it to any name.
I tried 2 simple projects. One is in ObjC and second one is in Swift.
When I was tried to add new file the Xcode offered Swift language for Swift project and Obj-C for Obj-C project file.
Next I removed in Swift project AppDelegate.swift file and added AppDelegate.h and AppDelegate.m and (sic!) main.m files. of course I had to create bridging file for obj-c.
Which contains
#import "AppDelegate.h"
I was able to compile this project but when I tried to add new file Xcode offered me to add Objective-C file. But initially the project was created as Swift project.
I did similar manipulation for Obj-C project.
I removed AppDelegate.h and .m file as well as main.m and added AppDelegate.swift
I was asked to create bridge file and did empty file.
Next I went to «Build Settings» and switched Define Module parameter to YES value. (Without this I got linker error).
After it I was able to build and run this initially obj-c project which has AppDelegate in Swift now.
When I tried to add a new file the Xcode offered me to add new Obj-C file too.
So. It looks like you cannot detect initial language based on a parameter in Xcode because project can be always corrected. I think that rarely the developer will try to replace AppDelegate in a project and add\remove main entry point.
Hope this helps you.

The “Swift Language Version” (SWIFT_VERSION) build setting error with project in Objective C

So i just went to build my app after adding in core data to update my app to store and fetch some information and i got this error. (I am using XCode 9.2)
The “Swift Language Version” (SWIFT_VERSION) build setting must be set
to a supported value for targets which use Swift. This setting can be
set in the build settings editor.
Now whats confusing me is this in an Objective C app and surely the swift version shouldn't matter. I have browsed some threads on here stating that i need to update build setting to Swift 3.0 or to set values to Yes then back to No, however i have none of these fields when i search for Swift in the search bar.
Is this just a bizarre bug or have i mistakenly added some swift code?
Any ideas would help, thanks.
I experienced this exact same issue after adding Core Data to an Objective-C framework I was working on.
I discovered that the reason I was getting the "Swift Language Version" warning was that the Code Generation language in the .xcdatamodel defaulted to Swift. Once I changed this back to Objective-C, the warning went away.
You can find this property in the File Inspector when you have the .xcdatamodel selected in the Project Navigator.
For Objective C Projects created using Xcode 8 and now opening in Xcode 9, it is showing the same error as mentioned in the question.
To fix that, Press the + button in Build Settings and select Add User-Defined Setting.
Then in the new row created add SWIFT_VERSION as key and 3.2 as value like below.
It will fix the error for objective c projects.

Can Swift code embed in existing Objective-C file - or must one add that code to a discrete Swift file?

I have an Objective-c file which used to work with the older XCode's
with the emergence of XCode 6.x - code no longer performs
I found the same functionality in code written in Swift online
I read Apple's documentation about using bridging header's to allow the use of Swift with Objective-C. I was able to get that setup ok, I do believe.
What is not clear is - can I simply cut and paste the Swift code into my Objective-C file (a viewcontroller.me file actually), or am I limited to having to use a Swift file from the start ?
If I have to use a new file in my (very simple) app, Im not quite sure how to pull off having two viewcontroller.m files do the work where one used to work fine, so this method obviously creates a new problem for me.
Thanks for your assistance in advance !
I would recommend just creating a class or struct for your swift code, and access that class or struct in your Objective-C ViewController.

How do I create a file with the iOS\Cocoa Touch\Objective-C class template in Xcode

Forgive how simple this might sound but I'm stuck and need help. I am putting In App Purchases into may first app, which is not yet released. (So I'm very new)
I have been using a tutorial by Ray Wenderlich. I have just finished starting the Master Detail Application Template. Now it says I need to create a IAPHelper class which will be my class for the IAP the step I am on says to "create a file with the iOS\Cocoa Touch\Objective-C class template". However when I open Xcode and I select new project, I don't see this option. It has Single View Application etc.. the closest thing I see is Cocoa Touch Static Library.
Can someone tell me how I "create a file with the iOS\Cocoa Touch\Objective-C class template". I'm lost, do I do it from inside the Master Application Template? Or do I use some other template name like utility or empty application or Cocoa Touch Static Library? Or do I not have this in my version of Xcode? Or am I totally coming at this the wrong way? ahhhh!lol
I have Xcode 4.3.2
I know this is probably a no-brainer for some but not for me :(
Thanks for the help :)
In the current project you are working in, right click on a file and click "New File..." or go to File>New>File and then you will probably want a Objective-c class objection under Cocoa Touch
You need to do this in the same project which you have done till now. What you are trying to do is to create new project where as the tutorial is asking you to create new file. You have to do this in the same project which you are working on. In order to do that there is an option in file menu to add a new file to project.