Use of a class from a second target - ios

As the IBDesignable attribute needs the designable class to be in a seperate target I created a second target which worked great in the designer. When I then tried to use the new class in my code I get the error
"Use of unresolved identifier 'CustomMarker'"
I have imported the second target in my main target -- import CustomViews -- and I can cmd+click the target name to see the main .h file that describes the target; where the class is included. The compiler still can't find the class in my code though.
Any thoughts?

I had the same issue. Fixed this by changing access modifier of the class to public.
#IBDesignable public class MyCustomView: UIView {
}
By default classes and methods will be at internal access level. If you want to use that out side of the target, you should mark it as public. Then only it will be the part of public interface(visible to outside of target)

second target must be of type "framework".
you must also link it in build phases like as an external framework. link binary with libraries must have a link to your customViews framework and embed framework must have that link too.
that should be enough.

Related

Import a Swift module in Obj C module

I have a xcworkspace with two xcodeprojects inside (one is a static library where all the base functionalities are implemented and the other is a demo project which uses classes from first one) - and all the code so far has been written in Objective C. What I want to achieve is create a Swift class in the static library and then access it from an Objective C class in the 'demo' xcodeproj.
I have created this 'Test.swift' class and a bridging header that was created automatically (changed the Defines Module property to YES in the Build setting), and everything works well - I can access it from Obj C classes in the same project. Next, I am creating a new "DemoTest.swift" Swift class in the 'demo' project and subclassing the Test.swift (which works). However, when trying to access this class from an Objective C file in the 'demo' project, the compiler doesn't recognize my "base" module -
In file included from
xxxxx/AppDelegate.m:26:
xxxxx/mpdemo-Swift.h:189:9: fatal error: module 'mpbba' not found
#import mpbba;
~~~~~~~^~~~~
1 error generated.
My question is how can I have a Swift code imported into another Objective C module?
Here's what to do:
configure an Objective-C bridging header
Click on your Xcode Project file
Click on Build Settings
Find the Search bar and search for Defines Module.
Change the value to Yes.
Search Product Module Name.
Change the value to the name of your project.
In class, add the following: #import "YourProjectName-Swift.h"
Hope this will work. let me know if you need anything else.

Cannot call value of non-function 'module<F1>'

I created a Swift framework, just a simple test.
The Swift file (F1.swift) code:
public class F1{
public init(){
print("inited")
}
public func call(){
print("called")
}
}
Then, I built the framework and I imported it into another project.
I tried to use it this way:
import F1
in the viewDidLoad of a UIViewController:
var c = F1()
c.call()
The F1.framework has been dragged under:
General > Embedded Binaries
General > Linked Frameworks and Binaries
and I can also see it under:
Build Phases > Link Library With Binaries
Build Phases > Embed frameworks
The XCode "reaction": no issues with the import statement.
I receive an error exactly where the class is instantiated:
Cannot call value of non-function 'module...'
Am I missing something?
[update] Based on some online resources and some other test, I'm supposing the problem lies in Build settings: eg. Build Active Architecture Only could be involved, but it would be interesting to understand how and why.
Find minimal sample Xcode project here.
Using your posted code gives the expected results here:
inited
called
In your example you've named the single class in your module F1 to the same name as the module, namely F1. Most likely Swift can't differentiate between module namespacing and the actual name of your class in the module, so when you just write F1, Swift possibly infers this to be an explicit namespacing annotation; refering to the namespace F1 (made available by your import of module F1). A namespace can naturally not be treated as a type, which would explain the error message you're prompted with ([emphasis mine])
Cannot call value of non-function 'module...'
You may test this theory by explicitly calling the class F1's initializer of module F1, by including both the module namespace and its (single) class type in the call:
var c = F1.F1()

Swift Compiler Error: Use of unresolved identifier 'name'

I tried to include a class called 'name' and I got an error:
Swift Compiler Error: Use of unresolved identifier 'name'
The class exists and doesn't contain any compile errors.
There could be a few possible issues.
One of the classes has a Testing target and other one doesn't. You have to even include all of your classes in the testing target or none of them.
If it's Objective C class, check that the class is in ObjectiveC bridging header file.
If it's NSManagedObject subclass. Add #objc(className) before the class declaration.
If it's part of a different framework, make sure that the class or function is public
I had this one too. You will probably find that your first class is included in your testing module and that "name" isn't. Simply, if you include a class in testing, then every class that it references has to be in testing.
I had this problem too. I was trying to reference Class 1 within the code in Class 2. My problem was that Class 2 had target memberships in A and B, and Class 1 only had Target Memberships in Class A.
You can fix this by opening the Utilities Tab (farthest right button on the top bar of the Xcode window), and make sure that the same boxes are checked for both classes in the Target Membership subsection.
Got problem solved by
Target -> Build Phases -> Compile Sources -> Adding the class file
Add one more to the list.
If it is part of another framework, make certain that the "Build Active Architecture Only" settings are the same.

how to package UIViewController?

I have create a UIViewController, e.g DemoTableViewController, and want to reuse it in other project. Instead of copying source file of controller, I need to create a library to reuse it, however, after I create a static library containing DemoTableViewController, and set the class of a view controller in storyboard, there's a an error:
Unknown class DemoTableViewController in Interface Builder file.
what can I do?
In the custom class field on the right top, you can see that you have given it as DemoTableViewController instead of DemoViewController(Assuming that what you have mentioned in question as DemoViewController is the class name you have given to this custom class). Change that and then check. It should work.
If the above is not working, make sure you have copied the DemoTableViewController.h file to your current project and this library is added to target settings -> Linked frameworks and libraries.

Unknown class info in Interface Builder file

like in the title i got a crazy massage and i cannot handle with it
Unknown class info in Interface Builder file.
Every time I run my Project some crazy bugs will appear or some bars disappear. Did anyone know what to do?
Try this, in order:
Product->Clean in Xcode
Delete the app from the simulator or device
Restart Xcode
(Build &) Run again
If this doesn't help, you likely have a reference to a class in the nib or storyboard that you have to manually find and remove.
The warning may be because you have assigned a removed/deleted custom class name for one of the UIView subclass. In order to fix such scenarios,
Open interface builder class as Source Code by right clicking on it (Open As -> Source Code).
Find your unknown class name in the source file. And handle accordingly.
It worked for me.
See my answer to https://stackoverflow.com/a/29013058/784318
I did run into this problem today using Swift.
I changed a class Model.h + Model.m to a Model.swift.
This object was used in Interface Builder with the class = Model.
As soon as I replaced the object the class could no longer be loaded.
What I had to do was to change the class reference in IB from:
Class = Model
Module =
to
Class = Model
Module = <TARGETNAME>
You'll find the <TARGETNAME> in the build settings. It is also the name that shows up in your generated Swift-Header: #import "TARGETNAME-Swift.h"
I have solved this problem (while using Swift 4 / Xcode 10.1) by going to Custom Class panel, checking 'Inherit Module From Target' (right below Class & Module row).
I have got the same issue with Lottie library.
Unknown class AnimationView in Interface Builder file.
It can happens if your class view is in an other module.
To resolve this, go in your IB file (Xib or storyboard) / Show the identity inspector and set correctly the module :
I tend to see these types of errors when you change the name of your view and view controller classes after customizing a view or controller in Interface Builder to that class. I'd go through your views and controllers in your nib or storyboard to see if your custom classes still exist and match accordingly.
You'll run into issues with IB files when you rename or delete the underlying code without updating the IB file. A similar issue occurs when deleting or changing the name of IBOutlets after linking them to an object in Interface Builder. You'll still have the reference in Interface Builder unless you delete it explicitly.
Lastly, you can look at the IB file as a text file. It's an xml document, where you'll see all the class references. You can see if there are any classes there that may not exist in your project anymore.
This also happens if you deleted ClassName.m from Build Phases/Compile Sources
I'll restate the problem, with a bit of context from my end, then provide the solution:
I have created a custom Swift framework, and imported the framework project into the workspace. Then, I made the framework a target dependency to my Swift application. Finally, I referenced a class from the framework in Interface builder.
I kept getting the class not found error, and I noticed in the debugger it had the output class was the mangled/unmangled ObjC counterpart ..
The solution:
Simply add your framework header to your ObjC bridging header:
#import <MazKit/MazKit-Swift.h> // In my case, MazKit
Now, the symbols are exported, and you can verify that by cmd + click on the header itself, and you'll see the mangled/unmangled class names.
Project -> General -> Deployment Info -> Main Interface <- clear this field.
It seems you have deleted file Main (Storyboard) but you still have reference to it in project properties.
It could be that the file was not copied over in the build process. Goto the Build Phases Tab and check the copy Bundle resources to see if the file is there. If not add to the project clean and rebuild to see if that fixes your issue. This has helps me.
I had renamed one of my TableViewCell Class. Nothing worked other than checking the Inherit Module From Target checkbox.
Open the xib file > Goto Identity Inspector while it is selected > Check the checkbox (also make sure the Class name is correct).
Done!

Resources