Can we create iOS app with swift and obj c both? - ios

Can we create iOS app with swift and obj c both.My half app is made using OBJ-C and just want to improve apps performance so is that possible to use swift in between

Yes you can.
You have to add objc header #imports in the <module name>-Bridging-Header.h that you want to make available in swift, and you can use swift code from objc by importing the <module name>-Swift.h file in your objc code.
Note that the latter header file is automatically generated when building, and it is not visible in the project navigator - but you can open it by cmd+click-ing its name in an existing #import. Also to note that if compilation fails, most likely the file will not be generated.
Last, I highly recommend that you avoid circular references between objc and swift - for instance, creating a SwiftClass, inherited from BaseObjcClass, and using SwiftClass from objc - I've experienced that it doesn't work (compilation errors), and I am not aware of any workaround

Yes, you can. You just need to follow this:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Apple’s intention to replace the Objective-C language using Swift, it is not practical in the short term simply due to the fact that developers are deeply entrenched in Objective-C. Rather than force Swift down the developer’s throat, Apple has made it easy to allow Objective-C to interoperate with Swift.
http://mobiforge.com/design-development/using-objective-c-and-swift-together-ios-apps

Dec 2018 - I found this useful YouTube video that helps explain what files you need to add and how to make the connections.
YouTube - How to use Swift and Objective-C in the same project

Related

importing Pure Swift Framework in Objective-C Project

I want to import a Swift framework called "Beethoven" into Objective-C project. I import this framework via cocoapods.
The problem is that framework is written in pure swift. Since the classes are not subclass of NSObject they can't be used directly in my objC classes.
I am newbie in Swift and intermediate in objC, but I think there may be 2 solutions for that:
1-Modifying the whole library: which is probably not an optimal solution.
2-Using a new class which behaves as an interface between swift framework and my objC code. I think second solution will be a better alternative in terms of time and effort needed.
Actually below given post explains modifying the pure swift classes but I don't know how to apply this in my case.
How to use Swift non-NSObject subclass in Objective-C
As I mentioned I am not a very capable programmer and there may be better solutions for that problem. I would be grateful if someone can help me and suggest the best solution for integrating pure Swift framework into my objC project.
NSObject is usually not the only problem. Swift has many features that are not supported by Objective-C. To name a few: swift value types (struct's), swift enums with attached values. If a library uses any of those, it will not be possible to auto-generate an Objective-C header (.h) for it.
Adapting the code of the library on the spot might work, but it is likely more work long term in case if you ever need to update that library again.
Your 2nd approach sounds better: create a layer that is compatible with Objective-C on top of the library which exports methods that need to be exported with #objc and adapts the types. If you go that way consider making a PR contribution to the original library so that everyone could use it in Objective-C projects, and you share responsibility of updating it when the swift code changes.
The 3rd approach would be actually to rewrite some parts of you app to Swift. That might or might not be easier depending on the size of the part of the app that is using the library and how well it is isolated from the rest of the app.

Swift framework for Swift and Objective C projects

I am building a new framework. The project is to be coded into Swift language, however the clients using this framework have the freedom of using either swift or Objective-C framework.
The question is how do I start. There could be numerous issues like
using structs in swift code but it cannot be made available in
objective C framework.
optionals are missing objective c
Even if I
write different set of files for Swift and Objective C, how will I
map them onto different frameworks under the same project.
Enums with other than Int as rawValue can't be used.
Tuples would not work
I know there have been a few questions around this but none have any satisfactory answer.
PS - Any link to a tutorial or blog would be super helpful too
I did this and got some unexpected results: I have trouble integrating the framework in Swift application. Objective-C works just fine.
You mentioned some of the caveats here.
I suggest doing this iteratively while writing test application in Objective-C which uses all the features. This way if there is some feature that does not cross Swift to Objective-C boundary well, it will be discovered as early as possible.
Your remarks about issues are generally correct with one small exception: optionals are not missing from Objective-C, they appear as nullable/nonnull modifiers on variables and method parameters. Although this does not replace optionals fully, it helps detecting issues early in the process.
Here is a random list of some other issues I discovered:
Bridging between Swift Error and NSError used in Objective-C. The conversion is not always as smooth at it could be, so better use NSError in exported code.
If you mix Objective-C and Swift in your framework, you cannot use bridging header, instead using modulemap files which tend to turn pretty large and complex.
Since you cannot embed frameworks inside a framework, you have to make sure that the application sets ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES flag for its target. This has to be clearly indicated in the documentation. Also, when creating fat library for your framework, you have to strip these files from the distributed SDK.
And, as I said in the beginning, I still have no success using the resulting mixed language framework in Swift application.
Hope, this will add to your list of things to take into account when developing the library.

Issues while using swift code in objective-c

I am very new to iOS who is trying to use swift in objective-c. I created a swift class in the objective-c project and i am able to call that class from an objective-c view controller.
The problem is if i make any changes in the swift code, it doesn't seem to be available immediately for objective-c class to access. Each time i change something in the swift class, i will have to build the project and only then it seems to be available in objective-c.
Is there a configuration that i am missing or it is expected to work this way by design?
Please read this explanation from apple. Read the sub-heading "Importing Swift into Objective-C". The generation of header files might be the explanation for your issue.
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID122.

Can we build and upload ios app with swift and objective-c mixed code?

Actually, I am working on one app and started with the objective-c. Now i have started working on swift with the same app for the further development and it will work with both the class (objc and swift).
Now I have doubt that it will accept by apple store or not.
Please help me.
This is fine, Apple provides the mechanisms for interacting with both and will not drop support for Objective-C for quite sometime. Many large organisations still have the majority of their code written in Obj-C (including Apple) so they need to give people time to transition, they aren't going to switch over immediately to a relatively immature language.
You can use Swift and Objective-c together. There is no issue in it. You need to create a bridging header file.
Please read this
Swift and Objective-C in the Same Project

Generic implementation of 3rd party Libraries and Frameworks in Swift

I see there’s many libraries, open source, like Cocos2d, ShareKit, FormatterKit, etc. that seems up to date. (Check GitHub).
But still, implementation, snippets, samples and manual are intended to be written in Objective-C.
But, a method is a method… I think… If I get an instance of any framework/library of these and I call the method in a full Swift enviroment, would I be affected behind the scenes?
After June WWDC Swift gonna get serious, Apple is pushing hard, I’m just getting ready for the move and I don’t want my Apps to implement a soon-to-be deprecated library implementation in Objective-C.
I do not understand your question so much that I will regard your question as "Is it possible to use the Objective-C&Swiftin the same project?"
Yes, Apple claimed that developers can use Bridging-Header to implement double languages development, whatever your main developing language is ObjC or Swift. The specific details and theories about it you shall check from there.
Next I want to tell you some errors occurred in my project when I used Bridging-Header.
I always meet this kind of problem like import 'file not found'. I think you can try to check your file&folder hierarchy. Such as you have a root folder named Project, as is often the case, there is another Project in your previous Project, the root folder.
And Checking the Objective-C Bridging Header in the Build Setting. Sometime error can occurred for Project/Project/Bridging-Header.h. And the error can be corrected by changing the definition as Project/Bridging-Header.h.
If the location of Bridging-Header.h is not matching with the definition in the Build Setting, the error like import file not found will occur.
I just tell your my solution for Bridging issue I already met, I hope it can help you.

Resources