How to properly import subclass frameworks into projects - swift

I have a question regarding importing subclasses into projects. We often find useful subclasses on GitHub that we can use. In my case it's just a Swift Xcode project which is a Swift port of an Objective-C subclass.
Contains what you see below.
There's no storyboard or anything so I'm wondering why the product folder at the bottom has a SiriWaveformView.framework suitcase icon but there is no framework with this name in the Finder.
Is this technically a framework/API? Do I simply copy SiriWaveformView.swift and SiriWaveformView.h into my own project? How do I connect an interface object to this class?

Related

Xcode Ignoring import

I have just installed Xcode 11 and when I try to create new fresh project with the SwiftUI check mark selected it returns an error.
Not able to build and run successfully.
File 'ContentView.swift' is part of module 'SwiftUI'; ignoring import
ContentView.swift
Use of undeclared type 'View'
SceneDelegate.swift
Use of unresolved identifier 'UIHostingController'
I have tried removing all derived data and also set command-line tools to 11
Your project is named SwiftUI - please try using a different name.
Detailed Answer
Each project you create has a module with the same name as the project. So there are two SwifUI modules here:
The actual SwiftUI
The project itself
Xcode always takes the nearest definition as the default. So your SwiftUI is closer than the system's SwiftUI. But you are in the project's module already! So Xcode ignores the import.
A very common mistake is to name the project same as one of the using frameworks! (e.g. CoreData, SwiftUI, SceneKit, Metal)
Solution
As Matteo mentioned in his answer, Don't name your project same with another module. Change it to anything else.
Note that It could appear as an error too. For example, if you name your project CoreData and using SwiftUI, the error appears as Circular dependency error:
Circular dependency between modules 'CoreData' and 'SwiftUI'
Because Xcode gets confused about modules and can not detect what the real issue is.
How can we access our module's classes instead of the system's module?
Imagine you have a class named Section in a custom framework called MyProject and you imported it alongside the SwiftUI.
import SwiftUI
import MyProject
Section // <- This could be either SwiftUI's section or MyProject's Section
To make it clear for the compiler (and anyone else), you should call the module before the class name:
SwiftUI.Section // <- This returns the SwiftUI's Section
MyProject.Section // <- This returns the MyProject's Section
Try with different project name. With SwiftUI, it will always show compilation error. Just change the name and enjoy coding with SwiftUI

Unknown class <MyViewController> in Interface Builder file + Localization issue

I have an app with two localizations.
Usually I use 'NSLocalizedString' and keep the same source code for both localizations, but one of my ViewControllers has to look and function different for each locale.
So I pressed 'Localize..' and created two localized versions of it. Normal stuff.
Since then - on runtime I get:
Unknown class MyViewController in Interface Builder file.
and the viewcontroller is not displayed (it's a UITableViewController if it matters).
I checked in my storyboard and the custom class is still listed & linked to the view controller display. I didn't change the class name in each locale - it's still the same.
what gives here?
thanks
i experimented this kind of problem in my project and my problem was my viewcontrollers .m file was not included in the compile sources in Build Phases->Compile Sources i manually add it to the Compile Sources and problem solved for me.
i hope it will help you.

Using custom UI views from library in interface builder

I have 2 projects. One is a library that I am compiling to a .a file and the other is the application. The library has a class (AdFactoryView) that extends UIView that is used to display ads. Previously I was creating the AdFactoryView programmatically calling initWithFrame, now I'm trying to put the class directly into a nib and use it from there. I can find the class in the interface builder library, add it and link up the reference to an IBOutlet but when the code runs I get a message that says:
Unknown class AdFactoryView in Interface Builder file.
And the IBOutlet contains a UIView instead of an AdFactoryView. How do I get the view from my library to work correctly?
Extra information:
The application is built by importing the .a and .h files for the library. I have another project that references the entire library project directly that I use for testing, in this project AdFactoryView does not show up in interface builder.
Edit: This suddenly started working in my application project, though I still can't see the custom views in interface builder in my test project.

Interface Builder can't see classes in a static library

I have refactored some UIView sub-classes into a static library. However, when using Interface Builder to create view components for a project that uses the static library I find that it is unaware of the library classes. What do I need to do to make the class interfaces visible to Interface Builder?
Update: The correct answer refers to dragging the headers into the 'XIB browser'. The '.h' files can be dragged from a finder window to the window area identified in this image:
alt text http://img211.imageshack.us/img211/1221/xibbrowser.png
Try dragging the static library into your xib browser in Interface Builder. I haven't tried this with a static library, but the concept is the same. When you drag header files into IB, you can access those classes.
LexH, try linking with the -ObjC flag when building your static library. That worked for me... for about a year :-) I found this post as the problem has returned with a fresh OSX install and an upgrade in xcode. But it worked in XCode 3.1.2.
David
Add the same problem as LexH. It worked only when I called a dummy class method.
The problem was that I did not add my static library to the "link binary with libraries" under target.
Strangely everything else worked.
I followed this guide to link with my static lib Create static lib
I had the same problem. Dragging the library or headers to XIB Browser didn't work. Read Class Files didn't work. So I called:
[MyLibraryClass version]; // Substitute your class name for "MyLibraryClass".
This worked. version is a class method of NSObject, so all subclasses of NSObject inherit it.

Packaging a Bundle with a static library

I have a static library that includes some xibs. These will basically be the same across projects. I'd like to include the xibs as part of the library. I can include their veiwcontrollers, reference these controllers in the calling project but then there isn't a xib to load. When I right click the xib in the library project, it can't be part of the target.
I thought about creating a CFPluginBundle but that creates a new project. I'd loose all of my IBOutlet and IBAction references. What is the best way to reuse xibs that also have outlets and actions to specific controllers?
Here more discussion about it: Can you reference Xib files from static libraries on the iPhone?
I had the same problem when I wanted to export my project as a library for other developers to use.
I found the perfect solution to my view and it seems it will answer yours too.
There is an xcode plugin that allows you to build your project as a library which includes the resources as well.
https://github.com/kstenerud/iOS-Universal-Framework
I do not know the guys that built this plugin, but it works like a charm
I'm not sure what you mean by "include the xibs as part of the library", since static libraries can't have resources--but they also aren't shipped stand-alone, so they don't need to. If you just want code re-use for your own projects, you could keep the xibs where-ever you keep the static library, and just include the xibs in any project that uses the library.
If you go the CFPluginBundle route, you can make new bundle targets in an existing project; there's nothing magic about the templates, they just take care of making dummy files and turning on the right build settings. You can copy those into a new target in your existing project and it will work just fine. That said, I'm not sure what you mean about losing IBOutlet and IBAction references, since that information is part of the xib (and the class you are using in the xib), not the project.