I'm new in swift-ios. I'm studying this
when I'm trying to create cocoa file,
import cocoa
is giving error.
I've searched in google and found cocoa class not work in ios-swift. it works in osx.
So I'm not getting what'll I do now. Any help appreciated.
Cocoa is a framework for macOS. However, CocoaTouch is an umbrella module for iOS. To import the main module dependency for an iOS app, you use
import Foundation
And to import the main UI module for an iOS app, you use
import UIKit
The term “Cocoa” has been used to refer generically to any class or object that is based on the Objective-C runtime and inherits from the root class, NSObject.
Related
I have added a framework to my xcode swift 4.x project. The framework was built using objective-c, is it not possible to set-up a bridging header w/ frameworks?
How will I be able to import my framework so I can start using its functionality?
Additionally, the project I'm using originally has a public cocoapod, I'm able to do the normal import PROJECT. But I have compiled the source to create a framework w/o the need of cocoapods, I have added the framework to the project, how will I be able to import it now in my swift classes?
I have swift 4 project, recently pull from another branch. But whenever i want to import any class which is already exist in project show me the error "No such module...". Please suggest me what is wrong.
You don't need to import files in Swift as they are globally available. In Swift you can only import module, e.g. any framework.
In Swift, if a class already exist in project then you don't need to import it; you can directly use its properties by creating its instance. but if you have problem with it then you need to check target membership for the class. it should be checked for the project like in my example in pics
I frankly have a hard time understanding how imports work in Swift. When I make a new class it will start out with:
import foundation
As an alternative we could use import Swift or import UIKit depending on what libraries we need. BUT I've noticed that if I simply remove the imports my projects runs without any problems (even though I use classes from these libraries). This is where I need some help: I'm wondering if that is because I have internal frameworks that I import Swift/UIKit/Foundation and thereby get the import. So imports works like plague... if they touch a new class everything that class touches will have access to that import.
Yes, when a class that you use imports that framework, it is imported in your class too. This is to make things more clear, for example when class Foo's method abc has a parameter that needs UIKit, which is present in Foundation. Therefore, when you use the class Foo, UIKit is automatically imported.
As a side note, importing UIKit will import Foundation, in which it will also import Darwin. So it is really like plague. If a third party library (such as Charts) imports UIKit, it imports Foundation and Darwin too.
So we found the issue. Someone had added header file to each framework with:
#import <UIKit/UIKit.h>
Removing this line made everything less mad.
Now about how imports works. If a class A in library A import library B, class A will not have access to UIKit even though library B imports UIKit. This is how I'd expect it to work and how it actually works.
I created a Cocoa Touch Framework, this framework contains mostly Objective-C codes, my goal is to keep these objective-c codes private, so I added an adapter class in Swift as a middleman between the consumer and my objective-c codes. To do this, I created module to import my objective-c classes and allow my swift class to access these objc classes. I used this guide and clang doc.
So with this I am able to access my objc codes inside my Swift class.
import PrivateObjc
public class SwiftClass{
public func createPersonClass() -> ObjcPerson {
return ObjcPerson()
}
}
The code snippet above works just fine, within my framework atleast. But when I try to use my framework in another project, I get this error:
Missing required module 'PrivateObjc'
I've seen similar posts here in SO but most of them are using Carthage or Cocoapods, some about CommonCrypto. This question is about a custom framework created from scratch. Can anyone enlighten me how to accomplish this? Thanks!
I have a project with large amount of obj-c and swift code inside it.
I have an obj-c class which imports ProjectName-Swift.h to let that class access swift code.
Now I need to expose that obj-c class to the swift so I can access it from swift code.
The problem is that after import in bridging header name of obj-c class project stops building with error that it cannot find ProjectName-Swift.h which import is stated in obj-c class.
I cannot remove import of ProjectName-Swift.h from obj-c class because after that class breaks.
What can I do ?
OK, had one answer and then re-read the question. Make absolutely certain that your import of the swift header is the Module Name of the project (not necessarily the project name):
Apple Documentation: Swift and Objective-C in the Same Project
More specifically, go to the section marked "Importing Swift into Objective-C" and read it. And even more specifically:
Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to “Yes”.
Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names:
#import <ProductName/ProductModuleName-Swift.h>